16

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')?

Bristow
  • 144
  • 7
Alien
  • 944
  • 2
  • 8
  • 22

4 Answers4

18

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')
jjhelguero
  • 2,281
  • 5
  • 13
11

cy.contains('hello').should('not.exist) isn't going to work if there's more that one occurrence of "hello".

You may prefer to check the actual element instance has been removed from the DOM

cy.contains('hello')
  .then($el => {

    // delete the element

    cy.wrap($el)
      .should($el => {
        // has this element been removed? 
        expect(Cypress.dom.isAttached($el)).to.eq(false)
      })
  })
Fody
  • 23,754
  • 3
  • 20
  • 37
  • the code does not work for me: `cy.getByData('success-msg').contains('falsy').then($el => { cy.wrap($el).should($el => { expect(Cypress.dom.isAttached($el)).to.eq(false) }) })`. It looks for `falsy` and if it is not there which is correct it throws an assertion error with a 4000 ms timeout. Reason is probably the wrong `then` which is not reached. – Timo Aug 23 '22 at 06:16
  • 2
    So Timo, you are obviously not doing the `// delete the element` part, which is kind of the whole point (see question) – user16695029 Sep 29 '22 at 18:29
0

You can use contains with a combination of selector and text. Firstly check it exists and then after deletion check, it doesn't exist.

cy.contains('selector', 'hello').should('exist')
//Actions to perform Deletion
cy.contains('selector', 'hello').should('not.exist')
Alapan Das
  • 17,144
  • 3
  • 29
  • 52
0

I prefer a slightly different syntax to the existing answers:

cy.root().should('not.contain.html', '<b>Fatal error</b>');

here you can use not.contain.html to search for html, or not.contain.text to search for text, for example to test a PHP application, i use

Cypress.Commands.add('visit2', (url, options) => {
    const ret = cy.visit(url, options);
    cy.root()
        .should('not.contain.html', '<b>Fatal error</b>') // <b>Fatal error</b>:  Uncaught ArgumentCountError: strlen() expects exactly 1 argument, 0 given
        .should('not.contain.html', '<b>Warning</b>') // <b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /in/tbUXQ:4) in <b>/in/tbUXQ</b> on line <b>4</b><br />
        .should('not.contain.html', '<b>Notice</b>') // <b>Notice</b>:  Undefined variable: a in <b>/in/tbUXQ</b> on line <b>4</b><br />        cy.should('not.contain', '<b>Parse error</b>'); // <b>Parse error</b>:  syntax error, unexpected '}' in <b>/in/tbUXQ</b> on line <b>4</b><br />
        .should('not.contain.html', '<b>Parse error</b>'); // <b>Parse error</b>:  syntax error, unexpected '}' in <b>/in/tbUXQ</b> on line <b>4</b><br />
    return ret;
});

to detect common-ish PHP application errors

hanshenrik
  • 19,904
  • 4
  • 43
  • 89