Creating Custom Commands in Cypress for Smarter Testing

Uncategorized By Apr 14, 2023

Cypress is an end-to-end testing framework for web applications. One of its key features is the ability to create custom commands that improve test efficiency and readability. These commands can be defined in the commands.js file and are automatically loaded by Cypress. The syntax for creating a custom command is straightforward, and they can be chained together like Cypress’s default commands. Benefits of custom commands include reducing code duplication, improving readability, increasing productivity, and easy maintenance. Common FAQs include the inability to override default commands, the ability to pass callbacks, and the option to export custom commands for use in other projects.

Creating Custom Commands in Cypress for Smarter Testing

Creating Custom Commands in Cypress for Smarter Testing


Cypress is a modern end-to-end testing framework for front-end web applications. It provides a fast, reliable and easy solution for automated testing of web applications. One of the key features of Cypress is the ability to create custom commands, which makes writing tests more efficient and readable. Custom commands are a powerful way to extend the functionality of Cypress and helps to create more expressive and descriptive tests.

Creating Custom Commands

Custom commands in Cypress can be created by defining them in the commands.js file. This file is automatically loaded by Cypress when the test runner starts, and all custom commands defined in the file are available for use in the tests.


The syntax for creating a custom command is simple. It follows the format:

			Cypress.Commands.add('commandName', (arg1, arg2, ...) => {
			// Implementation of the command

The commandName is the name of the custom command, and the arguments (arg1, arg2, …) are the inputs required by the command. The implementation of the command goes inside the curly braces.


Here is an example of a custom command that logs in a user:

			Cypress.Commands.add('login', (username, password) => {

This command can be used in the test like this:

			cy.login('[email protected]', 'password123')

Custom commands can be chained together and used in the same way as the Cypress default commands.

Benefits of Custom Commands

  1. Reduces code duplication: Custom commands allow for creating reusable code snippets, which can be used multiple times without repeating the same code over and over again.
  2. Improves readability: Custom commands enable developers to write expressive and descriptive tests. The tests become more readable and easier to understand, which helps to reduce confusion and misinterpretation of the code.
  3. Increases productivity: Custom commands help developers to write tests quickly and efficiently. They eliminate the need for writing repetitive code and simplify the syntax.
  4. Easy to maintain: Custom commands make the tests more modular and maintainable. Any changes in the application or test code can be easily made without affecting the rest of the code.


Can I override Cypress’s default commands with custom commands?
No, the default commands cannot be overridden, but they can be extended with custom commands.
Is it possible to pass callbacks as arguments to custom commands?
Yes, callbacks can be passed as arguments to custom commands. They can be passed as a function or an object with a function.
Can I export custom commands to use in different projects?
Yes, custom commands can be exported and used in different projects. You just need to copy the commands.js file and include it in the project’s Cypress folder.
Is there any limit to the number of custom commands that can be created?
No, there is no limit to the number of custom commands that can be created.