Retry Failed Test Case in Playwright Java

Profile picture for user arilio666

This article will show how we can retry failed tests in playwright java. The test retry method cant is used as we use in node.js.

It's part of the Playwright Test runner rather than the Playwright Library. So we have to look for generic test retry solutions depending on the test runner, like TestNG.

1.) IRetryAnalyzer

This is an interface present to retry the failed test.

public interface IRetryAnalyzer {

/**
 * Returns true if the test method has to be retried, false otherwise.
 *
 * @param result The result of the test method that just ran.
 * @return true if the test method has to be retried, false otherwise.
 */
boolean retry(ITestResult result);
}

The method returns true if we want to retry failed test and returns false if we don't want to.
When blended with the test TestNG automatically invokes a retry analyzer to see if the test needs to retry in case it fails.

2.) Implementation

Let us create a class retry and implement IRetryAnalyzer.

package week4.day2;

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

public class Retry implements IRetryAnalyzer {

   int retryCount = 0;
   int maxRetryCount = 4;

   @Override
   public boolean retry(ITestResult result) {

       if (!result.isSuccess()) {

           if (retryCount < maxRetryCount) {
               System.out.println(
                       "Retrying Test : Re-running " + result.getName() + " for " + (retryCount + 1) + " time(s).");

               retryCount++; // Increase the maxRetryCount by 1

               result.setStatus(ITestResult.FAILURE);
               return true;
           } else {
               result.setStatus(ITestResult.FAILURE);
           }
       } else {
           result.setStatus(ITestResult.SUCCESS);

       }

       return false;
   }
}
  1. First, we check if the test failed with the if statement.
  2. We initiated the maxRetryCount.
  3. Then we check if the max number of test execution has been reached.
  4. We then print the number of retry attempts.
  5. We then increase the maxRetryCount by 1.
  6. Then mark the test as failed and rerun the failed test.
  7. TestNg marks the last test run as failed if the previous run is the max retry.
  8. In the else block, TestNG marks the test as passed when the test passes.
  9. So here, the test will run four times and then fails it.

3.) Example

To use the IRetryAnalyzer in our Test annotation, follow this.

@Test(retryAnalyzer = Retry.class)
public void loginPage() {

}

Below we have an example of a Test Retry scenario.

package week4.day2;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;

public class PopUp {
    Browser browser;
    Page page;

    @BeforeTest
    private void setUp() throws InterruptedException {
        try (Playwright playwright = Playwright.create()) {
            browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
            page = browser.newPage();
            page.navigate("https://www.programsbuzz.com");
        }
    }

    @AfterTest
    private void tearDown() {
        browser.close();
    }

    @Test(retryAnalyzer = Retry.class)
    public void popUp() throws Throwable {

        page.locator("//i[@class='fas fa-search']").click();
        page.locator("//input[@id='edit-keys']").type("Playwright");
        page.locator("//input[@id='edit-submit']").click();
        page.goBackk();
        page.goForward();

    }

}
  • We deliberately failed the test and can see that we achieved test retry here.
  • The method popUp has run four times as our maxRetryCount and achieved failure in the end.
  • So this is how we can retry failed tests in playwright java using TestNG.