04
Sep

re-run failed test case with Selenium TestNG framework

In order to re-run failed test cases, TestNG provides “IRetryAnalyzer” interface, which needs to be implemented for “retry()”

Following code snippet demonstrates how to implement this interface and re-run your TestNG Scenarios

Step 1. In your library file, create an implementation of “IRetryAnalyzer” as follow:

import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;

public class RetryAnalyzer implements IRetryAnalyzer {

int start = 1;
int maxRetr = 2;

@Override
public boolean retry(ITestResult result) {
System.out.println(“Calling function for nth time=> “+start);
if (start < maxRetr) { start++; return true; // re-run only if start counter has not reached maxRetry limits } return false; } } Step 2. Write your Selenium tests with retryAnalyzer = class implemented above eg. import org.testng.Assert; import org.testng.annotations.Test; public class RerunAttemp { @Test(retryAnalyzer = package.RetryAnalyzer.class) public void Test1() { Assert.assertEquals(true, "true"); } }