I implemented logic that re runs a failed TestNG test class from the following link:
https://martinholladay.wordpress.com/2013/11/17/webdriver-2-0-automatically-retry-failed-tests/
Unfortunately, it runs the method with the "Test" annotation, and not running the BeforeClass (@BeforeClass) and AfterClass (@AfterClass) methods. I tried looking into setDependsOnMethods and getDependsOnMethods methods of ITestAnnotations to no avail.
Does anybody know how to get the listener class to run BeforeClass and AfterClass methods as well?
public class RetryListener implements IAnnotationTransformer {
public void transform(ITestAnnotation annotation, Class testClass,
Constructor testConstructor, Method testMethod) {
IRetryAnalyzer retry = annotation.getRetryAnalyzer();
if (retry == null) {
annotation.setRetryAnalyzer(Retry.class);
}
}
}
//The test begins here....
@BeforeClass(alwaysRun = true)
@Parameters("Environment")
public void BeforeClass(String sEnv) throws Exception {
WebDriver driver = new FirefoxDriver();
driver.get("www.google.com");
}
@Test
public void TestMethod() {
//Some test...
}
@AfterClass(alwaysRun = true)
public void AfterClass() {
driver.quit();
}
RetryListener
does not run whole test class it just runs the test which failed. So it won't execute any thing except @Test. To run entire test class again you have to use something else for e.g. you can testng-failed.xml from testng-report directory. Write some code to run the testng-failed.xml when you get any failure. But it will always run at end of execution. – Priyanshu Shekhar