TestNG - Annotations
~30 mins
TestNG Annotations
What are Annotations?
- Meta-tags which provides information to the test methods.
Why do we need to use it?
- To maintain a structured, readable , user-friendly testcases
When to use annotation?
- To control the execution order and behaviour of the test methods.
Annotations
Available annotations in Testng are:
- @BeforeSuite
- @BeforeTests
- @BeforeClass
- @BeforeMethod
- @Test
- @AfterMethod
- @AfterClass
- @AfterTest
- @AfterSuite
- @DataProvider
Annotations
-
Annotation Hierarchy
-
@BeforeSuite
- @BeforeTests
- @BeforeClass
- @BeforeMethod
- @Test
- @AfterMethod
- @BeforeMethod
- @AfterClass
- @BeforeClass
- @AfterTest
- @BeforeTests
-
@AfterSuite
@BeforeAll
- @Before->All the @Before annotations will be running before the testcases and are used
to setup the pre-conditions
@AfterAll
- @After→ All the @After annotations will be running after the testcases and are used to
setup the post-conditions
Annotations
- Suite →represents one xml file
@BeforeSuite:
- Configured to run the method once before all the tests in the suite -following which all other
annotation will execute
@AfterSuite:
- Method will run after the all other annotations defined for the testcases.
@BeforeTest:
- Configured to run the method once before each
(test modules) .
@AfterTest:
- Method will run for after every
(test modules) is executed .
@BeforeTest:
- Configured to run the method once before each
(test modules) .This annotation is typically used to
set up resources that will be used by all test methods in the suite.
@AfterTest:
- Method will run for after every
(test modules) is executed .
@BeforeClass:
- Configured to run the method once before all
in the test. This annotation is typically used to set
up resources that will be used by all test methods in the suite.
@AfterClass:
- Configured to run the method after every
in the test. This annotation is typically used to set up
resources that will be used by all test methods in the suite
@BeforeMethod:
- Configured to run the method once before each @Test method.
Example:
@BeforeMethod
public void preCondition() {
EdgeDriver driver = new EdgeDriver();
}
@AfterMethod
- Configured to run the method after each @Test method.
- Example:
@AfterMethod
public void postCondition() {
driver.close();
}
Classroom Activity (Breakout)
- Step:1 Create a class as BaseClass
- Step:2 Move all the common steps to common class (remove those steps in testcases)
- Step:3 @BeforeMethod and @AfterMethod annotation to define the preCondition and
postCondition Methods - Step:4 Extend the base class to leads classe