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

问题描述 投票:0回答:4

我可以用

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

javascript cypress matcher textmatching
4个回答
12
投票

对于简单的检查'hello'的问题不存在,你可以使用

.contains('hello')
.should()
。所以整个页面看起来像这样:

// code to delete hello

cy.contains('.selector', 'hello').should('not.exist')

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

// code to delete hello

cy.get('.element-had-hello').should('not.include.text', 'hello')

4
投票
如果不止一次出现“你好”,

cy.contains('hello').should('not.exist)
将无法正常工作。

您可能更愿意检查实际元素实例是否已从 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)
      })
  })

0
投票

您可以将

contains
与选择器和文本组合使用。首先检查它存在,然后删除检查后,它不存在。

cy.contains('selector', 'hello').should('exist')
//Actions to perform Deletion
cy.contains('selector', 'hello').should('not.exist')

0
投票

我更喜欢与现有答案略有不同的语法:

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

在这里你可以使用

not.contain.html
搜索html,或
not.contain.text
搜索文本,例如测试PHP应用程序,我使用

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;
});

检测常见的 PHP 应用程序错误

© www.soinside.com 2019 - 2024. All rights reserved.