代码之家  ›  专栏  ›  技术社区  ›  urig

在cypress.io中,如果没有元素,如何在不通过测试的情况下计算元素数?

  •  0
  • urig  · 技术社区  · 5 年前

    使用 cypress.io

    cypress.get(".some-class")

    如果没有具有 class 'some-class' 如果在页面上找到,则测试失败。这是故意的。

    如何使用cypress.io实现这一点?

    0 回复  |  直到 5 年前
        1
  •  2
  •   Yep_It's_Me    5 年前

    你可以使用 length assertion

    cypress.get('.some-class').should('not.have.length', 0);
    
        2
  •  1
  •   KunduK    5 年前

    describe('test check element', function () {
        it('testSelector reload', function () {
          cy.visit('https://docs.cypress.io/api/utilities/$.html#Usage')
          let found = false
          let count=0
          while (!found) {
    
            const nonExistent = Cypress.$('.fake-selector')
    
            if (!nonExistent.length) {
              cy.reload()
              found = false
              count=count+1
              cy.wait(1000)
              if(count==5)
              {
                found = true
                cy.log('Element not found after 5 seconds..Exit from loop!!!')
              }
            } else {
              found = true
            }
          }
        })
      })