I'm trying to come up with a generic method for checking in my Cypress tests that the current page doesn't have some text.
Basically something like the opposite to cy.contains('Foo')
- Foo must be missing and that's what is expected.
In other similar question the following method was suggested:
cy.contains('Foo').should('not.exist')
It seems to work for removed elements, not for elements that should not exist at all.
If possible I would like this solution to be as generic as possible because HTML class/XPath is unknown in my use case therefore I can not use
cy.get('.foo-container').should('not.exist');
Perhaps this could be some kind of conditional?
In pseudocode:
if (cy.contains('Foo')) {
throw new Error('Foo must be absent instead')
}
Failing test:
cy.visit('https://example.cypress.io/commands/actions')
cy.contains('lalafo').should('not.exists') //AssertionError Timed out retrying after 4000ms: Expected to find content: 'lalafo' but never did.
Cypress version: 6.3.0
question from:
https://stackoverflow.com/questions/65863092/cypress-fail-if-page-doesnt-contain-text 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…