TestNG Annotations in Selenium:


  • An annotation is a tag or metadata that provides additional information about class, interface, or method in TestNG.
  • TestNG are the lines of annotation code that are put into the program / business logic, to control how to run the methods given below.
  • TestNG also has the facility to pass parameters with annotation.


Advantages Of TestNG Annotations in Selenium:

  • They are easy to understand.
  • You can group test cases using appropriate annotations.
  • You can do parallel test.
  • You can pass extra parameters for annotation.
  • They are strongly typed, so the compiler will completely catch any errors.


Annotations Hierarchy in TestNG:


1) Basic Annotations:

Annotations that are used most often on a daily basis in TestNG.

@Test, @BeforeClass, @AfterClass, @BeforeMethod, @AfterMethod.


2) Advanced Annotations:

Annotations used on special Situation / setup / configuration

Group Annotations : @BeforeGroups, @AfterGroups

Suite related Annotations : @BeforeSuite, @AfterSuite, @BeforeTest, @AfterTest


Annotation with Description:

All tests of this suit will be annotated only once before running.


After running all the tests of this suit, the only way to annotated will be run only once.


The first test method in the current class will be annotated only once before applying.


The annotated method will be run only once after all the testing methods of the existing class are run.


The annotated method will be run before any test method related to the classes to be played.


Annotated methods will be run after all test methods that are going to be inside the class in the <Test> tag.


List of groups that will run before this configuration method. This method is guaranteed to be run shortly before the first test method under any of these groups.


List of groups, which will run after this configuration method. This method is guaranteed immediately after using the final examination method under any of these systems.


The method annotated before each test method will be run.


After each test method the annotated method will be run.


Example of TestNG Annotations in Selenium:




@Test attributes that we can pass to our Test method:

  • @Test TestNG is the most important and commonly used annotation. It is used to mark a method as a test. Therefore, on any method on which we are seeing @Test annotation, it is considered as TestNG test.


alwaysRun: It is used when we want to make sure that any method always runs.


dataProvider: TestNG dataProvider is used to provide data for parameterization.


description: It is the description for the method to write Extra stuff.


invocationCount: It refers to several times to implement a method. It will work as a loop.


invocationTimeOut: This refers to the maximum number of milliseconds, one method should be taken for all invocationCount .


priority: This order determines the priority of the trial method. Less priorities will be determined first.

Learn How to Use TestNG Annotations in Selenium and TestNG Tutorial: Annotations, Framework, Examples in Selenium and TestNG Annotations And Benefits