TestNG Annotations With Selenium WebDriver Examples

As you know, TestNG is the framework which is very useful to use with selenium WebDriver software testing tool. I have shared all selenium webdriver with testng tutorials on THIS LINK. The main reason behind TestNG's popularity is we can create and configure test case and test suite very easily for software web application using many different annotations of TestNG. Few annotations are similar in TestNG and junit and you can view junit annotations with example on this JUNIT ANNOTATIONS POST.

Annotations are those things in TestNG which guides it for what to do next or which method should be executed next. TestNG has also facility to pass parameters with annotations. Let we look at TestNG annotations list with its functional description.

@Test
@Test annotation describes method as a test method or part of your test.

@BeforeMethod
Any method which is marked with @BeforeMethod annotation will be executed before each and every @test annotated method.

@AfterMethod
Same as @BeforeMethod, If any method is annotated with @AfterMethod annotation then it will be executed after execution of each and every @test annotated method.

@BeforeClass
Method annotated using @BeforeClass will be executed before first @Test method execution. @BeforeClass annotated method will be executed once only per class so don't be confused.
VIEW PRACTICAL EXAMPLE OF @BeforeClass ANNOTATION

@AfterClass
Same as @BeforeClass, Method annotated with @AfterClass annotation will be executed once only per class after execution of all @Test annotated methods of that class.
VIEW PRACTICAL EXAMPLE OF @AfterClass ANNOTATION

@BeforeTest
@BeforeTest annotated method will be executed before the any @Test annotated method of those classes which are inside <test> tag in testng.xml file.
VIEW PRACTICAL EXAMPLE

@AfterTest
@AfterTest annotated method will be executed when all @Test annotated methods completes its execution of those classes which are inside <test> tag in testng.xml file.
VIEW PRACTICAL EXAMPLE

@BeforeSuite
Method marked with @BeforeSuite annotation will run before the all suites from test.

@AfterSuite
@AfterSuite annotated method will start running when execution of all tests executed from current test suite.

@DataProvider
When you use @DataProvider annotation for any method that means you are using that method as a data supplier. Configuration of @DataProvider annotated method must be like it always return Object[][] which we can use in @Test annotated method.
@BeforeGroups
@BeforeGroups annotated method will run before the first test run of that specific group.

@AfterGroups
@AfterGroups annotated method will run after all test methods of that group completes its execution.

@Parameters
When you wants to pass parameters in your test methods, you need to use @Parameters annotation.
VIEW PRACTICAL EXAMPLE OF @Parameters ANNOTATION

@Factory
When you wants to execute specific group of test cases with different values, you need to use @Factory annotation. An array of class objects is returned by @Factory annotated method and those TestNG will those objects as test classes.

@Listeners
@Listeners are used to with test class. It is helpful for logging purpose.

I have used most of all these annotations practically In WebDriver data driven framework for software testing. You can view real usage by creating data driven framework as described on THIS PAGE.

No comments:

Post a Comment