gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:28:54 29 4
gpt4 key购买 nike

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

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)

关于 Cypress 拦截 - 如何在响应上链接多个断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68231414/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com