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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <suite> <test> <classes> <method> <test> </method> </classes> </test> </suite> |
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:
@BeforeSuite:
All tests of this suit will be annotated only once before running.
@AfterSuite:
After running all the tests of this suit, the only way to annotated will be run only once.
@BeforeClass:
The first test method in the current class will be annotated only once before applying.
@AfterClass:
The annotated method will be run only once after all the testing methods of the existing class are run.
@BeforeTest:
The annotated method will be run before any test method related to the classes to be played.
@AfterTest:
Annotated methods will be run after all test methods that are going to be inside the class in the <Test> tag.
@BeforeGroups:
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.
@AfterGroups:
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.
@BeforeMethod:
The method annotated before each test method will be run.
@AfterMethod:
After each test method the annotated method will be run.
Example of TestNG Annotations in Selenium:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | package testng; 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 Aanotations { // test case 1 @Test public void testCase1() { System.out.println("@test1"); } // test case 2 @Test public void testCase2() { System.out.println("@test2"); } @BeforeMethod public void beforeMethod() { System.out.println("@beforeMethod"); } @AfterMethod public void afterMethod() { System.out.println("@afterMethod"); } @BeforeClass public void beforeClass() { System.out.println("@beforeClass"); } @AfterClass public void afterClass() { System.out.println("@afterClass"); } @BeforeTest public void beforeTest() { System.out.println("@beforeTest"); } @AfterTest public void afterTest() { System.out.println("@afterTest"); } @BeforeSuite public void beforeSuite() { System.out.println("@beforeSuite"); } @AfterSuite public void afterSuite() { System.out.println("@afterSuite"); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | @beforeSuite @beforeTest @beforeClass @beforeMethod @test1 @afterMethod @beforeMethod @test2 @afterMethod @afterClass @afterTest |
@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.
1 | Eg: @Test(alwaysRun = true) |
dataProvider: TestNG dataProvider is used to provide data for parameterization.
1 | Eg. @Test(dataProvider = “Hello”). |
description: It is the description for the method to write Extra stuff.
1 | Eg: @Test(description = “test method”) |
invocationCount: It refers to several times to implement a method. It will work as a loop.
1 | Eg: @Test(invocationCount = 10) . Hence, this method will execute 10 times. |
invocationTimeOut: This refers to the maximum number of milliseconds, one method should be taken for all invocationCount .
1 | Eg: @Test(invocationCount =7,invocationTimeOut = 30 ) |
priority: This order determines the priority of the trial method. Less priorities will be determined first.
1 | Eg: @Test(priority =1 ) |
Learn How to Use TestNG Annotations in Selenium and TestNG Tutorial: Annotations, Framework, Examples in Selenium and TestNG Annotations And Benefits