Let's get started with our first best practice, Batching. A Batch is an object provided by the Eyes SDKs that offers the ability to organize your tests as well as other important features that we'll discuss later. A batch is a simple object that consists of a name, an ID as well as some other properties. In this section, we'll first add in another test case and see what our test results look like when running without batching. Next, we'll create a Batch object, run our tests again and compare those results to our first test run. At the end of this exercise, you'll understand why batching is considered a best practice and should be used in every automation framework when working with Applitools.
In our code framework, let's make some slight modifications as well as add in a 2nd test case. Let's go over all the changes that I made. Firstly, I added in a beforeAll hook and moved our driver initialization into this function. I've also changed our driver object to static so that we don't need to spin up a new driver for each test case. Normally, a static driver would not be recommended since it prevents our tests from running multi-threadedly, but for our purposes today this will work out fine.
I've also added in a second test case that is identical to our first test case except it navigates to a different URL. Lastly, I now call driver.close() in the afterAll hook rather than the afterEach hook. Now that we have 2 test cases, let's go ahead and run them both and review our results on the Applitools dashboard.
On the left side of our dashboard, we see entries for our tests that we just ran. Even though these test cases were a part of the same test run, they show up in two different entries on this side panel. Since we only have 2 test cases, reviewing these tests isn't a huge deal but imagine if we have 50 or 100 test cases and each of them was their own entry in this side panel. It would be hard to go through all of them and determine which tests were a part of the same test run and which tests were a part of other test runs. This is where batching comes into play and why it's so important. Batching allows you to take all of the test cases that you run and group them together in a logical manner. Rather than each test case being in their own row in the side panel, there will instead be only one entry in the side panel and all the test cases will appear in the main section here. Let's head back to our code and add in batching to our framework.
Let's go ahead and review the changes. Firstly, I added a batchInfo variable to the top of our test class.
Next, I initialized our BatchInfo object in the beforeAll method so that it can be shared between all of our test cases.
Lastly, beforeEach test, we simply apply this batch object to our Eyes object by calling eyes.setBatch(). Each Eyes instance that receives this batchInfo object will be grouped together. Let's run our tests again and then head over to our dashboard and review the results.
Now that we've added the BatchInfo object to our code, our test results look different. Firstly, we see that there is only one entry added to the sidebar and the name of that entry reflects the name of the batch. Also, every test that we ran is now grouped together under this batch in the main section. We can see how much cleaner reviewing our tests becomes and we can easily determine which test cases belong to which test run.
Let's pause for a second to recap what we just learned about Batching. We saw how, without batching, our tests are all separate when they show up on the Applitools dashboard. This is fine for one or two test cases but it becomes difficult to review our results at scale when we run many more test cases. Batching is a powerful tool since it allows you to group your test runs together, making them much easier to review. It also gives you the power to create multiple batches and group tests any way you see fit. Some users prefer batching together different sections of their application while others prefer grouping together entire test runs. With batching, all of this is possible and it's completely up to you. Also, while it's out of the scope of this course, Batching plays a key role in many of our integrations such as the CI and Github integrations. Batching is also required to configure test result alerts over email, Slack and Microsoft Teams. All of these integrations will be covered in a later course but I wanted to mention them here to show how Batching plays an even larger role than what we've seen so far.