Custom Cypress Commands
I've been loving my first forays into Cypress integration testing. It feels really well made.
Today I learned how to create a custom Cypress command. Logging into the application is a classic use-case: I need to do it in almost every test, and I'd like a function that accepts arguments and centralizes the DOM manipulation.
Here it is:
// cypress/support/commands.js
Cypress.Commands.add('signIn', ({ email, password }) => {
cy.visit('/sign-in');
cy.get('input[name="email"]').type(email);
cy.get('input[name="password"]').type(password);
cy.get('form').submit();
});
The command is now available in any test without an explicit import:
cy.signIn({ email: 'dev@hashrocket.com', password: 'password' });
Abstraction!
Tweet