I'm trying to test that all 12 requests involved in creating an object are successful, so far this is the only way I can figure out that works. IS there a better/more efficient/more flexible way that won't break as soon as we add or remove a request?
cy.intercept('/api/**/**').as('objectCreation');
$button.click();
cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
cy.wait('@objectCreation').its('response.statusCode').should('eq', 200);
CodePudding user response:
I'll like more details about your question:
- Are the requests identical?
However, to answer your question on how to simplify your shared, code snippet...you could create a variable
representing the request
creation attempts & keep calling the cy.intercept()
method & asserting its response
until you hit the specified request creation amount like so:
const reqObjCreationNum = 0; // reqObjCreationNum object creation attempts number
/**
* @param {number} reqObjCreationNum - number of object creation attempts
*/
const createObject = ({ reqObjCreationNum }) => {
cy.intercept('/api/**/**').as('objectCreation');
while (reqObjCreationNum <= 12) {
cy.wait('@objectCreation')
.its('response.statusCode')
.should('eq', 200);
reqObjCreationNum = 1;
}
};
createObject({ reqObjCreationNum });
I would suggest the DRY
programming concept as a future reading.