Cypress 拦截 - 如何在响应上链接多个断言

时间:2021-07-02 21:31:07

标签: cypress

我刚开始使用新的拦截方法,有一个基本问题,想知道如何在一个测试中链接下面的两个断言。

cy.intercept('GET', '/states').as('states');
cy.reload(true);
// cy.wait('@states').its('response.statusCode').should('eq',200)
cy.wait('@states').its('response.body').should('have.length', 50)

两个断言单独工作。

1 个答案:

答案 0 :(得分:1)

.its('response.statusCode') 传递下来的主题是 statusCode 属性的值,因此您需要再次访问 response 以测试这两个条件

使用闭包使 response 可用于两个断言

cy.wait('@states')
  .its('response')
  .then(response => {
    cy.wrap(response).its('statusCode').should('eq', 200)     
    cy.wrap(response).its('body').should('have.length', 50)
  })

使用回调模式

cy.wait('@states')
  .its('response')
  .should(response => expect(response.statusCode).to.eq(200))   
  .should(response => expect(response.body.length).to.eq(50))

重读别名

cy.wait('@states')                                   // wait for the alias
  .its('response.statusCode').should('eq', 200)

cy.get('@states')                                    // 2nd time use get()
  .its('response.body').should('have.length', 50)