我刚开始使用新的intercept方法,有一个基本问题,想知道如何在一个测试中链接下面的两个断言。
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)
这两个断言分别起作用。
从.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)
像符咒一样工作:
cy.intercept("POST", "/graphql").as("someInterception");
cy.wait("@someInterception").then(({ request, response }) => {
expect(response.body.data.someField).to.be.true;
expect(request.body.variables.someField).to.be.true;
});
请参阅:https://docs.cypress.io/api/commands/intercept#Using-生成的对象