cypress 通过 intercept 获取 request 和 response 数据进行校验

本文介绍了如何在Cypress中利用`cy.intercept`命令来捕获和验证请求及响应数据,确保在UI测试的同时进行API测试。通过route handler,可以实现模拟响应、控制实际响应、制造错误、重定向和监听事件等操作,增强测试的全面性和准确性。
摘要由CSDN通过智能技术生成

 

在 跑一个cypress 进行ui 操作的用例时, 也可以观察到整个过程触发的API ,但是如果不加验证点的话,如果API 报错了,case还是会当成pass的,所以在进行ui测试的时候

,同时也可以做API测试。cypress也提供了cy. intercept 供使用。https://docs.cypress.io/api/commands/intercept#Syntax

// spying, dynamic stubbing, request modification, etc.
cy.intercept('/users*', { hostname: 'localhost' }, (req) => {
  /* do something with request and/or response */
}).as('exampleApi')
// once a request to api responds, this 'cy.wait' will resolve
cy.wait(@exampleApi).then((interception) => {
            let newfundsValue = interception.response.body.newfunds;
            var rescode = interception.response.statusCode
       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值