Search⌘ K
AI Features

The Test Class

Explore how to structure Selenium Java test classes efficiently by using a base test class for shared setup and driver management, applying TestNG listeners for enhanced test monitoring, and leveraging page objects to keep test methods simple and maintainable. This lesson helps you understand best practices for organizing automated tests with minimal code duplication and effective error handling.

Each test case is automated by a test method of the VplTests class.

The test class has 3 test methods, one for each test case:

  1. keyword_Search_Returns_Results
  2. any_Page_Has_Results
  3. next_Page_Has_Results
Java
package Tests;
import org.testng.annotations.Test;
import org.testng.annotations.Listeners;
import org.testng.Assert;
import PageObjects.HomePage;
import PageObjects.ResultsPage;
@Listeners(Listener.class)
public class VplTests extends BaseTest
{
private static final String KEYWORD = "java";
@Test(groups = "High")
public void keyword_Search_Returns_Results_Test()
{
HomePage homePage = openHomePage();
ResultsPage resultsPage = homePage.searchFor(KEYWORD);
Assert.assertTrue(resultsPage.totalCount() > 0,
"total results count is not positive!");
}
@Test(groups = "Medium")
public void any_Page_Has_Results_Test()
{
HomePage homePage = openHomePage();
ResultsPage resultsPage = homePage.searchFor(KEYWORD);
Assert.assertEquals(resultsPage.resultsPerPage(), "1 to 10");
ResultsPage resultsPage3 = resultsPage.goToPage(3);
Assert.assertEquals(resultsPage3.resultsPerPage(), "21 to 30");
ResultsPage resultsPage5 = resultsPage3.goToPage(5);
Assert.assertEquals(resultsPage5.resultsPerPage(), "41 to 50");
}
@Test(groups = "Low")
public void next_Page_Has_Results_Test()
{
HomePage homePage = openHomePage();
ResultsPage resultsPage = homePage.searchFor(KEYWORD);
Assert.assertEquals(resultsPage.resultsPerPage(), "1 to 10");
ResultsPage resultsPage2 = resultsPage.goToNextPage();
Assert.assertEquals(resultsPage2.resultsPerPage(), "11 to 20");
ResultsPage resultsPage3 = resultsPage2.goToNextPage();
Assert.assertEquals(resultsPage3.resultsPerPage(), "21 to 30");
}
}

A few important things about the test class need to be discussed before we proceed.

No driver object

The test class does not have a driver object. The driver object is moved from the test class to the base test class so that there are no Selenium methods and objects in the test class.

No test fixtures

The test class does not have test fixtures to set up and clean the environment. The ...