Cypress里面断言常用的有should, expect

.should()

  • should(‘have.class’, ‘success’) 断言元素的class属性值是 ‘success’
  • should(‘have.text’, ‘Column content’) 断言元素文本值 ‘Column content’
  • should(‘contain’, ‘Column content’) 断言元素文本包含 ‘Column content’
  • should(‘have.html’, ‘Column content’) 断言元素html文本’Column content’

同一元素多个断言用.and()

cy.get('.assertions-link')
  .should('have.class', 'active')
  .and('have.attr', 'href')
  .and('include', 'cypress.io')

自己封装断言
1,在项目下创建common文件夹,放置公共方法,比如单元,比如url维护
2,封装自己专属断言
3,在断言时引入使用

注: 也可以封装在commond.js中成为底层方法(不建议,会导致commond.js文件中方法太多,因为还要封装一些个性化操作)

/*!
 * 描述:断言table中的行数是否与预期相等
 * table_ele: 传入table层级的元素路劲
 * expect_value: 期望值
 */
function assertTableRowLen(table_ele ,expect_value){
    cy.get(table_ele).get("tbody > tr").should("have.length",expect_value)
}

/*!
 * 描述:断言一些报错的提示信息
 * text: 报错内容
 */
function assertAlert(text){
    cy.contains(text)
}

export default {
    assertTableRowLen,
    assertAlert
}
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐