Testing With Selenium WebDriver (70 Blogs) Become a Certified Professional
AWS Global Infrastructure

Software Testing

Topics Covered
  • Testing With Selenium WebDriver (60 Blogs)

All You Need to Know About TestNG Annotations in Selenium

Last updated on Jul 01,2021 6.9K Views
A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop. A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop.
9 / 14 Blog from Selenium Webdriver

Ever since the concept of Automation Testing has been introduced, a lot of testing methodologies have evolved, paradigms have changed but the requirements somehow triumphed to remain constant. One such methodology is TestNG which helps us to group the test cases into different sections of code with the help of Annotations. But how does it work? Let’s understand this concept through the medium of this article on TestNG Annotations in Selenium. For further understanding, check out the Selenium Course.

I will be covering below-mentioned topics in this article:

Introduction to TestNG

TestNG stands for Test Next Generation and it is an open-source test automation framework inspired by JUnit and NUnit. Well, TestNG is not just inspired but, it is an upgraded version of these two frameworks. So what is the upgrade here? The upgrade with TestNG is that it provides additional functionality like test annotations, grouping, prioritization, parameterization and sequencing techniques in the code which was not possible earlier.

Introduction to TestNG - TestNG Annotations - EdurekaIt not only manages test cases, but even detailed reports of tests can also be obtained by using TestNG. There will be a detailed summary which will display the number of test cases that have failed. Also, the bugs can be located accurately and fixed at the earliest. Now that you know what is TestNG, let’s see why to use TestNG in Selenium.

Why use TestNG in Selenium?

Software developers from around the world will unanimously agree that writing code in test cases saves a good part of their debugging time. Why? That is because test cases help in creating robust and error-free code by breaking the entire code into smaller test cases, and then by evaluating each of these test cases to pass/ fail conditions, you can create error-free code. Since Selenium does not support execution of code in test cases, TestNG comes into the picture which will help in the execution of the test cases.
Why use TestNG in Selenium - TestNG Annotations - Edureka

TestNG also supports the following features:

  • It generates the report in a proper format that includes a number of executed test cases, the number of failed test cases, and test cases that have been skipped.
  • Multiple test cases can be grouped more easily by converting them into TestNG.xml file. Here, you can set the priorities to execute the test cases.
  • Using TestNG, you can execute multiple test cases on multiple browsers, i.e., cross-browser testing.
  • The testing framework can be easily integrated with tools like Maven, Jenkins, etc.

Now that you know what is TestNG and why it is used, let’s move further and know the various Annotations that TestNG support in Selenium.

TestNG Annotations

TestNG Annotations in Selenium are used to control the next method to be executed. Test annotations are defined before every method in the test code. In case any method is not prefixed with annotations, then that method will be ignored and it will not be executed as a 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.

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

So this is all about Annotations in TestNG. Now let’s move further and understand how to write a first test case using TestNG.

Creating Test Cases using TestNG Annotations

The various steps involved in creating test cases using TestNG Annotation are as follows:

  1. Create a project and add the TestNG Library.
  2. Create a class file and code the program
  3. Finally, write XML file and execute it on TestNG Suite.

If you wish to get into more details about writing a test case using TestNG, then kindly check this article on TestNG Tutorial. Now, let’s move further and understand how TestNG annotations will help you to group the test cases and configure your program.

Test Case 1:

In this test case, I will use three different Annotations and code the program. Let’s understand how to do that with the below example.

package co.edureka.pages;

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;
import org.testng.annotations.Test;

public class AnnotationExample {
public String baseUrl = "https://www.edureka.co/";
String driverPath = "C://Users//Neha_Vaidya//Desktop//chromedriver_win32//chromedriver.exe";
public WebDriver driver ;

public void launchBrowser() {
System.out.println("launching Chrome browser");
System.setProperty("webdriver.chrome.driver", driverPath);
driver = new ChromeDriver();
public void verifyHomepageTitle() {
String expectedTitle = "Instructor-Led Online Training with 24X7 Lifetime Support | Edureka";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
public void terminateBrowser(){

Basically, I want to check whether the actual title of the Edureka webpage matches with the expected title or not. So, I am first using ‘@BeforeTest’ Annotation and creating an instance of browser driver and navigating through Edureka website using the driver.get() method. So these are the steps that should be performed before the test.

Next, during this test, I want to check whether the expected title and actual title matches or not. That’s why I am specifying all the steps with @Test Annotation. Finally, I want to close the driver and terminate the browser after the test. That’s why I am using @AfterTest Annotation and closing the driver. So this is how I am grouping the entire code into various annotations and executing the test case. Let’s run the program as TestNG Test and check the output.

Output of TestCase 1 - TestNG Annotations - Edureka

You can see in the snapshot that the test case run successfully on a default test and suite. Also, the test case passed and there is no failure as well.

Now let’s see one more example to understand the execution flow of various annotations in depth. It is very necessary to know the flow of execution of the annotations. So, let’s take a look at the snapshot below to know about it.

Annotations Execution flow - TestNG Annotations - Edureka

The flow of execution of annotations will be as shown in the above snapshot. Now let’s see one more example to understand the same.

Test Case 2:

package co.edureka.pages;

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
public void testCase1() {
System.out.println("Test Case 1");

// Test Case 2
public void testCase2() {
System.out.println("Test Case 2");

public void beforeMethod() {
System.out.println("Before Method");

public void afterMethod() {
System.out.println("After Method");

public void beforeClass() {
System.out.println("Before Class");

public void afterClass() {
System.out.println("After Class");

public void beforeTest() {
System.out.println("Before Test");

public void afterTest() {
System.out.println("After Test");

public void beforeSuite() {
System.out.println("Before Suite");

public void afterSuite() {
System.out.println("After Suite");


In the above code, I am randomly writing all the methods with Annotations. I am not following the sequence of it. But, when I execute the program, it will follow the same order. Let’s now check the output.

[RemoteTestNG] detected TestNG version 6.14.2
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: testCase1
PASSED: testCase2

Default test
Tests run: 2, Failures: 0, Skips: 0

After Suite

Default suite
Total tests run: 2, Failures: 0, Skips: 0

From the above output, you can notice that it executed both the test cases and Before Suite and After Suite executed only once. Also, the test ran successfully on the default test and suite. This is how you need to execute the test cases using Annotations. So that brings us to the end of this article on TestNG Annotations in Selenium.

I hope you understood the concepts and it added value to your knowledge. Now, if you want to get more insights into Selenium, you can check out the article on Selenium Tutorial.

If you found this “TestNG Annotations in Selenium” relevant, check out the Selenium Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. 


Join the discussion

Browse Categories

Send OTP
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.