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 plugin file 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 plugin file start and place the result into
Cypress.env()
object to pass to the specs
In these repo you can find:
- cypress/integration/list-spec.js iterates through an array, creating a test for each item
- cypress/integration/viewports-spec.js runs the same test against different viewports
- cypress/integration/subdomains-spec.js runs the same test against different urls
- cypress/integration/fixture-spec.js uses
require
to load JSON fixture file and generates tests for each item from the fixture. - cypress/integration/request-spec.js fetches a list of users from an external API endpoint and creates a test for each user
- cypress/integration/task-spec.js gets a list of items by calling
cy.task
and then generating a test for each item - cypress/integration/plugin-spec.js gets the list of users on start up, see cypress/plugins/index.js file. You can see the fetched list in the
Cypress.env()
object