To manage or to have more structures format i would like to create multiple commands files based on different pages i have in the application. would it be really possible to create multiple commands files, if yes how we should? else how we can manage all the page scenarios in one commands file?
The best place to define the custom commands is in the cypress/support/commands. js file, as it loads before any of the test-script files.
A Chainable object is what all Cypress commands return, and are what is stored in the queue of commands mentioned above. As explained, the commands don't actually perform the action right away, but only when the commands in the queue are getting processed, after the entire test function body completes.
Use back-ticks to create the selector string function myElement(text) { const selector = `span:contains(${text})` return cy. get(selector); } or shorter function myElement(text) { return cy. get(`span:contains(${text})`); } or custom command Cypress.
In the default cypress/support/index.js
file you'll see a line like this:
import "./commands";
This adds the commands from the cypress/support/commands.js
file.
You can similarly add a cypress/support/someOtherCommands.js
file and import it in the cypress/support/index.js
file by adding this line:
import "./someotherCommands";
You can add as many of those as you want, you could also nest them in directories for better structure. Example: cypress/support/settings/mainSettingsPage.js
would be imported with:
import "./settings/mainSettingsPage";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With