0
0
Selenium Javatesting~10 mins

Retry analyzer for failures in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test demonstrates how a retry analyzer works in Selenium with Java. It retries a failed test once before marking it as failed, verifying that the retry logic triggers on failure.

Test Code - TestNG with Selenium WebDriver
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class RetryAnalyzerExample {

    WebDriver driver;

    @BeforeClass
    public void setup() {
        driver = new ChromeDriver();
        driver.get("https://example.com/login");
    }

    @Test(retryAnalyzer = Retry.class)
    public void testLogin() {
        WebElement username = driver.findElement(By.id("username"));
        WebElement password = driver.findElement(By.id("password"));
        WebElement loginButton = driver.findElement(By.id("loginBtn"));

        username.sendKeys("wrongUser");
        password.sendKeys("wrongPass");
        loginButton.click();

        WebElement errorMsg = driver.findElement(By.id("errorMsg"));
        Assert.assertTrue(errorMsg.isDisplayed(), "Error message should be displayed for invalid login");
    }

    @AfterClass
    public void teardown() {
        driver.quit();
    }

    public static class Retry implements IRetryAnalyzer {
        private int retryCount = 0;
        private static final int maxRetryCount = 1;

        @Override
        public boolean retry(ITestResult result) {
            if (retryCount < maxRetryCount) {
                retryCount++;
                return true;
            }
            return false;
        }
    }
}
Execution Trace - 9 Steps
StepActionSystem StateAssertionResult
1Test starts and Chrome browser opensBrowser opened at https://example.com/login page-PASS
2Find username, password fields and login buttonLogin form elements are located on the page-PASS
3Enter invalid username and password, then click loginLogin form submitted with wrong credentials-PASS
4Find error message element on pageError message element locatedCheck if error message is displayedFAIL
5Retry analyzer triggers retry of testLoginTest restarts: browser still open at login page-PASS
6Repeat steps: find elements, enter invalid credentials, click loginLogin form submitted again with wrong credentials-PASS
7Find error message element againError message element locatedCheck if error message is displayedPASS
8Test completes successfully after retryTest marked as pass-PASS
9Browser closesBrowser closed-PASS
Failure Scenario
Failing Condition: Error message element is not found or not displayed after retry
Execution Trace Quiz - 3 Questions
Test your understanding
What does the retry analyzer do when the test fails the first time?
AIt retries the test once before marking it failed
BIt immediately marks the test as failed without retry
CIt retries the test infinitely until it passes
DIt skips the test after failure
Key Result
Using a retry analyzer helps reduce false negatives by rerunning failed tests automatically once, improving test reliability without manual intervention.