1

Use Case: I upload 27 images, and check to see whether the image list has those images.

Problem: Whilst the code is in the correct order, the assertion is made before the POST requests, thus failing the test.

Image:

Cypress Screen Grab

Code:

it('Can upload many images and see them', () => {
    cy.get('[datacy="uploadImages"]')
      .selectFile(
        [
            "cypress/fixtures/images/example.jpeg",
            ...
        
        ], { force: true });
    cy.get('.MuiAlert-message').should("be.visible");
    cy.get("[datacy='manageMediaImageList']").find("img").should("have.length", 15);
});
Luke
  • 761
  • 1
  • 18
  • Looks like this has already been asked and answered [Intercept the same API call multiple times in Cypress](https://stackoverflow.com/questions/66765452/intercept-the-same-api-call-multiple-times-in-cypress), but Orlas answer is nice and concise. – user16695029 Feb 18 '23 at 10:34
  • 3
    Does this answer your question? [Intercept the same API call multiple times in Cypress](https://stackoverflow.com/questions/66765452/intercept-the-same-api-call-multiple-times-in-cypress) – user16695029 Feb 18 '23 at 10:34

1 Answers1

4

Use an intercept to catch the POST 27 times, assign an alias, and wait on them.

cy.intercept('POST', url).as('images')
cy.get('[datacy="uploadImages"]')
  ...
Cypress._.times(27, () => cy.wait('@images'))
  ...
cy.get("[datacy='manageMediaImageList']")
  ...
Orla
  • 58
  • 4