TestNG Annotations in Selenium Webdriver with Examples

TestNG is a testing framework widely used in Selenium WebDriver for automation testing. It provides a wide range of annotations that help in organizing and controlling the flow of test cases. TestNG learns from JUnit and NUnit, making itself better by adding new features that make testing easier and more effective. One of these improvements is the use of annotations.

Table of Content

  • What are TestNG Annotations?
  • Types and Hierarchy of TestNG Annotations
  • Working on TestNG Annotations
  • Conclusion
  • FAQs on TestNG Annotations in Selenium Webdriver

What are TestNG Annotations?

The Concept Annotations is introduced in Java 1.5 (jdk5). TestNG annotations are special codes we add to our test programs to decide the order in which our test methods run. These annotations give extra information about our methods or classes and start with the ‘@’ symbol. They’re like special symbols that we put before each method in our test code. If a method doesn’t have these annotations, it won’t be run when we execute our tests. TestNG uses these tags to create a strong and organized testing framework.

Types and Hierarchy of TestNG Annotations

These are the types of TestNG Annotations

Let’s Elaborate each one by one:

  1. BeforeSuite: @BeforeSuite is one of the TestNG Annotations. As the name defines, @BeforeSuite is executed before the execution of all the test cases inside a TestNG Suite.
  2. AfterSuite: @AfterSuite is one of the TestNG Annotations. As the name defines, @AfterSuite is executed after the execution of all the test cases inside a TestNG Suite.
  3. BeforeTest: @BeforeTest is one of the TestNG Annotations. As the name defines, @BeforeTest is executed before the execution of all the @test annotated methods inside a TestNG Suite
  4. AfterTest: @AfterTest is one of the TestNG Annotations. As the name defines, @AfterTest is executed after the execution of all the @test annotated methods inside a TestNG Suite.
  5. BeforeClass: @BeforeClass is one of the TestNG Annotations. As the name defines, @BeforeClass is executed before all the methods of the current class start their execution.
  6. AfterClass: @AfterClass is one of the TestNG Annotations. As the name defines, @AfterClass is executed after all the methods of the current class finish their execution.
  7. BeforeMethod: @BeforeMethod is one of the TestNG Annotations. As the name itself defines, @BeforeMethod is executed before each test method within a test class. Suppose there are n test methods within a test class, then n times @BeforeMethod annotated method will be invoked.
  8. AfterMethod: @AfterMethod is one of the TestNG Annotations. As the name defines, @AfterMethod is executed after each test method within a test class. Suppose there are n test methods within a test class, then n times @AfterMethod annotated method will be invoked.
  9. BeforeGroups: @BeforeGroups is one of the TestNG Annotations. When you annotate a method with @BeforeGroups, TestNG ensures that this method is invoked before any test method belonging to the specified groups is executed.
  10. AfterGroup: @AfterGroups is one of the TestNG Annotations. As the name defines, @AfterGroups should be executed after all the test methods belonging to a specified group have been run.

Working on TestNG Annotations

Step 1: Open the Eclipse IDE.

Step 2: Create a Maven Project.

Step 3: After Creating the Maven Project, the project exploration will look like the below image.


Step 4: Create a TestNG Class that contains all TestNG Annotations.

The below code gives you a glimpse of the Working of TestNG Annotations.

TestNGAnnotations. Java

Java
package com.w3wiki.test;

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;

public class TestNGAnnotations {
  @Test
  public void test1() {
      System.out.println("Test Case 1");
  }
  @Test
  public void test2() {
      System.out.println("Test Case 2");
  }
  @BeforeMethod
  public void beforeMethod() {
      System.out.println("This will Execute Before Method");
  }

  @AfterMethod
  public void afterMethod() {
      System.out.println("This will Execute After Method");
  }

  @BeforeClass
  public void beforeClass() {
      System.out.println("This will Execute Before Class Execution");
  }

  @AfterClass
  public void afterClass() {
      System.out.println("This will Execute After Class Execution");
  }

  @BeforeTest
  public void beforeTest() {
      System.out.println("This will Execute Before Test ");
  }

  @AfterTest
  public void afterTest() {
      System.out.println("This will Execute After Test ");
  }

  @BeforeSuite
  public void beforeSuite() {
      System.out.println("This will Execute Before Suite ");
  }

  @AfterSuite
  public void afterSuite() {
      System.out.println("This will Execute After Suite ");
  }

}


Now, let’s explain what this code does:

  • Package Declaration
    • The code is in the com.w3wiki.test package.
  • Imports:
    • The code imports all annotations and classes from the TestNG framework like org.testng.annotations.AfterMethod and org.testng.annotations.Test etc.
  • TestNG Annotations Class : Inside the TestNG Annotations class there are various methods
    • @Test: This annotation marks a method as a test method. In this code, test1() and test2() are two test methods. They print out messages indicating which test case is being executed.
    • @BeforeMethod: This annotation denotes a method that should be run before each test method. Here, beforeMethod() is executed before each test method and prints a message.
    • @AfterMethod: This annotation denotes a method that should be run after each test method. Here, afterMethod() is executed after each test method and prints a message.
    • @BeforeClass: This annotation denotes a method that should be run before any test method belonging to the classes inside the <test> tag is run. Here, beforeClass() is executed before any test method prints a message.
    • @AfterClass: This annotation denotes a method that should be run after all the test methods in the current class have been run. Here, afterClass() is executed after all the test methods and prints a message.
    • @BeforeTest: This annotation denotes a method that should be run before any test method belonging to the <test> tag is run. Here, beforeTest() is executed before any test method and prints a message.
    • @AfterTest: This annotation denotes a method that should be run after all the test methods belonging to the <test> tag have run. Here, afterTest() is executed after all the test methods and prints a message.
    • @BeforeSuite: This annotation denotes a method that should be run before all tests in a suite have run. Here, beforeSuite() is executed before any test method prints a message.
    • @AfterSuite: This annotation denotes a method that should be run after all tests in a suite have run. Here, afterSuite() is executed after all the test methods and prints a message.

Step 5: Now, we create the AnnotationsTest.xml file to configure the TestNG Annotations class.

Annotations.XML

XML
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="suite">
    <test name="test1">
        <classes>
               <class name="com.w3wiki.test.TestNGAnnotations" /> 
                
        </classes>
    </test>
</suite>

Step 6: Run the After_Method file. Right-click on the After_Method, move the cursor down to Run As and then click on the 1 TestNG Suite.

Output:

Output of TestNG Annotations in Selenium

Conclusion

In conclusion, TestNG annotations are vital for organizing and controlling the flow of test cases in Selenium WebDriver automation. They provide a structured approach to executing tests, allowing for setup, teardown, and configuration at various levels such as suite, test, class, and method. This helps in creating robust and efficient test suites, enhancing the overall testing process.

FAQs on TestNG Annotations in Selenium Webdriver

Why do we use annotations in TestNG?

Answer:

TestNG are used for the control the next method of the test scripts, TestNG annotation are used before every method in the test script.

In which order will annotations work in TestNG?

Answer:

How can you group test methods using TestNG annotations?

Answer:

There are 10 types of TestNG Annotations



Contact Us