0
0
Selenium Javatesting~15 mins

Retry analyzer for failures in Selenium Java - Build an Automation Script

Choose your learning style9 modes available
Retry failed test automatically up to 2 times
Preconditions (3)
Step 1: Create a RetryAnalyzer class implementing IRetryAnalyzer
Step 2: Override retry method to retry test up to 2 times on failure
Step 3: Annotate the test method with @Test and set retryAnalyzer to the created class
Step 4: Run the test suite
Step 5: Observe that if the test fails, it retries automatically up to 2 times
✅ Expected Result: The failed test is retried automatically up to 2 times before final failure is reported
Automation Requirements - TestNG with Selenium WebDriver
Assertions Needed:
Verify the test method is retried on failure
Verify the test finally passes or fails after retries
Best Practices:
Use IRetryAnalyzer interface for retry logic
Keep retry count configurable
Use TestNG annotations properly
Avoid infinite retry loops
Automated Solution
Selenium Java
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import static org.testng.Assert.assertTrue;

public class RetryAnalyzer implements IRetryAnalyzer {
    private int retryCount = 0;
    private static final int maxRetryCount = 2;

    @Override
    public boolean retry(ITestResult result) {
        if (retryCount < maxRetryCount) {
            retryCount++;
            return true;
        }
        return false;
    }
}

public class SampleTest {
    WebDriver driver;

    @Test(retryAnalyzer = RetryAnalyzer.class)
    public void testGoogleSearch() {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
        driver.get("https://www.google.com");

        // Intentionally failing assertion to demonstrate retry
        boolean isSearchBoxPresent = driver.findElements(By.name("q")).size() > 0;
        assertTrue(isSearchBoxPresent, "Search box should be present on Google homepage");

        driver.quit();
    }
}

The RetryAnalyzer class implements IRetryAnalyzer interface from TestNG. It overrides the retry method to retry the test up to 2 times if it fails.

The SampleTest class contains a test method testGoogleSearch annotated with @Test and linked to RetryAnalyzer via retryAnalyzer attribute.

When the test fails, TestNG calls retry method. If retry count is less than max, it returns true to rerun the test. Otherwise, it stops retrying.

This setup helps automatically rerun flaky tests to reduce false negatives.

Common Mistakes - 4 Pitfalls
Not resetting retry count for each test
Retrying tests infinitely without max limit
Using hardcoded driver path inside test
Not quitting WebDriver after test
Bonus Challenge

Now add data-driven testing with 3 different search keywords and retry on failure

Show Hint