I am writing some Cypress
test cases and have been trying to figure out the best way to simplify it down. Lets say I have 10 forms, all with the exact same fields. Instead of creating a new .cy.js
file for each form I was hoping I could just create one and dynamically pass in the formID
with the command.
Something like this npx cypress run --spec cypress/forms/basicLayoutForm.cy.js/pass formID here
Is this even possible? I have been looking around and found this, but that looks a little to heavy for what I am trying to accomplish. I simply just want to pass one parameter (formID
)
I have found my solution…
I can pass it in like so
npx cypress run --env formID=myID --spec cypress/e2e/regression/forms/basicLayoutForm.cy.js
And access it like this in the .cy.js
file Cypress.env('formID')
You may be able to use the Cypress.spec variable to tell you which spec is running.
For example, at the top of the test:
const spec = Cypress.spec; // { name: "basicLayoutForm.cy.js", ...
const specName = spec.name // basicLayoutForm.cy.js
const formId = specName.split('.')[0] // basicLayoutForm
So even if you run all forms (i.e npx cpress run
without specifying a particular --spec
), each test will still have the formId
available to use.
You could use Cypress Lodash to iterate through an array of IDs.
const ids = [1, 2, 3, 4];
Cypress._.each(ids, (id) => {
describe(`Tests form with id ${id}`, () => {
it('Foo', () => {...});
});
})
You could store those ids in a separate file and import them, or just declare them above the Cypress Lodash command.