Overcoming Flakiness In Automated Tests With Playwright

Dealing with a broken build due to a test failure is never enjoyable, but flaky tests are especially troublesome. They’re challenging to fix and seem almost impossible to avoid.

Flaky tests are essentially unreliable. They might pass one time you run them but then fail the next time without any obvious explanation. These tests make automation testing incredibly frustrating because they introduce unpredictable failures that are hard to diagnose and rectify.

In this article, we’ll explore what flaky tests are, why they happen, and, most importantly, how to stop them from occurring when using Playwright for automation testing.

What are Flaky Tests?

A flaky test is an automated test that behaves inconsistently in software testing. It gives different results each time it’s run on the same functionality, showing unpredictability. These tests can pass or fail randomly, even if there are no changes in the code or the application being tested.

Flaky tests can show their unreliability in a few ways:

  • Random Flakiness: This is likely the flaky test you’re most familiar with. You run a test, and it passes. Run it again immediately after with no code changes, and boom – it fails. Or vice versa. The test results appear random and unpredictable.
  • Environmental Flakiness: With this type, a test works fine on your local development machine but fails when run on another developer’s computer or your continuous integration server. Or the opposite – it runs on CI but crashes locally. Environmental differences are the root cause.
  • Branch Flakiness: You’ve got a shiny new feature working perfectly in your branch, with all tests passing. But once that code gets merged into the main, one or more tests suddenly start failing. This is classic branch flakiness.

In Playwright, a test gets tagged as “flaky” when it fails the first time but passes when you try it again. Playwright gives you tools and methods to find and deal with these flaky tests, like retrying tests, running them simultaneously, and providing detailed reports.

How to Detect Flaky Tests

Flaky tests produce inconsistent results across multiple test runs, and detecting them is crucial for ensuring reliable automated testing. Here are some strategies and tools for identifying flaky tests:

Statistical Analysis:

  • Analyze historical test results.
  • Identify failure patterns.
  • Calculate the probability of flakiness.

Test Reruns and Variability:

  • Run tests multiple times.
  • Analyze result variations.
  • Inconsistent outcomes across runs suggest potential flakiness.

Custom Test Annotations:

  • Use annotations or markers to flag and track flaky behavior.
  • Designed specifically for flaky tests.
  • Easy tracking and monitoring over time.

Continuous Integration Tools:

  • Leverage tools with built-in flaky test detection.
  • Automatic analysis of inconsistent test behavior.
  • Integration with version control and reporting.

Active Monitoring and Reporting:

  • Continuous monitoring of test runs.
  • Comprehensive reports on test suite stability.
  • Detect flaky tests proactively through metrics like failure rates.

Reasons Why a Test Can Be Flaky

Here are some common reasons why a test may become flaky:

  1. Race conditions: When multiple operations or events occur concurrently, they can trigger dynamic changes on the web page, leading to unexpected behavior and test failures. This is known as a race condition, where the timing and order of events can affect the test outcome.
  2. External Dependencies: Sometimes tests rely on external services or systems, like APIs, databases, or third-party services. When these external things have issues or go down temporarily, it can cause the tests to fail unexpectedly, even though the actual code being tested is working fine.
  3. Asynchronous Operations: Asynchronous operations refer to tasks in the background while other things are happening. For example, when a webpage loads data without refreshing or when animations play, these happen separately from the main tasks. In the testing context, the tests might not work reliably if the test scripts don’t pause and wait for these background tasks to finish before moving on to the next steps.
  4. Test order dependencies: Tests should be self-contained and able to run in any order. However, sometimes, tests rely on shared resources like files, databases, or memory in a specific state. If another test modifies that state unexpectedly, it can unpredictably cause later tests to fail.
  5. Concurrency issues: Multi-threaded applications can exhibit flakiness if tests make incorrect assumptions about the order in which different threads execute. Since threads can be scheduled in different orders on different runs, a test that only accepts one specific order of operations will sometimes pass and sometimes fail.
  6. Environmental Factors: The testing environment itself can also contribute to flaky tests. These factors include internet interruptions, insufficient computer resources (like memory or processing power), or other outside influences that can affect how tests run.
  7. Bugs in tests: Flakiness can also arise from issues within the test scripts. For instance, if the test depends on finding elements using unreliable or fragile methods, like XPath or CSS selectors that are overly specific or based on attributes that change, even small alterations in the web page’s structure can cause the selectors to fail. This can result in tests failing occasionally, even though nothing significant has changed in the application.

Various factors, including timing problems, conflicts between tasks, outside influences like network issues, and fragile test code, can cause flaky tests. It’s important to pinpoint and fix these underlying issues to ensure your tests consistently produce accurate results. This helps maintain trust in the testing process and ensures confidence in the reliability of your applications.

Strategies to Avoid Flaky Tests in Playwright

Following are the strategies to avoid flaky tests in Playwright:

  • Run and Debug Tests Locally: Before committing your tests, thoroughly inspect and run them multiple times on a local machine with resources similar to those of your production server. Use Playwright’s debugging tools, such as the built-in debugger and UI mode, to identify and fix potential issues. This proactive approach can help catch flaky tests early in the development process.
  • Opt for Locators: Playwright advises relying on locators rather than CSS or XPath selectors for pinpointing webpage elements. Locators are deemed more dependable and less prone to causing flakiness as they’re designed to adapt to dynamic page content and HTML structure changes. Leverage Playwright’s native locator functions such as getByRole, getByText, and getByTestId to locate elements with a user-centric approach. This strategy fosters the development of tests that are robust and consistent.
  • Avoid Hard Waits: Relying on fixed time delays (waitForTimeout) to wait for certain actions to complete can lead to flaky tests. Using Playwright’s built-in waiting methods, such as locators and web assertions, is better. These automatically wait for specific conditions to be met within a set timeout, making tests more stable and less prone to failure.
  • Set Appropriate Timeouts: When using Playwright, it’s important to set appropriate timeout values for tests, assertions, and locator actions. These timeouts should be based on your application’s performance characteristics. If timeouts are too short, they may trigger false test failures. Conversely, overly long timeouts can unnecessarily extend test execution times. Therefore, finding a balance is crucial to setting timeouts that align with your application’s behavior and needs.
  • Use locator.all() Carefully: Use the locator when working with lists of elements.all() method judiciously. This method returns an array of locators for elements currently on the page without waiting for additional elements to be dynamically loaded. Call locator.all() only when the list of elements you want to locate has been fully loaded and rendered.
  • Prefer Locators over ElementHandles: Using Locators instead of ElementHandles in Playwright is better. ElementHandles can sometimes cause flaky tests if you don’t use them properly. Locators are a better choice because they have built-in waiting and checks to ensure actions happen only when the element is ready. This makes your tests more dependable and less likely to yield unexpected results.

Using these strategies and taking advantage of Playwright’s strong features can greatly decrease the chances of creating flaky tests and guarantee a more trustworthy and consistent testing process. Consistent and stable tests are crucial for maintaining trust in your application’s quality and making the development and deployment process go smoothly.

Leverage cloud-based platforms like LambdaTest. LambdaTest is an AI-powered test orchestration and execution platform that lets you run manual and automated tests at scale with over 3000+ real devices, browsers, and OS combinations.  It’s Test Intelligence is a powerful feature designed to tackle the issue of flaky tests and improve the overall stability of your automated testing process.

How to Deal With a Flaky Test in Playwright

Here are some steps you can take to deal with flaky tests in Playwright:

  • Dig into the Core Issue:

Use the features provided by Playwright to delve into the test run and understand why it’s yielding unpredictable outcomes. These tools provide a thorough breakdown of each test step, aiding in pinpointing where the problem lies. By examining these specifics, you can gain insight into the test’s behavior and the reasons behind its unexpected performance.

  • Develop a Fix:

After understanding why the test is acting flaky, the next move is to tweak the test logic. This means altering the code that guides the test on what actions to take. You might need to change how the test interacts with the website or app being tested or adjust the timing of specific actions to resolve the issue you’ve identified. After you’ve updated the test:

  1. Run it multiple times locally on your machine.
  2. Try to recreate the same conditions causing the flaky results before, and ensure the test now works consistently.
  3. Run it several times to confirm that the issue has been resolved.
  • Deploy the Updated Test:

Once you’re confident that the updated test works correctly, you can deploy it back into the larger testing system or pipeline. Tests typically run automatically in this environment when new code changes are introduced. Verify that the test now produces the expected results in this automated environment, just as when you ran it locally.

By taking proactive measures to address flakiness, such as using reliable testing practices and leveraging debugging tools, you can establish confidence in the accuracy and consistency of your test results. This ensures the reliability of your code and fosters trust in your development process.


In conclusion, flaky tests can be a major headache regarding automated testing with tools like Playwright. These tests pass sometimes and fail other times, even when nothing has changed. Flaky tests undermine confidence in your testing process, leading to seemingly random failures in continuous integration pipelines.

However, the article provides several strategies for avoiding flaky tests in the first place when writing Playwright tests. This includes using robust locators instead of brittle selectors, avoiding hard sleep, setting appropriate timeouts, using Playwright’s automatic retries, and leveraging the tool’s detailed tracing to identify issues.

Even when flaky tests arise, the article outlines steps to investigate the root cause using Playwright’s logging and debugging capabilities. Once the underlying reason is understood, updates can be made to the test logic and verified locally before deploying back to the automated pipelines.

While flaky tests can never be fully eliminated, following the best practices outlines can minimize their occurrence and impact. This helps ensure your Playwright tests remain reliable and consistent and provide accurate feedback on your web application’s health. Overcoming flakiness allows you to have confidence in your test automation efforts.