Cypress Quick Tip: Custom Commands

02/07/20171 Min Read — In Resources, Testing, Development

You might see that combining multiple items into one logic function is a smart way to dry up your code, naturally you might add a utils.js file and then import that into your test.spec.js files but Cypress has a built-in way to do this. Use custom commands.

In your project directory, navigate to the Cypress folder. Assuming you have already run Cypress at least once and the scaffolded folders are in place, you will see a Commands file in your Support folder.

In this file you will find comments that show you how to write your custom commands. Of course, you can always check the documentation at https://on.cypress.io/custom-commands

Essentially, your functions will be attached to the global Cypress object with Cypress.Commands.new That way you won't have to import it all over your files, you just have access to your new Command in your tests. Super handy!

cypress custom command