Selenium with Java · Building a Framework

TestNG for Selenium: Annotations, Assertions and Data

Lesson 7 of 9 · 10 min read

TestNG runs your tests, decides the order, and reports what passed and failed. JUnit 5 does the same job. Most Selenium + Java teams use TestNG.

Annotations

AnnotationRunsTypical use
@BeforeSuiteOnce before everythingRead config, start a report
@BeforeClassOnce before a test classCreate test data
@BeforeMethodBefore every testStart the browser
@TestThe test itselfSteps and assertions
@AfterMethodAfter every testScreenshot on failure, quit the browser
@AfterClass / @AfterSuiteAt the endClean up data, finish the report

Assertions

Assert.assertEquals(actual, expected);
Assert.assertTrue(button.isEnabled(), "Log in button should be enabled");
Assert.assertFalse(error.isDisplayed());

A hard assertion stops the test at the first failure. A soft assertion collects all failures and reports them at the end:

SoftAssert soft = new SoftAssert();
soft.assertEquals(nameText, "Demo Tester");
soft.assertEquals(emailText, "demo@testqualityhub.com");
soft.assertAll(); // do not forget this line

Priorities, groups and disabling

@Test(groups = {"smoke"}, description = "Valid user can log in")
public void validLogin() { ... }

@Test(groups = {"regression"}, dependsOnMethods = "validLogin")
public void logout() { ... }

@Test(enabled = false) // skipped until bug 123 is fixed
public void remembersUser() { ... }

Try not to make tests depend on each other. A test that needs another test to pass first is hard to run alone.

Data-driven tests with a DataProvider

@DataProvider(name = "badLogins")
public Object[][] badLogins() {
    return new Object[][] {
        {"", "Demo@1234", "Email is required"},
        {"demo@testqualityhub.com", "", "Password is required"},
    };
}

@Test(dataProvider = "badLogins")
public void showsFieldError(String email, String password, String message) {
    loginPage.open();
    loginPage.logIn(email, password);
    Assert.assertTrue(loginPage.fieldErrors().contains(message));
}

One test method, many rows of data. This is how you cover equivalence classes and boundary values without copying code.

testng.xml

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Practice suite" parallel="methods" thread-count="2">
  <test name="Smoke">
    <groups><run><include name="smoke"/></run></groups>
    <classes>
      <class name="tests.LoginTest"/>
    </classes>
  </test>
</suite>

Run a suite from the terminal with mvn test -DsuiteXmlFile=testng.xml after adding the Surefire plugin to your pom.xml.

Practice: turn the boundary values from the boundary value lesson into a DataProvider for the practice register page.
All 9 lessons in Selenium with Java
  1. What Is Selenium WebDriver?
  2. Set Up Java, Maven and Selenium
  3. Write Your First Selenium Test
  4. Selenium Locators: ID, CSS, XPath and More
  5. Clicks, Typing, Dropdowns, Alerts, Frames and Windows
  6. Waits in Selenium: Implicit, Explicit and Fluent
  7. TestNG for Selenium: Annotations, Assertions and Data
  8. Page Object Model in Selenium Java
  9. Selenium Framework Structure, Reports and CI