Is it possible to load command files in spec.js files instead of support/index.js in cypress?

73 views Asked by At

Right now whenever we write a new file that contains Cypress custom commands we add it using Cypress.Commands.add() and then import the file in support/index.js.

But my problem here is that I've 100 such Custom command files, and I cannot load every file everytime I run cypress, because this is going to cause memory issue soon or after

Instead is there a way to import the cypress custom command files in each testcase spec file itself (if needed) instead of support/index.js?

2

There are 2 answers

1
Rachel.Zawadi On BEST ANSWER

You can import the commands individually in the spec where they are used, the syntax is the same as you are already using in support/index.js. I can't think of any reason why not.

This would only have an effect if you run the specs individually, obviously.

But I do not think it will cause much difference to the memory usage, since code files have a small footprint (compared to say fixture files).

2
Yogendiran Barath On

I just found out that you can import the custom command file in your test script file as well

I just added

import './customCommand.js'

in my test script file and accessed the command as usual like

cy.customCommand1();

and it worked fine