cypress的断言是基于当下流行的Chai断言库,并且增加了对Sinon-Chai,Chai-jQuery断言库的支持,包括BDD(expect/should)和BDD(assert)格式的断言。

    • 针对长度的断言

      1. //重试,直到找到3个匹配的<li.selectes>
      2. cy.get('li.selected').should('have.length', 3)
    • 针对类的断言

      1. //重试,直到input元素没有类被disabled为止
      2. cy.get('form').find('input').should('not.have.class', 'diasbled')
    • 针对值的断言

      1. // 重试,直到textarea的值为iTestiing
      2. cy.get('textarea').should('have.value', 'iTesting')
    • 针对文本内容的断言

      1. //重试,直到这个spin不包含“click me”字样
      2. cy.get('a').parent('span.help').should('not.contain', 'click me')
    • 针对元素是否可见的断言

      1. // 重试,直到这个button是可见为止
      2. cy.get("button").should('be.visible')
    • 针对元素是否存在的断言

      1. // 重试,直到id为loading的元素不存在
      2. cy.get("#loading").should('not.exist')
    • 针对元素状态的断言

      1. //重试,直到这个radio button 是选中的状态
      2. cy.get('radio').should('be.checked')
    • 针对CSS的断言

      1. // 重试,直到completed这个类有匹配的css为止
      2. cy.get('.completed').should('have.css', 'text-decoration', 'line-through')
    • 针对回调函数的断言

      1. cy.get('div').should(($div) => {
      2. expect($div).to.have.length(1)
      3. const className = $div[0].className
      4. // 检查类名匹配通配符/heading-/
      5. expect(className).to.match(/heading-/)
      6. })