DEV Community

Hatem Hatamleh
Hatem Hatamleh

Posted on

Add multi commands in Cypress

If you used Cypress before, you would know that it comes with its API for creating custom commands. When creating a new Cypress project, and in the folder structure, we can find the support folder. And inside this folder, we can see a file called commands.js.

If we want to add our custom command, we can use Cypress API as below:

folder structure

Writing all the commands in one file for a small project is fine, but it means that the commands file may contain more than 100 commands for a more extensive project! So we will need to find a better way to organise our commands.

If we open the index.js file inside the support folder, we can see that cypress imports the commands file at the top; this means that we can also create a different javascript file and add our commands and then import it in the index.js file.

index folder

So let's create a new folder and call it commands inside the cypress folder, and inside it, we can create as many files as we want, let's say auth-commands.js, dashboard-commands.js, payment-commands.js
And inside these files, we add all the related commands.

commands folder

And the only thing left for us to do is to import these files into the index.js file.

index folder

Top comments (2)

Collapse
 
prasannakumar1 profile image
Prasanna-Kumar-1

Nice article. Thanks @hatemhatamleh for sharing this.

Collapse
 
akkiswag profile image
Akshaya

Nice way to organise functionality wise commands.