In this chapter, you will learn how to organize visual tests into suites, known as batches, and run them collectively. You’ll also learn how to view and manage batches via the Test Manager dashboard. We will also refactor the Eyes code by moving it to its own utility class.
So far, in previous lessons we have looked at executing individual tests. But there will be times where you want to execute an entire test suit.
For example, let's look at this website: The Internet - Tables
Here we have a table that can be sorted many different ways: by last name, by first name, by email, by amount due, by website. We can create automation code that will create a batch of visual tests which will validation each one of these sort methods.
Because we are going to run an entire batch, I've tidied up our code quite a bit.
I've removed a lot of the Eyes
functionality from the BaseTests
and put it into its own class, EyesManager
:
Let's take a look at the new EyesManager
class.
public EyesManager(WebDriver driver, String appName){
this.driver = driver;
this.appName = appName;
eyes = new Eyes();
eyes.setApiKey(System.getProperty("applitools.api.key"));
}
public void setBatchName(String batchName){
eyes.setBatch(new BatchInfo(batchName));
}
public void validateWindow(){
eyes.open(driver, appName, Thread.currentThread().getStackTrace()[2].getMethodName());
eyes.checkWindow();
eyes.close();
}
public void validateElement(By locator){
eyes.open(driver, appName, Thread.currentThread().getStackTrace()[2].getMethodName());
eyes.checkElement(locator);
eyes.close();
}
public void validateFrame(String locator){
eyes.open(driver, appName, Thread.currentThread().getStackTrace()[2].getMethodName());
eyes.checkFrame(locator);
eyes.close();
}
public void abort(){
eyes.abortIfNotClosed();
}
As you can see, we have:
Eyes
setBatchName
, which we will review shortlyThis refactor gives a cleaner, more reusable way to use our visual validation within the automation framework.
Note:
Setting the batch name is how you define a test suite with Eyes
. Before the class runs, we can indicate that we are running a batch by setting the batch name. Calling setBatchName
sets the batch name and every test that runs after the setBatchName
call will be assigned to this batch. And if setBatchName
is called again with another name, that will start a new batch.
Now let's look at creating a batch of tests.
public class TheInternetTests extends BaseTests {
private SortableDataTablesPage page = new SortableDataTablesPage(driver);
@BeforeClass
public static void startVisualTestSuite(){
eyesManager.setBatchName("Sort Table");
driver.get(System.getProperty("sites.tables.url"));
}
}
For this demonstration, we have five different tests we are going to run as part of the batch.
Test for Last Name Sort
@Test
public void testSortByLastName(){
page.sortLastNameColumn();
eyesManager.validateElement(page.getTableElementLocator());
}
Test for First Name Sort
@Test
public void testSortByFirstName(){
page.sortFirstNameColumn();
eyesManager.validateElement(page.getTableElementLocator());
}
Test for Email Sort
@Test
public void testSortByEmail(){
page.sortEmailColumn();
eyesManager.validateElement(page.getTableElementLocator());
}
Test for Amount Due Sort
@Test
public void testSortDueDate(){
page.sortDueColumn();
eyesManager.validateElement(page.getTableElementLocator());
}
Test for Website Sort
@Test
public void testSortByWebsite(){
page.sortWebsiteColumn();
eyesManager.validateElement(page.getTableElementLocator());
}
Note:
Notice with the validateElement
, instead of hard coding the By
locator, I have added a method to the SortableDataTablesPage class that returns that locator. This keeps our code nice and clean by excluding hardcoded locators from test classes.
When I run this batch, you will see that each of the tests have run and have passed.
Let's have a look at the Test Manager to view the result of this test batch.
Notice now, as opposed to having a single test, we now have the entire test batch listed here. And as opposed to having the individual tests, now we have all five of the tests that have run.
We see the status of the individual tests and a collective status. If any of these tests were not to have passed, then the collective status of the suite would have been Unresolved until we went in and resolved it.