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

within followed by should permanently limits scope #4672

Closed
hon2a opened this issue Jul 8, 2019 · 12 comments · Fixed by #8699
Closed

within followed by should permanently limits scope #4672

hon2a opened this issue Jul 8, 2019 · 12 comments · Fixed by #8699
Labels
pkg/driver This is due to an issue in the packages/driver directory type: bug

Comments

@hon2a
Copy link

hon2a commented Jul 8, 2019

Current behavior:

When within is used to find and return some element within some other element and then followed by should to test the returned element, this breaks all subsequent commands (all, not just in the same chain) by permanently setting the root to the aforementioned other element.

In other words, cy.get('#scope').within(() => cy.get(…)).should(…) breaks the rest of the test, because it makes it impossible to find anything outside #scope.

Desired behavior:

cy.get(…).within(() => cy.get(…)).should(…) shouldn't impact subsequent commands at all. E.g. cy.get(…) used after it should search the entire document as usual.

Steps to reproduce: (app code and test code)

<div id="foo"><p>foo</p></div>
cy.root().should('match', 'html') // passes, as it always should

cy.get('#foo')
  .within(() => cy.get('p')) // returns `#foo p`
  .should('match', 'p') // passes, but causes the scope of preceding `within` to become global

cy.root().should('match', 'html') // fails! root is still `div#foo`

Obviously this example is contrived, it's just a minimum reproducible sample.

Versions

Cypress 3.3.2
Chrome 75.0.3770.100 (Official Build) (64-bit)
MacOS 10.14.5 (18F132)

@jennifer-shehane
Copy link
Member

I am able to reproduce this bug with the code below.

  it('within scope', () => {
    cy.visit('https://example.cypress.io/commands/actions')

    cy.root().should('match', 'html') // passes, as it should outside a within

    cy.get('#actions').within(() => {
      cy.get('p') // returns `#foo p`
    }).should('match', 'p') // passes, from docs, should return <div#actions>

    cy.root().should('match', 'html') // fails! root is `div#actions`
  })

@cypress-bot cypress-bot bot added the stage: ready for work The issue is reproducible and in scope label Jul 10, 2019
@jennifer-shehane jennifer-shehane added type: bug pkg/driver This is due to an issue in the packages/driver directory labels Jul 10, 2019
@hon2a hon2a changed the title within followed by should permanently limit scope within followed by should permanently limits scope Jul 10, 2019
@Prashant-Kan
Copy link

Any update on this bug. I am facing this issue in various places.
This is the simplest one, we are facing it everytime. When we do any search and then assert the rows return fro search results using should(have.length,X); and then trying to target next element for further testing, but it finds the new element from inside the returned rows (due to should assertions).

It would be great if we can get work around for this,

@Prashant-Kan
Copy link

@jennifer-shehane : Any news on this issue ?

@gauravmak
Copy link

Was facing the same issue. Using the cy.get() again seems to be resetting the scope.

@spencer-shupe-qhr
Copy link

spencer-shupe-qhr commented Jun 25, 2020

Also running into this, this cost us a couple hours. This is supremely unintuitive. We found that cy.get after the .should did not reset the scope (we were unable to even cy.get('body') until we removed the call to cy.within(..).should(...))

@buklend
Copy link

buklend commented Jul 11, 2020

Had to add command, as a temp solution

Cypress.Commands.add('tempWithin', {
  prevSubject: true
}, (subject, method) => {
  cy.wrap(subject).within(() => method())

  return cy.wrap(subject)
})

@krinhorn
Copy link

krinhorn commented Aug 11, 2020

I'm seeing this in another situation... I am using .each, and Cypress isn't releasing the subject for the next get:

    cy.get('.interest-mosaic').each(($tile) => {
      cy.wrap($tile).invoke('removeAttr', 'style')
      cy.wrap($tile).should('exist').and('not.have.attr', 'style')
    })
    cy.contains('Master Topics').should('be.visible')

This results in Cypress looking for 'Master Topics' WITHIN the previous subject, which is not what I want:
image

Unless I'm using each wrong?

But if I remove the whole block before cy.contains('Master Topics').should('be.visible'), everything runs fine and it looks for 'Master Topics' on the whole page instead of just within the previous subject. Maybe within and each are doing something similar behind the scenes?

@jennifer-shehane
Copy link
Member

@krinhorn You're encountering this issue #4921

@krinhorn
Copy link

@jennifer-shehane Aha! Thank you!

@krysalead
Copy link

krysalead commented Aug 25, 2020

Hi,

I am facing the same issue but with a different pattern.

Cypress.Commands.add("out", { prevSubject: "element" }, (subject) => {
  return cy.get(subject).within(() => {
    return cy.get(".port-AND-OUT");
  });
});
cy.get(".parentA")
        .out()
        .should("exist");
cy.get(".parentB") --> Failed

I manage to fix it with

cy.get(".parentA")
        .out()
        .as("whatever")
        .should("exist");
cy.get(".parentB") --> Worked

@cypress-bot cypress-bot bot added stage: work in progress stage: needs review The PR code is done & tested, needs review and removed stage: ready for work The issue is reproducible and in scope stage: work in progress labels Sep 30, 2020
@cypress-bot cypress-bot bot added stage: pending release and removed stage: needs review The PR code is done & tested, needs review labels Oct 6, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Oct 6, 2020

The code for this is done in cypress-io/cypress#8699, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Oct 14, 2020

Released in 5.4.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v5.4.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Oct 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
pkg/driver This is due to an issue in the packages/driver directory type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants