How to do `cy.notContains(text)` in cypress? [duplicate]
Asked Answered
A

4

18

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

Arabinose answered 24/1, 2022 at 22:4 Comment(0)
S
14

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')
Selfconfidence answered 24/1, 2022 at 23:25 Comment(0)
R
21

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)
      })
  })
Roundshouldered answered 25/1, 2022 at 2:18 Comment(2)
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.Callen
So Timo, you are obviously not doing the // delete the element part, which is kind of the whole point (see question)Bareheaded
S
14

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')
Selfconfidence answered 24/1, 2022 at 23:25 Comment(0)
R
1

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

Rambo answered 29/9, 2022 at 12:6 Comment(2)
Any reason why testing html is better - does not seem to address the issue any differently.Bareheaded
Nice idea, this simpler syntax works perfectly for me: cy.should('not.contain', 'Some text that shouldn't be on the page')Ennui
A
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')
Alunite answered 25/1, 2022 at 5:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.