如何在 cypress 中执行 `cy.notContains(text)`?

Ali*_*ien 18 javascript textmatching matcher cypress

我可以检查 cypress 中是否存在文本cy.contains('hello'),但现在我从页面中删除 hello,我想检查 hello 不存在,我该怎么做cy.notContains('hello')

Fod*_*ody 19

cy.contains('hello').should('not.exist)如果出现不止一次“hello”,则不会起作用。

您可能更愿意检查实际的元素实例是否已从 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)
      })
  })
Run Code Online (Sandbox Code Playgroud)

  • 所以蒂莫,你显然没有做“//删除元素”部分,这就是整点(参见问题) (3认同)

jjh*_*ero 15

对于检查“hello”不存在的简单问题,您可以使用.contains('hello')后面的 a .should()。所以整个页面看起来像这样:

// code to delete hello

cy.contains('.selector', 'hello').should('not.exist')
Run Code Online (Sandbox Code Playgroud)

或者您可以进一步将范围缩小到应用程序的特定区域:

// code to delete hello

cy.get('.element-had-hello').should('not.include.text', 'hello')
Run Code Online (Sandbox Code Playgroud)