Basically we need TestNG because WebDriver can not generate reports but TestNG generates reports based on the test results of Selenium. Not only this but there are many advantages of TestNG like :
– It makes parallel testing possible.
– Grouping of test cases are really. You can customize and run your test cases the way you want.
– TestNG are very easy to understand.
In short, you can say that TestNG guides you what to do next i.e; which method you should execute next. Here we have TestNG annotation list :
It describes a class or method as part of the test.
The methods which are marked with @BeforeMethod annotation will come in execution before each and every @Test annotated method.
The methods which are marked with @AfterMethod annotation will come in execution after each and every @Test annotated method.
The methods marked under @BeforeClass annotation will be executed only once before first @Test annotation of the current class.
The methods in the @AfterClass annotations will run only once when the execution of all the test methods of the current class is done.
The @BeforeTest annotated methods are executed before any @Test annotated methods are executed of all those classes which comes inside <test> tag in testng.xml file.
The @AfterTest annotated methods are executed after all @Test annotated methods are executed of all those classes which comes inside <test> tag in testng.xml file.
All the methods marked under @BeforeSuite annotation will be executed before all suites are run from the test.
The methods of @AfterSuite annotation will start running once the execution of all the tests are done from the current test Suite.
When we want to use a method as data supplier, we write it under @DataProvider annotation.
The methods of @BeforeGroups annotation will run before the first test run of that particular test group.
The methods of @AfterGroups annotation will run after all the test run of that particular test group.
For passing parameters in your test method, you need to define @Parameters Annotation.
When there is a need to execute specific group of test cases by passing different values, then we use @Factory annotation. The method under it must return Object.
This annotation defines listeners on a test class. It is very helpful for logging purpose.