我正在使用Cypress测试一个应用程序,测试结果与真实的HTTP服务器进行了对比。我不会截断HTTP请求。
如果任何HTTP请求失败,有没有办法让我的测试失败?
在this other SO post中有一个看起来不错的解决方案,但我想知道是否有更合适的解决方案。在我的例子中,我并不总是将所有的HTTP错误转换为对console.error的调用。
发布于 2021-01-21 19:26:42
您可以使用cy.intercept()监听请求&检查状态码等。
参考:https://docs.cypress.io/api/commands/intercept.html#Intercepting-a-response
示例1:
// Wait for intercepted HTTP request
cy.intercept('POST', '/users').as('createUser')
// ...
cy.wait('@createUser')
.then(({ request, response }) => {
expect(response.statusCode).to.eq(200)
})示例2:
// Listen to GET to comments/1
cy.intercept('GET', '**/comments/*').as('getComment')
// we have code that gets a comment when
// the button is clicked in scripts.js
cy.get('.network-btn').click()
// https://on.cypress.io/wait
cy.wait('@getComment').its('response.statusCode').should('be.oneOf', [200, 304])https://stackoverflow.com/questions/65817278
复制相似问题