Home Guide TestNG Annotations in Selenium Webdriver with Examples

TestNG Annotations in Selenium Webdriver with Examples

By Neha Vaidya, Community Contributor -

TestNG Framework is an open-source test automation framework inspired by JUnit and NUnit. It is considered as an upgraded version of these two frameworks as it provides additional functionalities like test annotations, prioritization, grouping, parameterization and sequencing techniques in the code – which were not possible earlier.

This article will discuss a number of TestNG annotations, and the role they play in testing.

What are TestNG Annotations?

TestNG Annotations are used to control the next method to be executed in the test script. TestNG annotations are defined before every method in the test code. In case any method is not prefixed with annotations, it will be ignored and not be executed as part of the test code. To define them, methods need to be simply annotated with ‘@Test‘.

Types of TestNG Annotations

Below is the list of annotations that TestNG support in Selenium

  • BeforeSuite
  • BeforeTest
  • BeforeClass
  • BeforeMethod
  • Test Case
  • AfterMethod
  • AfterClass
  • AfterTest
  • AfterSuite

Let’s explore how these methods work.

  • @BeforeMethod: This will be executed before every @test annotated method.
  • @AfterMethod: This will be executed after every @test annotated method.
  • @BeforeClass: This will be executed before first @Test method execution. It will be executed one only time throughout the test case.
  • @AfterClass: This will be executed after all test methods in the current class have been run
  • @BeforeTest: This will be executed before the first @Test annotated method. It can be executed multiple times before the test case.
  • @AfterTest: A method with this annotation will be executed when all @Test annotated methods complete the execution of those classes inside the <test> tag in the TestNG.xml file.
  • @BeforeSuite: It will run only once, before all tests in the suite are executed.
  • @AfterSuite: A method with this annotation will run once after the execution of all tests in the suite is complete.
  • @BeforeGroups: This method will run before the first test run of that specific group.
  • @AfterGroups: This method will run after all test methods of that group complete their execution.

Now, let’s look at an example to understand how it works.

Working of TestNG Annotations

The example below verifies the title of the Browserstack Home Page. The entire test case has been split into 3 parts.

  1. Launching the browser will be the first step and hence it is included under the @BeforeTest Annotation.
  2. Next is the actual test case which verifies the title, and is therefore included in the @Test annotation.
  3. Finally, the quit browser test case is considered under the @AfterTest annotation.

Code Example for the above scenario

package testngtest;
import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
public class Test1 {
public String baseUrl = "https://www.browserstack.com/";
String driverPath = "D:\\Selenium\\chromedriver.exe";
public WebDriver driver ;
@BeforeTest
public void launchBrowser() {
System.out.println("launching Chrome browser");
System.setProperty("webdriver.chrome.driver", driverPath);
driver = new ChromeDriver();
driver.get(baseUrl);
}
@Test
public void verifyHomepageTitle() {
String expectedTitle = "Most Reliable App & Cross Browser Testing Platform | BrowserStack";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
}
@AfterTest
public void terminateBrowser(){
driver.close();
}
}

On executing the code above, the output looks as follows:

TestNG Annotations output

Run Selenium Test for Free

This is how it works. Now let’s understand how to include @BeforeSuite and @BeforeMethod Annotations.

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class testngAnnotations {
// Test Case 1
@Test
public void test1() {
System.out.println("Test Case 1");
}// Test Case 2
@Test
public void test2() {
System.out.println("Test Case 2");
}
@BeforeMethod
public void beforeMethod() {
System.out.println("Before Method");
}
@AfterMethod
public void afterMethod() {
System.out.println("After Method");
}
@BeforeClass
public void beforeClass() {
System.out.println("Before Class");
}
@AfterClass
public void afterClass() {
System.out.println("After Class");
}
@BeforeTest
public void beforeTest() {
System.out.println("Before Test");
}
@AfterTest
public void afterTest() {
System.out.println("After Test");
}
@BeforeSuite
public void beforeSuite() {
System.out.println("Before Suite");
}
@AfterSuite
public void afterSuite() {
System.out.println("After Suite");
}
}

In the above example, the sequence is changed and then the program is executed. When this program is run, the output will appear in the sequence as shown below.

[RemoteTestNG] detected TestNG version 7.2.0
Before Suite
Before Test
Before Class
Before Method
Test Case 1
After Method
Before Method
Test Case 2
After Method
After Class
After Test
PASSED: test1
PASSED: test2
===============================================
Default test
Tests run: 2, Failures: 0, Skips: 0
===============================================
After Suite
===============================================
Default suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================

So it always starts from executing Suite and then ends by executing method. This is how one uses TestNG Annotations.

TestNG Annotations are used to describe a batch of code inserted into the program or business logic used to control the flow of methods in the test script. They make Selenium test scripts more manageable, sophisticated and effective. Using them is extremely helpful for testers, and makes their lives much easier.

Try Running Selenium Test for Free

BrowserStack Logo Run Selenium Tests on 2000+ Browsers & Devices Get Started Free