Cypress: Test if element does not exist

前端 未结 7 2141
一个人的身影
一个人的身影 2020-12-09 14:42

I want to be able to click on a check box and test that an element is no longer in the DOM in Cypress. Can someone suggest how you do it?

//This is the Test          


        
相关标签:
7条回答
  • 2020-12-09 14:47

    you can also search a for a text which is not supposed to exist:

    cy.contains('test_invite_member@gmail.com').should('not.exist')
    

    Here you have the result in Cypress: 0 matched elements

    documentation: https://docs.cypress.io/guides/references/assertions.html#Existence

    0 讨论(0)
  • 2020-12-09 14:52

    You can also use below code

    expect(opportunitynametext.include("Addon")).to.be.false
    

    or

    should('be.not.be.visible')
    

    or

    should('have.attr','minlength','2')
    
    0 讨论(0)
  • 2020-12-09 14:57
    cy.get('[data-e2e="create-entity-field-relation-contact-name"]').should('not.exist');
    

    might lead to some false results, as some error messages get hidden. It might be better to use

    .should('not.visible');
    

    in that case.

    0 讨论(0)
  • 2020-12-09 15:00

    Cypress 6.x Migration

    Use .should('not.exist') to assert that an element does not exist in the DOM.


    Do not use not.visible assertion. It would falsely pass in < 6.0, but properly fail now:

    // for element that was removed from the DOM
    // assertions below pass in < 6.0, but properly fail in 6.0+
    .should('not.be.visible')
    .should('not.contain', 'Text')
    

    Migration Docs: https://docs.cypress.io/guides/references/migration-guide.html#Migrating-to-Cypress-6-0

    0 讨论(0)
  • 2020-12-09 15:05

    Well this seems to work, so it tells me I have some more to learn about .should()

    cy.get('.check-box-sub-text').should('not.exist');
    
    0 讨论(0)
  • 2020-12-09 15:06

    Here's what worked for me:

    cy.get('[data-cy=parent]').should('not.have.descendants', 'img')
    

    I check that some <div data-cy="parent"> has no images inside. Regarding original question, you can set data-cy="something, i.e. child" attribute on inner nodes and use this assertion:

    cy.get('[data-cy=parent]').should('not.have.descendants', '[data-cy=child]')
    
    0 讨论(0)
提交回复
热议问题