Sometimes you want to iterate through a list of items, and for each item create a separate test. These tests are called dynamic tests. Examples of dynamic tests are:
- run same test against different viewport resolutions
- run same test against different sub-domains of your site
- generate tests based on the fetched data
- get the dynamic data from the
setupNodeEvents
function before the tests start
Sometimes the data to generate tests might be dynamic and come from an external source. You cannot ask for this kind of data from the test itself or from before
hook, since it is too late. There are several workarounds:
- you can fetch data using a script that runs before Cypress starts. Save the results as a JSON file and load data using
require
. This has an advantage that the data does not change while running the tests, making debugging simpler. See suite called "generated from fixture" for an example. - you can fetch the dynamic data before the tests and save as a local variable or context property, then have multiple tests assert against dynamic data items.
- you can get the data to use to create dynamic tests using
cy.task
- you can get the data during the
setupNodeEvents
function start and place the result intoCypress.env()
object to pass to the specs
In these repo you can find:
- cypress/e2e/list-spec.cy.js iterates through an array, creating a test for each item
- cypress/e2e/viewports-spec.cy.js runs the same test against different viewports
- cypress/e2e/subdomains-spec.cy.js runs the same test against different urls
- cypress/e2e/fixture-spec.cy.js uses
require
to load JSON fixture file and generates tests for each item from the fixture. - cypress/e2e/request-spec.cy.js fetches a list of users from an external API endpoint and creates a test for each user
- cypress/e2e/task-spec.cy.js gets a list of items by calling
cy.task
and then generating a test for each item - cypress/e2e/user-spec.cy.js gets the list of users on start up, see the setupNodeEvents function. You can see the fetched list in the
Cypress.env()
object