cypress-each icon indicating copy to clipboard operation
cypress-each copied to clipboard

Read from array of objects

Open chaussen opened this issue 2 years ago • 4 comments

Hi I am reading test data from json fixture with typescript typing like this:

import { sites } from '../../../fixtures/api/sites.json'
...
let tests: SiteAccessTestData = sites;  // to specify the type of sites
...
it.each(tests)(testTitle, testElementSelector);

Got this error > Do not know how to create tests from the values array / object. See DevTools console

When I directly do it.each(sites)(testTitle, testElementSelector);

It works. Is there a way to force sites to satisfy the type definition SiteAccessTestData while able to use it in cypress-each?

chaussen avatar Mar 13 '23 23:03 chaussen

Can you provide a public repo showing the problem? If there is none, can you make one with just the issue?

bahmutov avatar May 08 '23 16:05 bahmutov

Hi @bahmutov ,

I am running into this same error and I can recreate this error with the object below. Here is what I am looking at:

I am retrieving a search response from a server via cy.request, the response looks like this:

[{ "Index": 1, "Uri": "/address1" "Fields": {} "Matches": [] }, { "Index": 2, "Uri": "/address2" "Fields": {} "Matches": [] }, { "Index": 3, "Uri": "/address3" "Fields": {} "Matches": [] }]

When I print out this response directly with cy.log I see Object{8}, Object{8}, Object{8} in the Cypress runner (3 responses).

However, if I wrap the response with cy.wrap and print it out with cy.log I will see [Object{8}, Object{8}, Object{8}]. If I pass this wrapped response to it.each I see the error 'Do not know how to create tests from the values'.

I am trying to use this wrapped method since my plan is to have all the steps needed to get the server response in a before block and then use it.each to create tests from that response.

How can I get it.each to recognize the wrapped response?

ryan-g2 avatar Jun 05 '24 18:06 ryan-g2

@ryan-g2 you cannot create tests dynamically (from a cy.request response for example), the data should be static, and all tests should exists before hooks start running. You could make a request from the cypress config file and then pass the object to the spec via Cypress.env object, for example

bahmutov avatar Jun 05 '24 19:06 bahmutov

Oh, so what I am trying to do is not possible? That's unfortunate.

Thanks for the answer and for the idea about making the request in the config file. The steps to get this search result are a bit lengthy so I worry that if I put them in the cypress config file they will be run each time cypress starts. Would this happen?

Also, do you have a link to an example of using a cy.request inside of the config file?

ryan-g2 avatar Jun 05 '24 19:06 ryan-g2