Transcripted Summary

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



html 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.


# Adding a Utility Class for Eyes

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:

  • The constructor that takes the WebDriver and the app name
  • The instantiation of Eyes
  • Setting the API Key
  • A new method called setBatchName, which we will review shortly
  • All of our validation methods

This refactor gives a cleaner, more reusable way to use our visual validation within the automation framework.

# Creating a Batch with Applitools

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.


tests passed in IntelliJ

Let's have a look at the Test Manager to view the result of this test batch.


html tables

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.



Resources



© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR