Playwright + Java · Lesson 36 of 38

0/38 done

Module 36: Retries & Flaky Tests

36. Diagnosing & Eliminating Test Flakiness

16 minlesson

Your task

Implement isRetryableError:

  1. Accept Throwable t.
  2. Return true if t.getMessage() contains "Timeout" or "closed", otherwise false.

Write it in the editor, then press Check answer (Ctrl + Enter).

Root Causes of Flakiness

  1. Shared State: Test B depends on data created by Test A. Always use isolated contexts.
  2. Fragile Locators: Non-deterministic IDs (#btn_4829_x). Always use getByRole or getByLabel.
  3. Hardcoded Waits: Thread.sleep(3000) will fail when CI servers experience CPU spikes.
  4. Network Jitter: Use page.waitForResponse() rather than waiting for DOM side effects.
java
// Diagnostic retry execution:
public static void retryOnFailure(Runnable testAction, int maxAttempts) {
    int attempts = 0;
    while (attempts < maxAttempts) {
        try {
            testAction.run();
            return;
        } catch (RuntimeException e) {
            attempts++;
            if (attempts >= maxAttempts) throw e;
            System.out.println("Test attempt " + attempts + " failed. Retrying...");
        }
    }
}

Key Takeaways & Walkthrough Summary

Flaky tests erode team confidence in automation. In this lesson, we break down the root causes of flakiness: test order dependency, state leaks, fragile locators, and unhandled network latency. You will learn strategic retry mechanisms and isolation techniques. Let's make tests rock-solid!

Part of the Playwright + Java course (Playwright with Java). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Is relying on test retries bad practice?

Retries are a safety net for transient network blips, but flaky tests must still be investigated and refactored using Playwright Trace Viewer.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Java
Skip