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



我刚开始使用新的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-生成的对象

最新更新