I have around 7 test files and in total 60 test cases that needs to be run for 2 URLs.
Way 1 is: loop on each test - so each test will run for 2 URLs and URL I am passing as argument. But that requires forEach in each test.
Way 2: I tried opening URL in beforeEach() like below:
describe('Your Finance Page tests', () => {
beforeEach(() => {
brands.forEach(function (brand) {
openUrl(brand)
});
});
It is opening brand for each test but running test for 1 brand only. Like before each test it opens brand A, then brand B and run test on brand B only.
Is there any better way to do this other than writing forEach in each test?
The for-loop will probably work if it's external to the beforeEach()
and the it()
blocks.
This article Dynamic Tests From Cypress Fixture describes the pattern in detail.
const brandUrls = [...]
describe('Test each brand', () => {
brandUrls.forEach(brandUrl => {
beforeEach(() => {
cy.visit(brandUrl)
})
it(`tests url ${brandUrl}`, () => {
...
})
})
})
Note, your brands must be either hard-coded or read from a fixture using require()
or import
at the top of the spec.
As for applying it to multiples specs, yes add it to each spec (for simplicity).
cypress-each package
The cypress-each package does something similar, but has expanded capabilities.
import 'cypress-each'
// create a separate test for each brand
const brands = [...]
it.each(brands)('test %s brand, (brand) => {
cy.visit(brand)
... // assertions
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.