I know there is same official limitation to send network request by loop in Cypress, but probably there is some unofficial way to do it.
The use case is to send some cy.request()
and wrap in in for()
or while()
loop and pass different values in the header everytime from some array or directly from the database and then to manipulate on the result by some assert.
e.g.
let query = 'query bla bla bla'
let projectId = some value from array or db';
let result;
describe('Tests', () => {
it('send graphql request to endpoint', () => {
for(let i = 0; 0 > 3; i++) {
cy.request({
method: 'POST',
url: 'https://www.blabla.con/api2',
body: {
'operationName': 'bla bla',
'variables': {
'campaignProjectId': null,
'ids': [ { 'type': 'project', 'id': projectId } ],
'userData': null,
},
query,
},
headers: {
'accept': '*/*',
'content-type': 'application/json',
},
}).then((response: any) => {
// placeholder for assert - will compare between the results
expect(JSON.stringify(response.body.data).is.equal(JSON.stringify(result);
});
};
});
In the code above, it's just looping without to send the request, seems like a recursive issue or something else.
Take a look at @ArtjomProzorov question Cy requests retries.
The same approach can work with some mods. Maybe set the attempts limit to the total number of data items to send, and return instead of throwing an error.
const data = [...]
function req (attempts = 0) {
if (attempts === data.length) return // finished the data set
const dataset = data[attempts]
cy.request(...) // format request from dataset
.then((resp) => {
// handle response
cy.wait(300) // 300 ms delay so as not to slam the server
req(++attempts)
})
}