Monday, February 19, 2024

How to re execute failed Selenium / Playwright Java testcases with the help of TestNG

  1. Create a new class with any name at the test project.
  2. With this class we are going to implement the IRetryAnalyzer class which is available with TestNG
  3. Here is the sample working implementation.
import com.novatti.common.Constants;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;

public class RetryAnalyzer implements IRetryAnalyzer {

private static int maxTry = 3;
private int count = 0;

@Override
public boolean retry(ITestResult iTestResult) {
System.out.println("------------------------ Retrying test: " + iTestResult.getName());
if (!Constants.IS_RETRY) maxTry = 0;

if (!iTestResult.isSuccess()) {
if (count < maxTry) {
count++;
iTestResult.setStatus(ITestResult.FAILURE);
return true;
} else {
iTestResult.setStatus(ITestResult.FAILURE);
}
} else {
iTestResult.setStatus(ITestResult.SUCCESS);
}
return false;
}
}

4. Then create another class to make all our rest methods and test classes to transform to above class at the run time.
Or we can manually update each test method which is not the best approach.

5. From the new class we are going to implement IAnnotationTransformer  interface which is inbuilt with TestNG.

6. Here is the sample working implementation.

import com.novatti.utils.RetryAnalyzer;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;

public class TestNGRetryListener implements IAnnotationTransformer {
@Override
public void transform(
ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
annotation.setRetryAnalyzer(RetryAnalyzer.class);
}
}

7. Now add the above class as a listner to your testNG.xml file.
8. Here is the sample working implementation.

DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="UI Test Suite">
<listeners>
<listener class-name="com.XXXXXX.listeners.TestNGRetryListener"/>
</listeners>
<test verbose="1" preserve-order="false" name="UI Tests">
<classes>
<class name="com.xxx.tests.XXXXX"/>
<class name="com.xx.tests.XXXXXXXX"/>
<class name="com.xx.tests.XXXXXX"/>

</classes>
</test>
</suite>


9. Now everytime when you tigger the testNG.xml file it will listen to the TestNGRetryListener   
class and pick the failed testcases.

10. Then it will retry for the count you have set at the maxTry variable.
private static int maxTry = 3;

I'm with Test NG version 7.9.0