Cypress · Browser testing end to end
Should you use the page object model in Cypress?
Short answer
Page objects can work, but Cypress tends to favour app actions and small reusable functions. Use whichever keeps tests readable, and avoid an abstraction that hides assertions or duplicates selectors.
The page object model wraps a page's selectors and actions in a class or module. It originated with remote-driver frameworks where direct DOM access was expensive. Cypress can query the DOM directly and retries assertions, so the original motivation is weaker, but the readability benefit can remain for large pages.
export const loginPage = {
visit() {
cy.visit("/login");
},
fill(email: string, password: string) {
cy.get("[data-cy=email]").type(email);
cy.get("[data-cy=password]").type(password);
},
submit() {
cy.get("[data-cy=submit]").click();
},
};
loginPage.visit();
loginPage.fill("asha@example.com", "secret123");
loginPage.submit();
cy.location("pathname").should("eq", "/dashboard");Cypress-friendly alternatives
- App actions: call the API or set state directly instead of clicking through setup UI
- Small helper functions that return a chain
- Custom commands for repeated user intent
- Page objects focused on actions and selectors, not on assertions
- A shared fixture or factory for test data
The main risk is a page object that asserts internally and returns nothing. When it fails, the test body does not show what was expected, and the object becomes a second test suite. Keep assertions in the spec, or expose a clear, named action that the spec can assert around.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1Why is the page object pattern less necessary in Cypress?
- 2What is an app action?
- 3Where should assertions live when using page objects?
- 4When would you introduce a page object?
Say it in the interview
Page objects can still help on large screens, but Cypress does not need them the way older remote-driver tools did. I often prefer app actions for setup and small helpers or custom commands for repeated intent. If I use a page object, I keep assertions in the spec so a failure is readable. The abstraction should reduce repetition, not hide what the test verifies.
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.