Cypress Commands
A practical reference for the Cypress commands you'll reach for every day. All examples are TypeScript-first.
Navigation
cy.visit()
Navigate the browser to a URL.
cy.visit('/login');
cy.visit('https://example.com', { timeout: 10000 });
cy.visit('/dashboard', {
auth: { username: 'admin', password: 'secret' },
headers: { 'X-API-Version': '2' },
});cy.reload()
Reload the current page.
cy.reload();
cy.reload(true); // bypass the cachecy.go()
Move forward or back through browser history.
cy.go('back');
cy.go('forward');
cy.go(-1);
cy.go(2);Selectors & Querying
cy.get()
Get one or more DOM elements by selector.
cy.get('[data-testid="submit"]');
cy.get('button.primary');
cy.get('input').first();
cy.get('li').eq(2);
cy.get('.card', { timeout: 8000 });cy.contains()
Get an element containing matching text.
cy.contains('Submit');
cy.contains('button', 'Save changes');
cy.contains('h1', /^Welcome/);
cy.contains('li', 'Tasks').click();cy.find()
Get descendants of the current subject.
cy.get('form').find('input[type="email"]');
cy.get('.card').find('.title').should('contain', 'Hello');cy.within()
Scope subsequent commands to a parent element.
cy.get('form').within(() => {
cy.get('input[name="email"]').type('user@example.com');
cy.get('input[name="password"]').type('correct-horse-battery-staple');
cy.get('button[type="submit"]').click();
});cy.parent(), cy.children(), cy.siblings()
Walk up and across the DOM tree.
cy.get('input[name="email"]').parent('label');
cy.get('ul').children('li.active');
cy.get('.tab.active').siblings();Actions
cy.click()
Click an element.
cy.get('button').click();
cy.get('.dropdown').click({ force: true });
cy.get('li').click({ multiple: true });
cy.get('button').click(50, 30); // click at offset (x, y)
cy.get('a').rightclick();
cy.get('.image').dblclick();cy.type()
Type into an input.
cy.get('input[name="email"]').type('user@example.com');
cy.get('input[name="search"]').type('cypress{enter}');
cy.get('input').type('hello', { delay: 100 });
cy.get('input').type('{selectall}{backspace}');
cy.get('input').type('{shift}{home}'); // keyboard modifierscy.clear()
Clear an input or textarea.
cy.get('input').clear();
cy.get('input').clear().type('replacement');cy.check() / cy.uncheck()
Toggle checkboxes and radios.
cy.get('[type="checkbox"]').check();
cy.get('[type="radio"]').check('option-2');
cy.get('[type="checkbox"]').check(['a', 'b']); // multiple by value
cy.get('[type="checkbox"]').uncheck();cy.select()
Select an option in a <select>.
cy.get('select').select('Option 1'); // by visible text
cy.get('select').select('option-value'); // by value
cy.get('select[multiple]').select(['a', 'b']);cy.scrollTo() / cy.scrollIntoView()
Scroll the page or an element.
cy.scrollTo(0, 500);
cy.get('.list').scrollTo('bottom');
cy.get('.modal').scrollTo('top', { duration: 500 });
cy.get('.footer-link').scrollIntoView();cy.trigger()
Dispatch a DOM event.
cy.get('.draggable').trigger('mousedown', { button: 0 });
cy.get('.target').trigger('mousemove').trigger('mouseup');
cy.get('input').trigger('change');Assertions
should()
Assert about the subject. Cypress retries until the assertion passes or times out.
cy.get('button').should('be.visible');
cy.get('input').should('have.value', 'hello');
cy.get('.alert').should('contain', 'Error');
cy.get('li').should('have.length', 3);
cy.get('a').should('have.attr', 'href', '/home');
cy.get('.spinner').should('not.exist');and()
Chain another assertion against the same subject.
cy.get('input')
.should('be.visible')
.and('have.value', 'hello')
.and('not.be.disabled');expect()
Chai-style assertions inside .then().
cy.get('button').then(($el) => {
expect($el).to.have.text('Submit');
expect($el.attr('type')).to.equal('button');
expect($el).to.have.class('primary');
});Network
cy.intercept()
Stub or spy on requests. Replaces the older cy.route().
// Stub a static fixture
cy.intercept('GET', '/api/users', { fixture: 'users.json' });
// Inline response
cy.intercept('POST', '/api/login', {
statusCode: 200,
body: { token: 'abc' },
});
// Spy + dynamic reply with delay
cy.intercept('GET', '/api/*', (req) => {
req.reply({ delay: 1000, statusCode: 200, body: [] });
}).as('apiCall');
cy.wait('@apiCall').its('response.statusCode').should('eq', 200);
// Force a network error
cy.intercept('GET', '/api/flaky', { forceNetworkError: true });cy.request()
Make an HTTP request directly — no UI involved.
cy.request('GET', '/api/users');
cy.request({
method: 'POST',
url: '/api/login',
body: { username: 'admin', password: 'secret' },
})
.its('body')
.should('have.property', 'token');cy.wait()
Wait for an aliased intercept or a fixed duration (use sparingly).
cy.wait('@getUsers');
cy.wait(['@getUsers', '@getPosts']);
cy.wait(1000); // generally an anti-pattern — prefer aliasesUtilities
cy.wrap()
Wrap a value as a Cypress chainable.
cy.wrap({ name: 'cypress' }).should('have.property', 'name');
cy.wrap([1, 2, 3]).each((item) => {
cy.log(`item: ${item}`);
});cy.task()
Invoke a Node.js task defined in cypress.config.ts.
cy.task('seedDatabase');
cy.task('log', 'Test started');
cy.task('readFile', 'fixtures/data.json').then((data) => {
cy.log(JSON.stringify(data));
});cy.exec()
Execute a system command.
cy.exec('npm run db:reset');
cy.exec('echo $HOME').its('stdout').should('contain', '/Users');
cy.exec('git rev-parse HEAD').its('stdout').should('have.length', 41);cy.fixture()
Load a JSON, image, or text fixture.
cy.fixture('users.json').then((users) => {
cy.intercept('GET', '/api/users', users);
});
cy.fixture('user').as('userData');
cy.intercept('GET', '/api/user', { fixture: 'user.json' });Aliases (.as())
Reuse subjects across commands.
cy.get('[data-testid="email"]').as('emailInput');
cy.get('@emailInput').type('user@example.com');
cy.get('@emailInput').should('have.value', 'user@example.com');Custom commands
Defined in cypress/support/commands.ts.
Cypress.Commands.add('login', (email: string, password: string) => {
cy.session([email, password], () => {
cy.visit('/login');
cy.get('[data-testid="email"]').type(email);
cy.get('[data-testid="password"]').type(password);
cy.get('[data-testid="submit"]').click();
cy.url().should('not.include', '/login');
});
});
// usage:
cy.login('user@example.com', 'secret');