Annotation is repeatedly used by developers to describe a piece of code that is inserted into the program or business logic used to control the flow of methods in the programming language Java. Annotations play a major role in Test Next Generation (TestNG), an automation framework widely used by Selenium. Testers need to understand the workings and uses of each annotation while they are working on TestNG. 

What is Selenium Webdriver?

It is a group of open source APIs used for automating web application testing. The new feature of Selenium Webdriver is the inclusion of the Webdriver API. Selenium Webdriver was developed for the better support on the dynamic web pages.

Selenium 1.0 + Webdriver = Selenium 2.0

The main advantage of Selenium Webdriver is that it’s simple to use, and the programming interface is easy to understand with basic knowledge of programming languages and improved support on web-app testing problems.

Before You Begin Writing Test With TestNG:

  1. Install the TestNG in Eclipse
  2. Java Development Kit (JDK)
  3. Add TestNG Maven dependency for your pom.xml
  4. Start writing test scenario with the help of TestNG annotations (Note: annotations can be used in Java version 1.5 or higher)
  5. Convert your test code into the testng.xml file
  6. Compile and run your test.

TestNG Annotations, Explanations, and Examples

Annotations differ from project to project depending on their requirements. Though the requirements change, the execution flow will be the same for every single project. The following are TestNG annotations, with explanations and examples for each below. 

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

The Workflow of TestNG is Shown Below:

In this TestNG workflow example, @Test is the base annotation in this TestNG workflow. It is continuous with @Method, which executes before and after execution of @Test. Now, @Class will execute before and after the execution of @Method, and so on.

Now, let’s see each one from the list of annotations:

1) @Test

In any automation script, @Test annotation is the important part, where we write code/business logic. If something needs to be automated, that particular code needs to be inserted into the test method. The test method then executes @Test by passing attributes. Here are some attributes that are used to pass in the test methods.

  • dependsOnGroups: In this attribute, we can get a group of the list to the particular method it depends on. 
    Example: @Test (groups = { "Organization" ,"Employee" })
     
  • alwaysRun: This attribute can be used whenever we get a situation to run a method continuously, even if the parameters of the process fail. 
    Example: @Test(alwaysRun = true)
     
  • dataProviderClass: dataProviderClass is class used to provide the data to the dataProvider, so let’s give the class name “Computer.”
     
  • dataProvider: It is used for providing any data to the parameterization.
    Example: @Test (dataProvider = "Computer")
     
  • dependsOnMethods: Methods are used to execute its dependent method in the same way dependsOnMethods works.
    Example: @Test (dependsOnMethods = { "start", "init" })

2) @BeforeMethod and @AfterMethod

@BeforeMethod allows the method to execute before the execution of each @Test methods, whereas @afterMethod is executed after the execution of each @Test methods. 

Code:

Output:

Each time @BeforeMethod executes for the @Test method, the output for the above code follow. Next, accountLogin() method will be executed before the execution of updateProfile() and then accountLogout() method will be the last step. Again, the accountLogin() method will be executed before execution of bankBalance() and then ends with the accountLogout() method.

First @Test Method: 

Second @Test Method:

3) @BeforeClass and @AfterClass

The method annotated with @BeforeClass will execute only once before the first test method in that particular class is invoked. You can then initialize or configure set-up for all the conventional test methods. The @AfterClass annotation will be executed only once, after all the test methods of that particular class have been invoked. In the below output, you can see that @BeforeClass and @AfterClass are executed at the very beginning and very end, so we can conclude that they are both used only once.

Code:

Output:

When we run the above code, the output will be accountLogin() method. It is executed before executing updateProfile() and void bankBlance() method. During the final step, accountLogout () method is executed.

4) @BeforeTest and @AfterTest

The @BeforeTest methods run after @beforeSuite. For frameworks like smoke testing, @BeforeTest is used for creating an initial data set up and prior to running other test methods, whereas @AfterTest annotation will run after the other tests are complete.

Code:

5) @BeforeSuite and @AfterSuite

With the help of @BeforeSuite annotation, we can set up and start the Selenium Webdrivers. The @AfterSuite annotation is used to stop the Selenium Webdrivers.

Code:


When you are working with annotation, it’s essential to check whether your system is installed with Java version 1.5, because annotation won’t work properly otherwise. In case you are working with annotation on older versions of Eclipse, you’ll likely receive an error stating that annotations are not supported on your system.

In this tutorial, we’ve discussed some of the important annotations and few attributes that software testers use often. Some other annotations that are also used include @AfterGroups and @BeforeGroups. However, they are significant when you are working with groups in your project. From the above annotations, pick the right one for your project and use them as you best see fit.

The above examples are just the basics. Selenium Webdriver can be complex and robust. For a deeper dive into programming with Selenium, consider our Selenium Training Course, which covers Selenium 3.0, Selenium Grid, Java essential skills, Maven, and much more. Or if you’re looking to master a wide range of languages in addition to Selenium, our Automation Test Engineer Masters Program includes Core Java, Docker, and other tools, as well as a full understanding of DevOps test automation practices. Both courses provide hands-on projects and practice labs that give learners a stress-free environment to practice as frequently as needed.