javascriptcypressmatchertextmatching

How to do `cy.notContains(text)` in cypress?


I can check if text exists in cypress with cy.contains('hello'), but now I delete hello from the page, I want to check hello doesn't exist, how do I do something like cy.notContains('hello')?


Solution

  • For the simple problem of checking 'hello' doesn't exist, you can use .contains('hello') followed a .should(). So it would look something like this for the whole page:

    // code to delete hello
    
    cy.contains('.selector', 'hello').should('not.exist')
    

    Or you can further narrow it down to a particular area of the app:

    // code to delete hello
    
    cy.get('.element-had-hello').should('not.include.text', 'hello')