A real Cypress flow, scaffolded
Cypress drives a real browser, so its tests read like a user’s actions: visit a page, type into fields, click a button, and assert on what shows up. A typical first spec covers logging in, landing on a dashboard, and submitting a form. This builder writes that three-test spec with the commands and selectors filled in so you only adjust details.
How it works
The tool emits a describe('...', () => { ... }) block with a beforeEach(() => cy.visit(loginRoute)) that opens your login page before each test. The first it types into the email and password fields with cy.get(selector).type(...), clicks submit, and asserts the URL changed with cy.url().should('include', ...). The second it checks navigation by clicking a link and asserting the new path. The third it fills and submits a form, then asserts a success message is visible. Selectors default to [data-cy="..."] form because those are the most resilient against UI churn.
Tips and example
- Prefer
[data-cy="submit"]selectors over text or class selectors so restyles do not break tests. - Let Cypress retry — assert on
should('be.visible')rather than inserting fixedcy.waitdelays. - Set
baseUrlincypress.config.jsso you can pass relative paths tocy.visit.
describe("Login flow", () => {
beforeEach(() => {
cy.visit("/login");
});
it("logs in with valid credentials", () => {
cy.get('[data-cy="email"]').type("[email protected]");
cy.get('[data-cy="password"]').type("Passw0rd!");
cy.get('[data-cy="submit"]').click();
cy.url().should("include", "/dashboard");
});
});