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

 

在 跑一个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.statusCodeassert.isNotNull(interception.request.body, 'requestbody is not null')expect(rescode).to.equal(200)})//You can chain .its() and .should() to assert against request/response cycles:
cy.wait(@exampleApi).its('response.statusCode').should('eq',200)
cy.wait(@exampleApi).its('interception.response.body').should('include','newfunds')

Stubbing a response

// requests to '/update' will be fulfilled with a body of "success"
cy.intercept('/update', 'success')// requests to '/users.json' will be fulfilled
// with the contents of the "users.json" fixture
cy.intercept('/users.json', { fixture: 'users.json' })A StaticResponse object represents a response to an HTTP request, and can be used to stub routes:
const staticResponse = {/* some StaticResponse properties here... */
}cy.intercept('/projects', staticResponse)Stub headers, status code, and body all at once:
cy.intercept('/not-found', {statusCode: 404,body: '404 Not Found!',headers: {'x-not-found': 'true',},
})
//Adding a header to an outgoing request and waiting on the intercept
cy.intercept('/req-headers', (req) => {req.headers['x-custom-headers'] = 'added by cy.intercept'
}).as('headers')// the application makes the call ...
// confirm the custom header was added
cy.wait('@headers').its('request.headers').should('have.property', 'x-custom-headers', 'added by cy.intercept')//You can add, modify or delete a header to all outgoing requests using a beforeEach() in the cypress/support/index.js file
// Code from Real World App (RWA)
// cypress/support/index.ts
import './commands'beforeEach(() => {cy.intercept({ url: 'http://localhost:3001/**', middleware: true },// Delete 'if-none-match' header from all outgoing requests(req) => delete req.headers['if-none-match'])
})

Controlling the response

The intercepted request passed to the route handler (hereafter referred to as req, though you can use any name) contains methods to dynamically control the response to a request:

  • req.reply() - stub out a response requiring no dependency on a real back-end
  • req.continue() - modify or make assertions on the real response
  • req.destroy() - destroy the request and respond with a network error
  • req.redirect() - respond to the request with a redirect to a specified location
  • req.on() - modify the response by attaching to events

 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部