-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
cy.contains() reruns previous commands when re-querying an alias #7413
Comments
I can recreate this with the code below. It seems it's just not recognizing the
it('cy.contains()', () => {
cy.visit('index.html');
// removing this line changes the behavior of the test
cy.get('h1')
cy.contains('Current').as('counter')
cy.get('@counter').should('match', 'div')
// this causes the <div> to be recreated and also removes H1
cy.contains('button', 'Update DOM').click()
// fails trying to get 'h1'
cy.get('@counter').should('contain', 'Current 1')
})
<html>
<body>
<h1 id="h1">Hello</h1>
<div id="div">Current</div>
<button id="button">Update DOM</button>
</body>
<script>
document.getElementById('button').onclick = () => {
const newText = document.createTextNode('Current')
const newDiv = document.createElement('div').appendChild(newText)
document.getElementById('div').remove()
document.body.appendChild(newDiv)
document.getElementById('h1').remove()
}
</script>
</html> With
|
We ran into the same issue and our test is structurally identical to the one in #7413 (comment). This might be obvious to others but a quick workaround is to do |
Good workaround, yeah. I'm going to close this issue, since the resolution to #7306 (coming out in Cypress 12) is basically redoing how commands chain together, and the internal logic around re-querying the DOM is pretty different. See my comments in that issue for a more complete explanation of the changes coming in Cypress 12 in this regard. |
Released in This comment thread has been locked. If you are still experiencing this issue after upgrading to |
Current behavior:
Preconditions:
cy.contains()
invoked off thecy
object and saved as@alias
If the test tries to retrieve the element using
cy.get('@alias')
, the whole chain is executed again as expected (starting withcy.contains()
). Thecy.contains()
works fine and finds the element in the whole document.However, if there were some querying commands before
cy.contains()
, they seem to interfere with the alias querying procedure. For instance, the alias query fails if the query command preceding tocontains
does not succeed. Please see the reproduction example for more clarityDesired behavior:
When invoked off the
cy
object, thecontains
command should not depend in any way on the precedent command.Test code to reproduce
Reproduction repo:
https://github.com/kokovtsev/cypress-repro-cy.contains/blob/master/cypress/integration/examples/contains.spec.js
Application under test:
https://codesandbox.io/s/gracious-newton-q4913?file=/src/App.js
Versions
Windows 10 x64, Cypress 4.5.0, Electron 80/Chrome 81/Firefox 76
See also
Might be related to:
.each
changes scope for next dual-command (likecy.contains()
).each
changes scope for next dual-command (likecy.contains()
) #4921The text was updated successfully, but these errors were encountered: