B · Core · intermediate · Week 2–3 · Chapter 4 of 12
Custom commands & reusable flows
DRY with intent. Custom commands wrap repeated flows without hiding too much. Keep parameters visible in specs.
Path progress27%
Step 1 of 4
login custom command
Wrap auth in cy.login(user, pass). Specs stay readable; implementation lives in cypress/support/commands.js.
Example
Cypress.Commands.add("login", (username, password) => {
cy.visit("/");
cy.get("[data-test=username]").type(username);
cy.get("[data-test=password]").type(password);
cy.get("[data-test=login-button]").click();
});Do this now
Create cy.login and use it in two specs.
Was this step clear?
Chapter learning outcomes
- cy.commands.add
- Support file
- Type definitions for IDE
Clear these before you leave
Side quest
Add to cart command
cy.addItem("Sauce Labs Backpack") — reusable across cart and checkout specs.