Back to Test Automation
2026-02-289 min read

Debug and fix flaky tests (Test Automation)

Learn Debug and fix flaky tests (Test Automation) step by step with clear examples and exercises.

Why This Matters

Debugging flaky tests is an essential part of test automation that ensures reliable results and maintains the integrity of your automated testing suite. Flaky tests can lead to false positives or negatives, causing confusion and wasting valuable time during development cycles. In this lesson, we will guide you through debugging flaky tests using popular test automation frameworks such as Selenium, Cypress, and Playwright with JavaScript examples.

Why This Matters

Flaky tests can have a significant impact on the quality of your software and the efficiency of your development process. False positives or negatives can lead to incorrect assumptions about the state of your application, which can result in regressions reaching production. Debugging flaky tests is crucial for maintaining the reliability of your test suite, ensuring code quality, and preventing regressions from affecting your users.

Prerequisites

  • Basic understanding of JavaScript programming
  • Familiarity with one or more test automation frameworks: Selenium, Cypress, Playwright
  • Knowledge of the test automation lifecycle
  • Understanding of how to write test cases and test suites

Prerequisite Details

To fully understand this lesson, you should have a basic understanding of JavaScript programming concepts such as variables, functions, loops, and conditionals. Familiarity with one or more test automation frameworks is necessary for implementing the strategies discussed in this lesson. You should also be familiar with the test automation lifecycle, including writing test cases, setting up test environments, running tests, and analyzing results.

Core Concept

Flaky tests are tests that pass sometimes but fail at other times without any code changes. This inconsistency can be due to various reasons such as network issues, browser compatibility problems, or race conditions. To debug flaky tests, you need to identify the root cause and implement strategies to make them more stable.

Debugging Strategies

  1. Isolate the test: Run the failing test in isolation to eliminate external factors that might be causing the failure.
  2. Inspect the browser console: Check the browser console for any error messages or warnings that could indicate the cause of the flakiness.
  3. Use logging statements: Add debugging statements to your code to help identify where the test is failing and gather more information about the execution flow.
  4. Slow down the test: Introduce delays in your test code to allow time for elements to load or other asynchronous operations to complete.
  5. Use stable selectors: Use CSS selectors that are less likely to change, such as class names or IDs, instead of relying on XPath or other unstable selectors.
  6. Handle exceptions gracefully: Implement exception handling in your test code to handle unexpected errors and continue with the remaining tests.
  7. Use a headless browser: Test in a headless browser to eliminate user interaction issues that might be causing flakiness.
  8. Run tests multiple times: Run the failing test multiple times to see if it consistently fails or passes, which can help identify intermittent issues.
  9. Use assertion libraries: use assertion libraries like Chai for JavaScript to write more robust and reliable tests.
  10. Refactor the code: Refactor the code that is causing the flakiness to make it more stable and less prone to errors.

Worked Example

Let's consider a simple example using Cypress, where a test fails due to an element not being present on the page.

describe('Flaky Test', () => {
it('Check if element is present', () => {
cy.visit('http://example.com')
cy.get('#element').should('be.visible') // This line causes the test to fail sometimes
})
})

To debug this flaky test, we can:

  1. Isolate the test by running it in isolation.
  2. Inspect the browser console for any error messages or warnings.
  3. Add a logging statement before the failing line to see if the element is present when the test fails.
  4. Slow down the test using cy.wait() to ensure the page has loaded completely before checking for the element.
  5. Use a stable selector instead of #element.
  6. Handle exceptions gracefully by wrapping the failing line in a try-catch block and logging the error if it occurs.
  7. Run the test multiple times to see if it consistently fails or passes.
  8. Refactor the code to make it more stable, for example, by using a function that checks for the element’s presence periodically until it is found.

Common Mistakes

  1. Using unstable selectors: Using XPath or other unstable selectors can lead to flaky tests due to changes in the DOM structure.
  2. Ignoring browser console errors: Ignoring error messages in the browser console can cause tests to fail without proper diagnosis.
  3. Not handling exceptions gracefully: Failing to handle exceptions can result in test failures and make it difficult to identify the root cause of the issue.
  4. Relying on user interaction: Tests that rely on user interaction are more prone to flakiness due to differences in how users interact with the application.
  5. Not slowing down tests enough: Not allowing enough time for elements to load or asynchronous operations to complete can cause tests to fail.
  6. ### Common Mistakes - Subheadings
  • Using unstable selectors: Using XPath or other unstable selectors can lead to flaky tests due to changes in the DOM structure.
  • Ignoring browser console errors: Ignoring error messages in the browser console can cause tests to fail without proper diagnosis.
  • Not handling exceptions gracefully: Failing to handle exceptions can result in test failures and make it difficult to identify the root cause of the issue.
  • Relying on user interaction: Tests that rely on user interaction are more prone to flakiness due to differences in how users interact with the application.
  • Not slowing down tests enough: Not allowing enough time for elements to load or asynchronous operations to complete can cause tests to fail.
  1. Using brittle assertions: Using assertions that are sensitive to minor changes in the output can lead to flaky tests.
  2. Testing on unstable environments: Testing on environments with network issues, low resources, or other instability factors can increase the likelihood of flaky tests.
  3. Not validating expected states: Failing to validate expected states before performing actions can result in flaky tests due to unexpected changes in the application state.
  4. Using outdated test automation frameworks: Using outdated test automation frameworks can lead to compatibility issues and increased likelihood of flaky tests.

Practice Questions

  1. You have a test that fails due to an element not being present on the page. How would you debug this issue using Selenium WebDriver in JavaScript?
  2. A test is failing intermittently due to network issues. What strategies could you use to make it more stable?
  3. You notice that a test is passing sometimes but failing at other times without any code changes. What steps would you take to identify the root cause of the issue?
  4. How would you handle exceptions in your test code using Chai assertion library in JavaScript?
  5. A test is relying on user interaction, and you suspect that this might be causing flakiness. How could you refactor the test to make it more stable?
  6. ### Practice Questions - Subheadings
  • Selenium WebDriver: You have a test that fails due to an element not being present on the page. How would you debug this issue using Selenium WebDriver in JavaScript?
  • Investigate the browser console for any error messages or warnings.
  • Use logging statements to help identify where the test is failing and gather more information about the execution flow.
  • Slow down the test by introducing delays in your test code.
  • Refactor the code to make it more stable, for example, by using a function that checks for the element’s presence periodically until it is found.
  • Network issues: A test is failing intermittently due to network issues. What strategies could you use to make it more stable?
  • Run the test on a stable network connection.
  • Introduce retry logic in your test code to handle temporary network issues.
  • Use mock services or stubs to simulate expected network behavior during tests.
  • Identifying root cause: You notice that a test is passing sometimes but failing at other times without any code changes. What steps would you take to identify the root cause of the issue?
  • Isolate the test by running it in isolation.
  • Inspect the browser console for any error messages or warnings.
  • Add logging statements to help identify where the test is failing and gather more information about the execution flow.
  • Run the test multiple times to see if it consistently fails or passes, which can help identify intermittent issues.
  • Chai assertion library: How would you handle exceptions in your test code using Chai assertion library in JavaScript?
  • Wrap the failing line in a try-catch block and log the error if it occurs.
  • Use Chai's expect function to write more robust tests that can handle expected exceptions.
  • Refactoring for stability: A test is relying on user interaction, and you suspect that this might be causing flakiness. How could you refactor the test to make it more stable?
  • Remove any user interaction from the test by automating the actions using test automation framework functions.
  • Use a headless browser to eliminate user interaction issues that might be causing flakiness.
  • Refactor the code to make it more stable, for example, by using a function that checks for the element’s presence periodically until it is found.

FAQ

  1. Why do flaky tests occur? Flaky tests can occur due to various reasons such as network issues, browser compatibility problems, or race conditions.
  2. How can I isolate a failing test? To isolate a failing test, run it in isolation by either running the test individually or creating a separate test suite for problematic tests.
  3. What is the best way to handle exceptions in test code? Handle exceptions gracefully by wrapping the failing line in a try-catch block and logging the error if it occurs. You can also use assertion libraries like Chai to write more robust and reliable tests.
  4. How can I make my selectors more stable? Use CSS selectors that are less likely to change, such as class names or IDs, instead of relying on XPath or other unstable selectors.
  5. What strategies can I use to make flaky tests more stable? Strategies for making flaky tests more stable include slowing down the test, using stable selectors, handling exceptions gracefully, and refactoring the code to make it more reliable.
  6. ### FAQ - Subheadings
  • Flakiness causes: Why do flaky tests occur? Flaky tests can occur due to various reasons such as network issues, browser compatibility problems, or race conditions.
  • Isolating a failing test: How can I isolate a failing test? To isolate a failing test, run it in isolation by either running the test individually or creating a separate test suite for problematic tests.
  • Handling exceptions: What is the best way to handle exceptions in test code? Handle exceptions gracefully by wrapping the failing line in a try-catch block and logging the error if it occurs. You can also use assertion libraries like Chai to write more robust and reliable tests.
  • Stable selectors: How can I make my selectors more stable? Use CSS selectors that are less likely to change, such as class names or IDs, instead of relying on XPath or other unstable selectors.
  • Strategies for stability: What strategies can I use to make flaky tests more stable? Strategies for making flaky tests more stable include slowing down the test, using stable selectors, handling exceptions gracefully, and refactoring the code to make it more reliable.
Debug and fix flaky tests (Test Automation) | Test Automation | XQA Learn