DEV Community

Cover image for Supercharging Your Cypress Tests with Custom Commands
Aswani Kumar
Aswani Kumar

Posted on • Edited on

Supercharging Your Cypress Tests with Custom Commands

Introduction

Cypress is a powerful tool for end-to-end testing, offering a robust set of built-in commands to interact with web applications. However, every project has unique needs that might not be fully covered by the default set of commands. This is where custom commands come in. Custom commands allow you to extend Cypress's functionality, making your tests more readable and maintainable. In this post, we’ll explore how to create and use custom commands in Cypress to enhance your test automation framework.

Why Use Custom Commands?

Custom commands offer several benefits:

  1. Reusability: Encapsulate common actions that are repeated across multiple tests.
  2. Maintainability: Centralize the logic of complex actions, so changes only need to be made in one place.
  3. Readability: Improve the readability of your tests by abstracting away implementation details.

Setting Up Cypress

Before we dive into creating custom commands, let’s set up Cypress if you haven’t already:

npm install cypress --save-dev
Enter fullscreen mode Exit fullscreen mode

After installation, open Cypress:

npx cypress open
Enter fullscreen mode Exit fullscreen mode

Creating Custom Commands

Cypress custom commands are defined in the cypress/support/commands.js file. Let’s walk through some examples to see how you can create and use custom commands.

Example 1: Login Command
Suppose you have a login form that you need to interact with frequently. You can create a custom command to handle the login process:

// cypress/support/commands.js
Cypress.Commands.add('login', (email, password) => {
  cy.visit('/login');
  cy.get('input[name=email]').type(email);
  cy.get('input[name=password]').type(password);
  cy.get('button[type=submit]').click();
});
Enter fullscreen mode Exit fullscreen mode

Now, you can use the login command in your tests:

// cypress/integration/login.spec.js
describe('Login Tests', () => {
  it('Should login with valid credentials', () => {
    cy.login('test@example.com', 'password123');
    cy.url().should('include', '/dashboard');
  });
});
Enter fullscreen mode Exit fullscreen mode

Example 2: Custom Command with Assertions
You can also add custom assertions to your commands. Let’s create a command to check if an element is visible and contains specific text:

// cypress/support/commands.js
Cypress.Commands.add('shouldBeVisibleWithText', (selector, text) => {
  cy.get(selector).should('be.visible').and('contain.text', text);
});
Enter fullscreen mode Exit fullscreen mode

Usage in a test:

// cypress/integration/visibility.spec.js
describe('Visibility Tests', () => {
  it('Should display welcome message', () => {
    cy.visit('/home');
    cy.shouldBeVisibleWithText('.welcome-message', 'Welcome to the Dashboard');
  });
});
Enter fullscreen mode Exit fullscreen mode

Best Practices for Custom Commands

  1. Name Commands Clearly: Use descriptive names for your custom commands to make tests more understandable.
  2. Parameterize Commands: Accept parameters to make commands flexible and reusable.
  3. Chain Commands: Ensure commands return Cypress chainables (cy.wrap()) to enable chaining.
  4. Document Commands: Add comments to describe the purpose and usage of each custom command.

Advanced Tips

  1. TypeScript Support: If you’re using TypeScript, you can add type definitions for your custom commands to enhance autocompletion and type checking.
  2. Error Handling: Implement error handling within custom commands to provide informative feedback when something goes wrong.
  3. Reusable Functions: For complex logic, create helper functions that can be used within custom commands to keep your commands.js file clean and focused.

Conclusion

Custom commands in Cypress provide a powerful way to extend the framework’s capabilities, making your tests more reusable, maintainable, and readable. By encapsulating common actions and adding custom assertions, you can streamline your test automation process and focus on what matters most: ensuring your application works flawlessly.

Start implementing custom commands in your Cypress projects today and see the difference they can make in your testing workflow. Happy testing!

Top comments (0)