Cypress · Browser testing end to end
What are the most important Cypress best practices?
Short answer
Write independent tests, use stable selectors, wait on conditions rather than time, keep setup in app actions or sessions, and assert on user-visible behaviour. Treat flakiness as a defect.
Good Cypress tests share the same qualities as good tests anywhere: they are independent, deterministic, readable and focused on behaviour. Cypress adds specific tools for each of those, and the practices below use them rather than fighting them.
The practices that matter most
- Each test sets up its own state and does not depend on order
- Select with data-cy or accessible names, not CSS structure
- Wait for requests with intercept aliases, not fixed delays
- Use cy.session or API setup for login and other expensive setup
- Assert on what the user sees, not on internal state or implementation
- Keep specs focused so failures are easy to locate
- Treat a flaky test as a bug and fix the race rather than adding retries
describe("cart", () => {
beforeEach(() => {
cy.intercept("GET", "/api/cart").as("getCart");
cy.visit("/cart");
cy.wait("@getCart");
});
it("removes an item", () => {
cy.get("[data-cy=cart-item]").should("have.length", 2);
cy.get("[data-cy=cart-item]")
.first()
.find("[data-cy=remove]")
.click();
cy.get("[data-cy=cart-item]").should("have.length", 1);
});
});Prefer app actions for setup when the journey is not what you are testing, and keep assertions in the spec so failing output is readable. A common smell is a helper that performs five actions and three assertions; when it fails, nobody can tell which requirement broke.
Do not disable test isolation, add force clicks or raise global timeouts to get a suite green. Those changes treat symptoms. Fix the state or the wait at the point where the test actually depends on it.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why should tests avoid order dependence?
- 2When is an app action better than clicking through the UI?
- 3What makes a Cypress test deterministic?
- 4How do you decide what belongs in a single test?
Say it in the interview
I aim for independent tests that own their data and assert on visible behaviour. I use stable selectors, wait on intercepts rather than time, and reuse login through a session or API setup. I keep assertions in the spec and avoid hidden helpers that make failures ambiguous. When a test is flaky, I fix the race instead of adding retries or force clicks, because those only hide the problem.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Cypress mock interview and get scored feedback on depth, clarity and confidence.