cypress: .should('not.exist') is not detecting that element is no longer there
Current behavior
In 10.7.0 the code below is working fine. Once the element disappears from the page, the assertion passes.
Cypress.Commands.add('WaitForObjectWithTextToDisappear', (pageObject: string, text: string): any => {
cy.log('Wait for the object [' + pageObject + 'with text' + text + ' to show up.')
cy.get(pageObject).contains(text).should('exist')
cy.log('Wait for the object [' + pageObject + '] to close.')
cy.get(pageObject).contains(text).should('not.exist')
})
In 12.3.0 the code above is no longer working as expected. Even thou the element is no longer on the page, the assertion fails and reports that it is still there.
Desired behavior
.should(‘not.exist’) works in 12.3.0 the same way as it was in 10.7.0
Test code to reproduce
Cypress.Commands.add('WaitForObjectWithTextToDisappear', (pageObject: string, text: string): any => {
cy.log('Wait for the object [' + pageObject + 'with text' + text + ' to show up.')
cy.get(pageObject).contains(text).should('exist')
cy.log('Wait for the object [' + pageObject + '] to close.')
cy.get(pageObject).contains(text).should('not.exist')
})
Cypress Version
10.3.0
Node version
18.12.1
Operating System
Windows 10
Debug Logs
No elements in the current DOM matched your query:
> cy.get(div[role="dialog"] h5)
at isElement (https://staging.extracker.co/__cypress/runner/cypress_runner.js:151854:70)
at validateType (https://staging.extracker.co/__cypress/runner/cypress_runner.js:151712:14)
at Object.isType (https://staging.extracker.co/__cypress/runner/cypress_runner.js:151743:7)
at <unknown> (https://staging.extracker.co/__cypress/runner/cypress_runner.js:139050:22)
at Object.subjectFn (https://staging.extracker.co/__cypress/runner/cypress_runner.js:149750:16)
at $Cy.verifyUpcomingAssertions (https://staging.extracker.co/__cypress/runner/cypress_runner.js:131027:31)
at onRetry (https://staging.extracker.co/__cypress/runner/cypress_runner.js:149741:15)
at tryCatcher (https://staging.extracker.co/__cypress/runner/cypress_runner.js:8914:23)
at Promise.attempt.Promise.try (https://staging.extracker.co/__cypress/runner/cypress_runner.js:6188:29)
at whenStable (https://staging.extracker.co/__cypress/runner/cypress_runner.js:146641:65)
at <unknown> (https://staging.extracker.co/__cypress/runner/cypress_runner.js:146082:14)
at tryCatcher (https://staging.extracker.co/__cypress/runner/cypress_runner.js:8914:23)
at Promise._settlePromiseFromHandler (https://staging.extracker.co/__cypress/runner/cypress_runner.js:6849:31)
at Promise._settlePromise (https://staging.extracker.co/__cypress/runner/cypress_runner.js:6906:18)
at Promise._settlePromise0 (https://staging.extracker.co/__cypress/runner/cypress_runner.js:6951:10)
at Promise._settlePromises (https://staging.extracker.co/__cypress/runner/cypress_runner.js:7031:18)
at Promise._fulfill (https://staging.extracker.co/__cypress/runner/cypress_runner.js:6975:18)
at <unknown> (https://staging.extracker.co/__cypress/runner/cypress_runner.js:8589:46)
From Your Spec Code:
at Context.eval (webpack:///./cypress/support/commands-object-actions.ts:256:36)
Other
About this issue
- Original URL
- State: open
- Created a year ago
- Reactions: 6
- Comments: 16 (6 by maintainers)
@nagash77 Unfortunately, I cannot provide you with a simpler example. As you can see I am not the only one facing the issue. LINK
The behavior is not consistent with Cypress’s documentation.
Expected Behavior: If an element that contains text, exists in the DOM and the following command is executed: cy.get(“objectLocator]”).contains(“TEXT”).should(‘not.exist’); If the element disappears from the DOM within the set command timeout in cypress.config.ts the assertion should(not.exist) should pass.
Actual Behavior: If an element that contains text, exists in the DOM and the following command is executed: cy.get(“objectLocator]”).contains(“TEXT”).should(‘not.exist’); If the element disappears from the DOM within the set command timeout in cypress.config.ts the assertion should(not.exist) fails.
Hi, Experiencing almost the same issue. In my tests i’m trying to assert than an element does not exist on page
cy.get('wahtever').should('no.exist')fails for all tests once upgrading to 12.x When downgraded to 11.3.0 same tests passedHi @nagash77 , I did some more testing between 10.7.0 and 12.3.0
The problem seems to be only with the last part of my command, where I am trying to check if an object with text does not exist in the DOM.
cy.get(pageObject).contains(text).should(‘not.exist’)
Again, this is working in the 10.7.0 version. But in 12.3.0 it fails. My scenario is that a message appears on the screen for about 5 seconds. I would like to check when the message is no longer in the DOM, so I can do my next action. If I break it down like this: cy.get(pageObject) cy.get(pageObject).contains(text) cy.get(pageObject).contains(text).should(‘not.exist’)
The first two rows pass because the message is still on the page. But the last row is the one that fails to detect that the message is no longer in the DOM. It is executed while the message is still on the screen. My defaultwaitoncommand is 10 seconds and the message is on the screen 5 sec. So once the message is closed, the assertion should pass.
If I do the check without the contains() part, then cypress successfully detects that the message is no longer in the DOM. So this code works:
cy.get(pageObject).should(‘not.exist’)
this code works too:
cy.contains(pageObject, text).should(‘not.exist’)
but this does not:
cy.get(pageObject).contains(text).should(‘not.exist’)
This is what does not make sense to me. Why is the contains() breaking the assertion, when used between get() and should()?