cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
47.23k stars 3.19k forks source link

`failOnStatusCode + retryOnStatusCodeFailure` has weird behaviour #30330

Open aboqasem opened 1 month ago

aboqasem commented 1 month ago

Current behavior

onBeforeLoad not running and failOnStatusCode + retryOnStatusCodeFailure has weird behaviour.

Desired behavior

1. To be able to run code before page load.

  1. To have correct behaviour for { failOnStatusCode: false, retryOnStatusCodeFailure: false }.

  2. Error message should be fixed.

Test code to reproduce

const BASE_URL = "https://www.cypress.io/";

describe.only("visit", () => {
    //it("onBeforeLoad", () => {
    //  cy.log("set navigator.languages")
    //      .visit("/", {
    //          onBeforeLoad(win) {
    //              Object.defineProperty(win.navigator, "languages", {
    //                  value: ["en", "ar"],
    //              });
    //          },
    //      })
    //      .its("navigator.languages", { timeout: 1000 })
    //      // FAILS, `onBeforeLoad` IS NOT BEING CALLED
    //      .should("deep.equal", ["en", "ar"]);
    //});

    it("failOnStatusCode + retryOnStatusCodeFailure", () => {
        cy.visit(
            `${BASE_URL}/non-existing`,

            // KEEPS RETRYING FOR LONG TIME
            // { failOnStatusCode: false },
            // KEEPS RETRYING FOR LONG TIME, SHOULD NOT RETRY NOR FAIL
            { failOnStatusCode: false, retryOnStatusCodeFailure: false },

            // CypressError: These options are incompatible with each other.
            // - To retry on non-2xx status codes, pass { failOnStatusCode: true, retryOnStatusCodeFailure: true }.
            // - To not retry on non-2xx status codes, pass { failOnStatusCode: true, retryOnStatusCodeFailure: true }.
            // - To fail on non-2xx status codes without retrying (the default behavior), pass { failOnStatusCode: true, retryOnStatusCodeFailure: false }
            // { failOnStatusCode: false, retryOnStatusCodeFailure: true },

            // MAKES THE TEST FAIL
            // {},
            // { failOnStatusCode: true },
            // { failOnStatusCode: true, retryOnStatusCodeFailure: false },
            // { failOnStatusCode: true, retryOnStatusCodeFailure: true },
        )
            .url({ timeout: 1000 })
            .should("eq", `${BASE_URL}/non-existing`);
    });
});

Cypress Version

13.15.0

Node version

18.17.1

Operating System

macOS 14.6.1

Debug Logs

No response

Other

No response

jennifer-shehane commented 1 month ago

@aboqasem I don't believe the navigator.locales is settable here. See the thread about setting locales for testing: https://github.com/cypress-io/cypress/issues/7890

aboqasem commented 1 month ago

Ok thanks. But it's not only locales, the method is not being called at all.

Also I reported two issues here. Any response to the second one?

jennifer-shehane commented 1 month ago

@aboqasem Can you provide an example of it not running at all? It running no code within there?

aboqasem commented 1 month ago

It's the same example but with a console.log("hi") instead of setting locales. The log is ONLY happening when I navigate from within the Cypress app to another URL. The test case alone does NOT produce a log on the website within the Cypress app, which I believe it should be.

    it("onBeforeLoad", () => {
        cy.log("set localStorage")
            .visit("/", {
                onBeforeLoad(win) {
                    win.localStorage.setItem("foo", "bar");
                },
            })
            .its("localStorage.foo", { timeout: 1000 })
            // FAILS, `onBeforeLoad` IS NOT BEING CALLED
            .should("deep.equal", "bar");
    });
jennifer-shehane commented 1 month ago

This code works when visiting our example site with my running on Chrome 129, Cypress 13.14.2. Is the issue isolated to the URL you are visiting? Or a specific browser?

it("onBeforeLoad", () => {
    cy.log("set localStorage")
        .visit("https://example.cypress.io", {
            onBeforeLoad(win) {
                win.localStorage.setItem("foo", "bar");
            },
        })
        .its("localStorage.foo", { timeout: 1000 })
        // FAILS, `onBeforeLoad` IS NOT BEING CALLED
        .should("deep.equal", "bar");
});

Screenshot 2024-10-01 at 11 23 31 AM

aboqasem commented 1 month ago

Ok I need to create a reproducible example. I was trying to do so on Codesandbox but couldn't install required dependencies there. Will get back with more info.

May I know if the other issue regarding retry on failure is a valid issue?

aboqasem commented 1 month ago

Ok I tried again and onBeforeLoad is working, I've probably missed something previously so that it was not running.

But this is still not working:

describe.only("onBeforeLoad and visit failures", () => {
    it.only("failOnStatusCode + retryOnStatusCodeFailure", () => {
        cy.visit(
            "https://www.cypress.io//non-existing",

            // KEEPS RETRYING FOR LONG TIME
            // { failOnStatusCode: false },
            // KEEPS RETRYING FOR LONG TIME, SHOULD NOT RETRY NOR FAIL
            // { failOnStatusCode: false, retryOnStatusCodeFailure: false },

            // CypressError: These options are incompatible with each other.
            // - To retry on non-2xx status codes, pass { failOnStatusCode: true, retryOnStatusCodeFailure: true }.
            // - To not retry on non-2xx status codes, pass { failOnStatusCode: true, retryOnStatusCodeFailure: true }.
            // - To fail on non-2xx status codes without retrying (the default behavior), pass { failOnStatusCode: true, retryOnStatusCodeFailure: false }
            // { failOnStatusCode: false, retryOnStatusCodeFailure: true },

            // MAKES THE TEST FAIL
            // {},
            { failOnStatusCode: true },
            // { failOnStatusCode: true, retryOnStatusCodeFailure: false },
            // { failOnStatusCode: true, retryOnStatusCodeFailure: true },
        )
            .url({ timeout: 1000 })
            .should("eq", "https://www.cypress.io/");
    });
});