lundi 15 juin 2020

Intercepting all Cypress API calls to assert their status

I'm trying to improve the e2e tests we have by adding some assertions for failing API calls being made in the background (checking HTTP status).

Adding something like this at the beginning of the tests works, but it is not optimal because I have no idea how many API calls are being made, and hardcoding a value in the for loop is bad practice.

    cy.server();
    cy.route("**/api/**").as("apiStatusCheck");
    for (let i = 0; i < 20; i++) {
        cy.wait("@apiStatusCheck", { timeout: 30000 }).then((xhr) => {
            if (xhr.status) {
                expect(xhr.status).to.be.lessThan(400);
            }
        });
    }

Does anyone have an idea on how to improve this, by making it dynamic somehow?

Thanks!

Aucun commentaire:

Enregistrer un commentaire