Avoiding Flaky Tests in Playwright

On This Page What Are Flaky Tests in Playwright?

February 27, 2026 · 10 min read · Tool Comparison

How to Detect and Avoid Playwright Flaky Tests in 2026?

Playwright tests that surpass one moment and fail the next without any codification change create contiguous friction. CI pipelines slacken down, failures become difficult to trust, and squad spend more time rerunning examination than doctor real issues.

That inconsistency rarely happens by hazard. Flaky conduct usually arrive from timing gaps, precarious selectors, or environment difference that quietly slip into test suites. If left unchecked, these issues erode confidence in automation.

Overview

Why Do Flaky Tests Occur?

  • Timing topic
  • Unstable chooser
  • External service habituation
  • Varying tryout environments
  • Limited scheme resources
  • Test interference
  • Random data and async behavior

How to Avoid Writing Flaky Tests in Playwright?

  • Run and debug tests locally:Use npx playwright test & # 8211; debug and page.pause () to catch flaky conduct before promote to CI.
  • Avoid hard delay:Replace waitForTimeout () with Playwright ’ s auto-waiting actions like chink () and fill ().
  • Control the test environment:Fix browser, viewport, and permissions in playwright.config.ts for logical execution.
  • Use locators instead of selectors:Prefer role-, text-, or label-based locators over brittle CSS or XPath chooser.
  • Avoid random values:Use fixed or seeded data to maintain exam deportment deterministic.
  • Ensure test isolation:Reset province before each test to prevent shared data or side effects.
  • Configure reflex retries:Enable retries to manage passing failures and identify freakish tests.
  • Limit external dependencies:Mock third-party APIs and services to avoid network-related instability.

This article explicate how flaky tests affect Playwright task, why they happen, how to prevent them, and how to detect them using tools like BrowserStack Automate.

What Are Flaky Tests in Playwright?

in are automated tests that legislate during one performance and fail the following, still when no change feature been do to the codebase. They make both false positives and mistaken negatives, which squander debugging time and make it hard to get real issues.

Also Read:

Impact of Flaky Tests on CI Pipelines

Flaky tests waste time on false failures and slow down liberation. Here are some ways they impact.

  • False alarms:Eccentric exam cause failures that are not existent and make teams expend clip investigating issues that do not really exist.
  • Blocked deployments:When test results find unreliable teams hesitate to push code which slow down releases and disrupts development flow.

Read More:

  • Repeated reruns: Tests often need multiple runs to determine if a failure is real or just flaky, which wastes time and imagination.
  • Loss of trust: Over clip, teams stop trusting test results and ignore failures, adventure missing real problems.
  • Slower feedback: Flaky tests make dissonance that make it take longer to identify and fix unfeigned bug, delaying the ontogenesis summons.

Read More:

Why Do Flaky Tests Occur?

Flaky trial make inconsistent results despite unchanged code. This pass because:

  • Timing issues: Tests fail if the page elements haven ’ t amply loaded or appeared yet. For example, if a push hasn ’ t appear, the examination can ’ t click it and will neglect.
  • Unstable selectors: Tests break when developer change the page layout because the selectors can & # 8217; t encounter the right elements anymore. For example, if a developer changes a push ’ s class gens, the tryout won ’ t be able to find it and will end up betray.

Read More:

  • External service trouble: If trial bet on APIs or databases that are slow or unreliable, they & # 8217; ll fail when those services conduct too long to respond or crash.
  • Varying : Testing surroundings vary because browsers, operating scheme, and networks behave and perform differently. These departure affect how pages load or respond, causing tryout to pass in one environment but fail in another.
  • Circumscribed resources: Tests can slow down or fail unexpectedly if the machine lead them has determine retention or CPU resources.
  • Test intervention: Tests that share information or state can conflict with each other, leading to treacherous upshot. For example, if two tests update the same user account at the same time, one might overwrite the other ’ s changes and lead to inconsistent resultant.
  • Random data and async operations: Tests that use random inputs or deal with asynchronous code can behave otherwise each clip they run.

How to Avoid Writing Flaky Tests in Playwright

Stable Playwright tests require specific practices that trim flakiness. Implement the following methods with examples and code snipping:

1. Run and Debug Tests Before Committing

Execute tests locally usingnpx playwright test and & # 8211; debug modeto get failure early. Playwright ’ s debug mode suspension at breakpoints, making it easier to trace flaky behaviour before promote to CI.

Here ’ s how to use Playwright ’ s debug mode:

test ('example exam ', async ({page}) = & gt; {await page.goto ('https: //example.com '); await page.pause (); // Opens Playwright Inspector for debug await expect (page.locator ('h1 ')) .toHaveText ('Example Domain ');});

Read More:

2. Avoid Hard Waits

Hard expect pause test execution for a fixed amount of clip, yet if the app is already ready or still loading. This take to failures when the app loads dim or faster than expected. Use Playwright ’ s built-in auto-waiting, which look for elements to go visible, attached, and actionable before interact with them.

// Avoid this await page.waitForTimeout (5000); // Use auto-waiting await page.click ('button: has-text (`` Submit '') ');

Read More:

3. Control the Testing Environment

Inconsistent environments can cause examination to behave differently across runs. Use fixed settings for viewport size, browser, and permissions to cut variability. Set these in playwright.config.ts to ensure every test scat in the same environment:

use: {browserName: 'chromium ', viewport: {width: 1280, height: 720}, permissions: ['geolocation '],}

This helps forestall environment-based flakiness and makes test results more predictable.

4. Use Locators Instead of Selectors

oftentimes break when the UI changes. Playwright locators are more stable because they rely on roles, text, or labels. Use this approach for more dependable element targeting:

// Avoid await page.click (' # submit-btn '); // Use await page.getByRole ('button ', {name: 'Submit '}) .click ();

5. Avoid Random Values

Random inputs do tests treacherous because they make different outcome each run. Use rigid value or seed your randomness to keep test behavior predictable.

Here ’ s an example of using fixed input values:

const username = 'testuser '; await page.fill (' # username ', username);

6. Ensure Test Isolation

Tests that look on share state can interfere with each other and create daftness. ensure each trial escape independently with a clean setup.

Here ’ s how to reset the state before each test:

test.beforeEach (async ({page}) = & gt; {await page.goto ('https: //example.com/reset ');});

60 % of CI failure are flaky tests

Validate Playwright test on existent environments using BrowserStack before flaky failures reach CI.

7. Configure Automatic Retry

Some failures arrive from impermanent mesh or environment issues. Setting retries help detect gonzo tests by rerunning failed 1 before marking them as broken.

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

Here ’ s how to configure retries in Playwright:

module.exports = {retries: 2, // Retry failed tryout up to 2 multiplication};

Read More:

8. Limit Dependence on External Resources

International services like third-party APIs can be slow or unreliable during tests. Mock these calls using page.route to avoid delays and ensure consistent reply:

await page.route (' * * /external-api/ * * ', route = & gt; route.fulfill ({status: 200, body: JSON.stringify ({data: 'mocked '})}));

Read More:

How to Detect Flaky Tests in Playwright?

Use these methods to name flaky tests.

1. Loop Suspected Tests to Confirm Flakiness

If a test fails intermittently in CI but not locally, it may be off-the-wall. Run that specific test multiple times in a controlled local environment to see if it betray at random. Use a loop or a script to automatise repeated execution:

for (let i = 0; i & lt; 20; i++) {const result = await exec ('npx playwright test tests/sample.spec.ts '); console.log (` Run $ {i + 1}: `, result.status);}

This helps confirm whether the topic is existent or just a one-time failure.

Read More:

2. Use Playwright & # 8217; s Built-In Retry Feature

Retries help surface test that fail for non-deterministic reasons. When a test legislate only after a retry, it likely depends on unstable factors like timing,, or inconsistent apparatus.

You can enable retries globally or per test:

// Global setting in playwright.config.ts retries: 2 // Per-test setting test ('example test ', async ({page}) = & gt; {// test logic}) .retries (2);

3. Capture Flaky Behavior With TestInfo

TestInfo in Playwright provides metadata about the current test run, such as status, retries, and error details. Logging these facilitate identify flaky trial by shew how often a trial demand rerunning and what causes failure. For example, you can log retry attempts inside your examination:

test ('retry log demo ', async ({page}, testInfo) = & gt; {if (testInfo.retry) {console.log (` This test is retrying for the $ {testInfo.retry} time `);}});

This information highlights prove that behave inconsistently and need investigation.

4. Analyze With Trace Viewer and Video

Enable tracing or video for flakey exam to replay the test step-by-step and name where timing or async failures occur.

use: {trace: 'on-first-retry ', video: 'on-first-retry'}

5. Run Flaky Tests in Isolation Using Test.only

Test.only is a command in Playwright that lam solely the specified test, skipping all others. Use it to isolate flaky tests that pass entirely but neglect when run with the total suite. This facilitate identify issues caused by shared province, side effects, or test order.

6. Use Hard-and-fast Locators to Reduce Timing Issues

Flaky failures like “ locator not found ” often happen because the test seek to accession elements before they seem. Using strict locators and awaiting visibility checks makes trial look properly and reduce timing-related flakiness. Here ’ s how to use it:

await expect (page.locator ('button: has-text (`` Submit '') ')) .toBeVisible ();

7. Monitor flaky exam patterns over time

Track recurring flaky tests using Playwright ’ s HTML reports or CI analytics tools. Adding tags like@flakyhelps filter these tests and prioritize fixing them based on frequency and encroachment.

Fixing Playwright Flaky Tests

Once a outre test is identified, fix it by targeting the root cause to ensure consistent issue:

1. Analyze Failure Details

Use Playwright ’ s Trace Viewer to inspect failure in detail. Run the following to review test steps, shot, and net action.

npx playwright show-trace trace.zip

2. Use Rich Locators

Replace thin selectors with Playwright ’ s role-based or chained locator for stable element aim. For example:

await page.getByRole ('button ', {name: 'Submit '}) .click ();

3. Avoid Hard waits

Remove fixed delay. Instead, delay for specific app conditions like responses or element states:

await require (page.locator ('.toast-message ')) .toHaveText ('Saved successfully ');

Also Read:

4. Handle Async Flows Properly

Ensure seafaring and API calls are complete before move. Use Playwright ’ s built-in wait and affirmation.

5. Run Qualified Tests Sequentially

If tests parcel state or data, disable parallelism for that scope:

test.describe.configure ({mode: 'serial '});

6. Validate Fixes Across Environments

Test your changes locally, in CI, and across browser to reassert stability before merging.

How BrowserStack Helps Detect Flaky Tests in Playwright

Playwright cover test flakiness with auto-waits, locator strategies, and trace debugging. However, it runs locally or in headless containers, which may miss environment-specific failures. runs tests on real devices and browser with actual run systems, screen sizes, and web conditions.

While Playwright can sham throttled conditions like slow networks or device, BrowserStack reveals real-world variability in,, and third-party behavior. This make it easier to identify flakiness draw to platform differences.

To run Playwright tests on and detect outlandish issues:

1. Install Dependencies

npm install -D @ browserstack/playwright-cli

2. Authenticate Using BrowserStack Credentials

export BROWSERSTACK_USERNAME='your_username' export BROWSERSTACK_ACCESS_KEY='your_access_key '

3. Set Up Test Configuration

Update playwright.config.ts to include BrowserStack ’ s project name, build, and test scene. For exemplar:

use: {browserName: 'chromium ', viewport: {width: 1280, height: 720}, screenshot: 'on ', video: 'on ', hint: 'on ',}

4. Run Tests with BrowserStack

browserstack-playwright -- config playwright.config.ts

5. Enable Retries and Traces:

retries: 2, use: {trace: 'on-first-retry ', video: 'on ',}

6. Review Session Logs

Utilize the BrowserStack splasher to view video recordings, console logarithm, traces, and failure reasons.

Talk to an Expert

Conclusion

Eccentric tests cause intermittent failures due to timing issues, surroundings differences, or external factors, sabotage trust in trial results and slacken growing. Detecting them requires running test repeatedly across varied environs and analyzing logarithm to spot inconsistencies.

However, real device examination is crucial because it reveals issues induce by actual hardware, operating scheme, and network weather that simulator or local frame-up miss. BrowserStack provides access to thousands of existent devices and browsers, allowing Playwright tests to run in. Its detailed logs, videos, and trace datum helper quickly detect and fix flaky tests, improving test reliability and product caliber.

Useful Resources for Playwright

Tool Comparisons:

Tags
32,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free