Cypress Commands
Navigation & Querying
// Navigation
cy.visit('/login');
cy.visit('https://example.com', { timeout: 10000 });
cy.reload();
cy.go('back');
cy.go('forward');
// Querying elements
cy.get('#email'); // by CSS selector
cy.get('[data-cy="submit-btn"]'); // test attribute (recommended)
cy.get('button').first();
cy.get('li').eq(2); // 0-indexed
cy.get('input').filter(':visible');
// Text content
cy.contains('Submit');
cy.contains('button', 'Sign In');
cy.contains('[data-cy="alert"]', 'Error');
// Traversal
cy.get('.form').find('input'); // within parent
cy.get('label').next('input');
cy.get('tr').parent();
cy.get('.card').children();
// URL / title
cy.url().should('include', '/dashboard');
cy.title().should('equal', 'Dashboard');
Actions โ click, type, select
// Clicking
cy.get('button').click();
cy.get('a').click({ force: true }); // bypass visibility check
cy.get('.item').dblclick();
cy.get('.menu-trigger').rightclick();
// Typing
cy.get('#email').type('user@example.com');
cy.get('#password').type('secret{enter}'); // {enter} key
cy.get('#search').type('{selectAll}new text');
cy.get('#field').clear().type('new value');
// Forms
cy.get('select').select('Option 2');
cy.get('select').select(['opt1', 'opt2']); // multi-select
cy.get('[type="checkbox"]').check();
cy.get('[type="checkbox"]').uncheck();
cy.get('[type="radio"]').check('value1');
// File upload
cy.get('input[type="file"]').selectFile('cypress/fixtures/upload.pdf');
// Keyboard
cy.get('body').type('{ctrl}a');
cy.focused().type('{esc}');
Assertions โ should / and
// Existence & visibility
cy.get('.modal').should('exist');
cy.get('.modal').should('be.visible');
cy.get('.loading').should('not.exist');
cy.get('#msg').should('be.hidden');
// Text content
cy.get('h1').should('have.text', 'Welcome');
cy.get('p').should('contain.text', 'partial match');
// Value
cy.get('input').should('have.value', 'Alice');
cy.get('input').should('be.empty');
// Attributes & classes
cy.get('a').should('have.attr', 'href', '/home');
cy.get('.btn').should('have.class', 'active');
cy.get('.btn').should('not.have.class', 'disabled');
// Count
cy.get('li').should('have.length', 5);
cy.get('li').should('have.length.greaterThan', 2);
// Multiple assertions with .and()
cy.get('input')
.should('be.visible')
.and('have.attr', 'type', 'email')
.and('not.be.disabled');
cy.intercept โ Network Stubbing
// Stub API response
cy.intercept('GET', '/api/users', { fixture: 'users.json' }).as('getUsers');
cy.intercept('POST', '/api/users', { statusCode: 201, body: { id: 99 } });
// Wait for request
cy.visit('/');
cy.wait('@getUsers');
cy.get('@getUsers').its('response.statusCode').should('eq', 200);
// Match with pattern
cy.intercept('GET', '/api/products*', (req) => {
req.reply({
statusCode: 200,
body: [{ id: 1, name: 'Widget' }],
delay: 100,
});
}).as('getProducts');
// Spy without stubbing
cy.intercept('POST', '/api/analytics').as('analytics');
cy.get('button').click();
cy.wait('@analytics').its('request.body').should('have.property', 'event', 'click');
Custom Commands & Aliases
// cypress/support/commands.ts
declare global {
namespace Cypress {
interface Chainable {
login(email: string, password: string): Chainable;
}
}
}
Cypress.Commands.add('login', (email, password) => {
cy.request('POST', '/api/login', { email, password })
.its('body.token')
.then(token => {
window.localStorage.setItem('auth_token', token);
});
});
// Usage:
cy.login('admin@example.com', 'password');
cy.visit('/dashboard');
// Aliases
cy.get('[data-cy="submit"]').as('submitBtn');
cy.get('@submitBtn').click();
cy.intercept('GET', '/api/data').as('dataRequest');
cy.wait('@dataRequest').its('response.body').as('data');
cy.get('@data').should('have.property', 'total');
Command Quick Reference
| Command | Purpose |
|---|---|
cy.visit(url) | Navigate to URL |
cy.get(selector) | Find element(s) |
cy.contains(text) | Find by text content |
cy.click() | Click element |
cy.type(text) | Type into element |
cy.should() | Assert on element |
cy.intercept() | Mock/spy network requests |
cy.wait(alias) | Wait for intercepted request |
cy.fixture() | Load fixture data |
cy.screenshot() | Capture screenshot |