Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to 2 create multiple command files in cypress

Tags:

cypress

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?

like image 547
Waterworks Avatar asked Aug 16 '19 16:08

Waterworks


People also ask

Where do I put custom commands in Cypress?

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.

What is Cypress Chainable?

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.

What is the syntax to pass the string in Cypress?

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.


1 Answers

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";
like image 127
Brendan Avatar answered Sep 22 '22 19:09

Brendan