C · Structure · intermediate · Week 5–6 · Chapter 8 of 12
Page Object Model in Cypress
POM in Cypress uses plain classes or modules — not inheritance magic. Encapsulate selectors and intent methods; keep specs as user stories.
Path progress60%
Step 1 of 4
LoginPage class
Methods: visit(), login(user, pass), assertError(msg). Selectors private to the class.
Example
class LoginPage {
visit() { cy.visit("/"); }
login(user, pass) {
cy.get("[data-test=username]").type(user);
cy.get("[data-test=password]").type(pass);
cy.get("[data-test=login-button]").click();
}
assertError(msg) { cy.get("[data-test=error]").should("contain", msg); }
}
export default LoginPage;Do this now
Create cypress/e2e/pages/LoginPage.js. Refactor login specs to use it.
Was this step clear?
Chapter learning outcomes
- Page classes
- Composition over deep hierarchies
- cypress/e2e/pages/ layout
Clear these before you leave
Side quest
Checkout flow POM
CheckoutPage: fillShipping(), fillPayment(), assertConfirmation(). End-to-end purchase spec.