cypress-io / cypress-example-recipes

Various recipes for testing common scenarios with Cypress

Home Page:https://on.cypress.io/examples

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to have a dynamic number of dynamic tests?

talyh opened this issue · comments

I was really excited when I found
https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/fundamentals__dynamic-tests, as I thought it was going to help me solve a problem I've been struggling with.

I read

generate tests based on the fetched data

and I was sure it was going to help me.

Then I looked at the examples, tried on my end, and no luck.
The core difference between the examples and my reality (and I'd like to believe, a lot of realities where we'd generate tests based on data fetched from an API), is whether the length of the data to iterate over is known ahead of time.
https://github.com/cypress-io/cypress-example-recipes/blob/master/examples/fundamentals__dynamic-tests/cypress/integration/request-spec.js knows how many results the API should return, and is only interested in a subset of them.

In a scenario where a dynamic number can be returned and a test should be generated for each them, this doesn't seem to work as we'd need to write it as Cypress._.range(0, users.length) and, at the point when this is run, users.length is still 0.

Is there a way to generate a dynamic amount of tests based on an api call's result?

Take a look at the linked PR #681 showing how to get the data in the plugin file, then pass to the spec file through Cypress.env() object.