Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code in before hooks are rerun when navigating to new superdomain #17164

Closed
anmeeks-vt-edu opened this issue Jun 30, 2021 · 6 comments
Closed
Labels
stale no activity on this issue for a long period topic: hooks ↪ type: bug

Comments

@anmeeks-vt-edu
Copy link

anmeeks-vt-edu commented Jun 30, 2021

Current behavior

In a single test script, with a single describe that contains a before(), beforeEach(), and beforeAfter(), the before() method can be invoked multiple times. In this example before() is being invoked before the first test, then again on a test that visits a page with an xhr request loading its main content, then again on the test following that one. Accessing the DOM via cy.get() is required to observe this behavior. Another observed effect is that the console is getting cleared. I'm not relying on a full console log for anything but on the other hand I have no desire for it to be cleared.

It reaches this point
image
Then seems to hiccup
image
Then the GUI seems to get back on track but you look through the console it got cleared just before the Microbiology and Immunology test and re-ran the before() function.
image

Desired behavior

Two outcomes would be better than the current behavior.

  1. Visiting a page that loads some content via xhr should not invoke the before() in the middle of a script; even if the authors of the page being tested had a hair-brained approach for achieving that.
  2. If something is wrong with the page and Cypress cannot process the html the it() should fail so that the Cypress tester (me) knows to go fix something.

Test code to reproduce

Two tests in the middle of this script focus on the page in question. The currently skipped test accesses the returned xhr response directly and results in the desired behavior. The currently not-skipped test demonstrates the undesired behavior. This test accesses a page which makes a call for an xhr resource. The Cypress gui kind of blinks, the console gets cleared, and the before() function is run again but Cypress continues with the test in question. After finishing with the test of a page with the xhr resource, the Cypress gui kind of blinks again, the console gets cleared a second time, and the before() function is run again. No error is logged that I can tell. The GUI log on the left of the test browser appears to indicate that everything ran ok.

describe('demonstrate multiple runs of the before() function in a single test script',() => {

    before(()=>{
        console.log(">ran before all");
    })

    beforeEach(()=>{
        console.log(">>ran before each");
    })

    afterEach(()=>{
        console.log(">>ran after each");
    })

    it("has expected departments", ()=>{
        cy.visit("https://ag.montana.edu/departments/")
        cy.get("#maincontent > ul:nth-of-type(1) > li").should("have.length",8);
    })

    it("Agricultural Economics & Economics",()=>{
        cy.visit("https://www.montana.edu/econ/directory/index.html")
        cy.get(".row.faculty-directory").each((profile)=>{
            //perform some checks
        })
    })
    

    it("Agriculture and Technology Education",()=>{
        cy.visit("https://www.montana.edu/agtecheducation/directory/index.html")
        cy.get(".row.faculty-directory").each((profile)=>{
            //perform some checks
        })
    })


    // ---- The following two tests are the focus of the problem.
    it("Animal & Range Sciences page visit - induces before() and clears console, both before this test and after this test",()=>{
        cy.visit("http://animalrange.montana.edu/directory/faculty/index.html")
        cy.get(".row.faculty-directory").each((profile)=>{
            //perform some checks
        })
    })

    it.skip("Animal & Range Sciences xhr visit - does not induce before() nor clear console",()=>{
        cy.visit("https://www.montana.edu/cpa/cope/page_curator/auto-gen-templates/ajax/index.php?type=ai&version=2&region=maincontent&style=department_tenure&group%5B%5D=&tag%5B%5D=5&custom_tag=0&count=5&pagination=0&search=0&filter_type=&filter_label=&sticky%5B%5D=1524775&include%5B%5D=1524130&exclude%5B%5D=1896378&exclude%5B%5D=1524068&exclude%5B%5D=1687989&only%5B%5D=&=undefined&page=null") 
        cy.get(".row").each((profile)=>{
            //perform some checks
        })
    })
// ---- end of the two tests focusing on the problem

    it("Microbiology & Immunology",()=>{
        cy.visit("https://www.montana.edu/mbi/directory/faculty.html")
        cy.get(".row.faculty-directory").each((profile)=>{
            //perform some checks
        })
    })

    it("Land Resources & Environmental Sciences",()=>{
        cy.visit("https://landresources.montana.edu/directory/faculty/index.html")
        cy.get(".row.faculty-directory").each((profile)=>{
            //perform some checks
        })
    })

    it("Plant Sciences & Plant Pathology",()=>{
        cy.visit("https://plantsciences.montana.edu/directory/faculty/index.html")
        cy.get(".row.faculty-directory").each((profile)=>{
            //perform some checks
        })
    })

})

https://github.com/anmeeks-vt-edu/cypress-test-tiny/blob/master/cypress/integration/spec.js

Versions

All versions of Cypress used to run this:
7.6.0
6.8.0

@jennifer-shehane
Copy link
Member

The before hooks are incorrectly running again when the application under test is navigated to a second superdomain. I thought this was fixed in #7154 but there's clearly some more edge cases.

describe('demonstrate multiple runs of the before() function in a single test script', () => {
  before(() => {
    console.log(">ran before all")
  })

  it("superdomain 1", () => {
    cy.visit("https://ag.montana.edu/departments/")
  })

  it("superdomain 2", () => {
    cy.visit("http://animalrange.montana.edu/directory/faculty/index.html")
  })
})

@jennifer-shehane jennifer-shehane changed the title unexpected additional call of before() when visiting page containing an xhr request Code in before hooks are rerun when navigating to new superdomain Jun 30, 2021
@anmeeks-vt-edu
Copy link
Author

Thank you for the explanation, that helps me understand why it would happen.

@lc-caigwatkin
Copy link

Is this likely to be fixed? We've experienced the same problem.

@cypress-bot cypress-bot bot added stage: backlog and removed stage: ready for work The issue is reproducible and in scope labels Apr 29, 2022
@cypress-app-bot
Copy link
Collaborator

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

@cypress-app-bot cypress-app-bot added the stale no activity on this issue for a long period label May 17, 2023
@cypress-app-bot
Copy link
Collaborator

This issue has been closed due to inactivity.

@cypress-app-bot cypress-app-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 2, 2023
@jennifer-shehane
Copy link
Member

See #17940

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale no activity on this issue for a long period topic: hooks ↪ type: bug
Projects
None yet
Development

No branches or pull requests

4 participants