Cypress End to End Testing Lessons for Reliable Automation
This article explains the key lessons for building reliable end-to-end (E2E) tests with Cypress, focusing on how to avoid flaky tests and maintain a stable suite. You will learn about Cypress’s unique architecture, best practices for selectors and waiting, and common pitfalls to avoid. The goal is to help you write E2E tests that consistently pass when your application works correctly and fail only when it truly breaks.
Background: Why E2E Testing Is Hard
End-to-end tests simulate real user flows through your entire application, from UI clicks to backend responses. Unlike unit or integration tests, E2E tests run in a full browser environment, which introduces many sources of variability: network latency, animations, third-party scripts, and timing differences. This variability often leads to flaky tests—tests that pass or fail without any code change. Cypress was designed to address many of these issues by running in the same event loop as the application, giving it direct access to the DOM and network requests. This architecture allows Cypress to automatically wait for elements to appear and for network requests to finish, reducing the need for arbitrary sleeps. However, even with Cypress, reliability requires deliberate effort and adherence to best practices.
How Cypress Works: The Key to Its Reliability
Cypress runs in a unique way compared to Selenium or Playwright. Instead of sending commands over a network protocol to a separate browser driver, Cypress executes directly in the browser alongside your application. This gives it the ability to:
- Automatically wait for elements to become actionable, without explicit waits.
- Retry assertions until they pass or time out, reducing flakiness.
- Take snapshots and videos of test runs for easier debugging.
- Pause and inspect the application state at any point during the test.
This architecture also means Cypress is not subject to the same race conditions that plague Selenium tests. However, it introduces its own constraints: Cypress runs in a single browser tab, and it cannot work across multiple tabs or domains without special configuration. Understanding these strengths and limitations is the first step to writing reliable tests.
Lesson 1: Use Data Attributes for Selectors
One of the most common causes of flaky tests is using CSS classes or XPath that change frequently. A better approach is to add dedicated data-cy attributes to elements that you need to interact with. For example:
<button data-cy="submit">Submit</button>
Then in your test:
cy.get('[data-cy=submit]').click();
This decouples your tests from styling and structural changes, making them more resilient. It also makes the intent of the test clearer to other developers.
Lesson 2: Avoid Implicit Waits and Use Cypress’s Built-In Retry
Newcomers often write cy.wait(2000) to give the app time to load. This is a recipe for flakiness because the wait is arbitrary and does not reflect the actual state of the application. Instead, rely on Cypress’s built-in retry mechanism. Commands like cy.get(), cy.contains(), and cy.find() automatically retry until the element exists or the timeout is reached. For assertions, use should() which also retries. If you need to wait for a network response, use cy.intercept() and cy.wait() on a specific alias, which waits for the exact request you expect. For example:
cy.intercept('GET', '/api/users').as('getUsers');
cy.visit('/users');
cy.wait('@getUsers').its('response.statusCode').should('eq', 200);
This approach waits for a real event, not a guess.
Lesson 3: Keep Tests Independent and Isolated
Each test should be able to run on its own, without depending on the state left behind by a previous test. This means you should:
- Use
beforeEach()to set up the necessary state, such as visiting a page or seeding the database. - Avoid sharing variables between tests.
- Clean up any data created during the test, or use a fresh database for each run.
Isolation makes tests easier to debug and reduces the chance of one test’s failure causing a cascade of failures. It also allows you to run tests in parallel without interference.
Lesson 4: Handle Animations and Transitions Explicitly
Animations can cause elements to be present in the DOM but not yet visible or clickable. Cypress automatically waits for elements to be actionable, but sometimes animations can cause unexpected behavior. A good practice is to disable animations in your test environment, or to use cy.clock() to control time. If you cannot disable them, use cy.get() with a timeout and then cy.should('be.visible') before interacting. For example:
cy.get('[data-cy=modal]')
.should('be.visible')
.find('[data-cy=close]')
.click();
This ensures the element is fully rendered and ready for interaction.
Lesson 5: Use Cypress’s Built-In Retry for Assertions
Assertions in Cypress are not immediate; they retry until they pass or time out. This is a powerful feature, but it can also mask issues if you are not careful. For example, if you assert that a text is present, Cypress will keep checking for a few seconds. If the text never appears, the test fails. This is better than a hard wait, but you should still ensure that your assertions are specific and not overly broad. For instance, avoid cy.contains('Submit') if there are multiple elements with that text. Instead, use a more specific selector or chain commands to narrow down the element.
Lesson 6: Manage Network Requests with cy.intercept()
Modern web applications rely heavily on API calls. Cypress’s cy.intercept() allows you to stub or spy on network requests, which can make your tests faster and more reliable. By stubbing responses, you can test your frontend in isolation from backend issues. For example:
cy.intercept('GET', '/api/data', { fixture: 'data.json' }).as('getData');
cy.visit('/');
cy.wait('@getData');
cy.get('[data-cy=result]').should('contain', 'expected');
This not only makes tests deterministic but also faster, as you do not have to wait for real network latency. However, be careful not to over-stub, as you may miss integration issues.
Common Problems and Misconceptions
One common misconception is that Cypress is a drop-in replacement for Selenium. While it is easier to set up and use, it has a different model. For example, Cypress does not support multiple tabs or iframes as easily. Another issue is that tests can become slow if you overuse cy.visit() or if you do not use cy.intercept() to speed up network calls. Additionally, many developers think that because Cypress retries automatically, they do not need to think about timing at all. This is not true; you still need to design your tests to wait for the right conditions, especially when dealing with external services or complex UI states.
Frequently Asked Questions
How do I avoid flaky tests in Cypress?
Flaky tests usually come from relying on arbitrary waits, using fragile selectors, or not isolating tests. Use data attributes for selectors, rely on Cypress’s automatic retries, and stub network requests to make tests deterministic. Also, run tests in a clean environment and avoid depending on a specific order of execution.
Can I use Cypress for testing multiple tabs?
No, Cypress does not support multiple tabs natively. It runs in a single browser tab. If you need to test a flow that involves opening a new tab, you can use cy.url() to assert the new tab’s URL, but you cannot interact with the new tab’s content. For such scenarios, consider refactoring your app to use in-page navigation or use a different tool for that specific test.
What is the difference between cy.wait() and cy.intercept()?
cy.wait() with a number pauses the test for a fixed amount of time, which is not recommended. cy.wait() with an alias, such as cy.wait('@getData'), waits for a specific network request to occur, which is more reliable. cy.intercept() allows you to stub or spy on network requests, so you can control the response and wait for it.
Should I use Cypress for all my testing?
No, E2E tests are slow and expensive to run. Use them for critical user journeys, such as login, checkout, or main features. For unit and integration tests, use tools like Jest or React Testing Library. A balanced testing pyramid is recommended.
How can I speed up Cypress tests?
You can speed up tests by stubbing network requests, avoiding unnecessary waits, and running tests in parallel. Also, keep your test suite focused on critical paths and avoid testing every minor detail. Use cy.clock() to control time-dependent features and disable animations in your test environment.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.