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.
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.
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; } } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser opened at https://example.com/login page | - | PASS |
| 2 | Find username, password fields and login button | Login form elements are located on the page | - | PASS |
| 3 | Enter invalid username and password, then click login | Login form submitted with wrong credentials | - | PASS |
| 4 | Find error message element on page | Error message element located | Check if error message is displayed | FAIL |
| 5 | Retry analyzer triggers retry of testLogin | Test restarts: browser still open at login page | - | PASS |
| 6 | Repeat steps: find elements, enter invalid credentials, click login | Login form submitted again with wrong credentials | - | PASS |
| 7 | Find error message element again | Error message element located | Check if error message is displayed | PASS |
| 8 | Test completes successfully after retry | Test marked as pass | - | PASS |
| 9 | Browser closes | Browser closed | - | PASS |