我在同一个规范文件中截取2个api有困难。端点是
问题:它捕获了ipuser json中的用户响应。有人能帮助如何在端点中使用regex吗?
cy.intercept('GET', '/client/users/ip_user', { fixture: 'ipuser.json' }).as('ipuser')
cy.intercept('GET', '/client/users', { fixture: 'users.json' }).as(
  'user'
)
cy.wait('@ipuser').then((interception) => {
    interception.response.body.data.attributes.limit = 10000
    interception.response.body.data.attributes.amount = 10000
    cy.log(JSON.stringify(interception.response.body))
    cy.writeFile(filename, JSON.stringify(interception.response.body))
 )
 cy.intercept('GET', '/client/users/ip_user', {
   fixture: 'ipuser.json',
 }).as('ipuser')        发布于 2022-10-06 22:16:38
您将使用regex匹配您的urls的结尾,并将需要转义斜杠。
cy.intercept('GET', /\/client\/users\/ipuser$/, { fixture: 'ipuser.json' }).as('ipuser')
cy.intercept('GET', /\/client\/users$/, { fixture: 'users.json' }).as('user')发布于 2022-10-06 22:29:52
似乎有一些问题,
@ipuser的
ipuser.json,则需要在截取.中动态分配它。
假设触发请求的是cy.visit('/'),它应该是这样的
// set up both intercepts at the top of the test
// the more specific URL (/client/users/ip_user) should go last
cy.intercept('GET', '/client/users', {fixture: 'users.json'}).as('user')
cy.intercept('GET', '/client/users/ip_user', req => {
  req.reply({fixture: 'ipuser.json'})                  // responds after the fixture is written
}).as('ipuser')  
// trigger the fetches
cy.visit('/')
// wait on the 1st - presume it creates the fixture for the second
const filename = './cypress/fixtures/ipuser.json'
cy.wait('@user').then((interception) => {
  interception.response.body.data.attributes.limit = 10_000
  interception.response.body.data.attributes.amount = 10_000
  cy.writeFile(filename, interception.response.body)  // JSON.stringify not needed
})
// wait on the 2nd and check it's result
cy.wait('@ipuser')
  .its('response.body')
  .should('have.property', 'data')
  .should('have.property', 'attributes')
  .should('have.property', 'limit', 10_000)https://stackoverflow.com/questions/73979952
复制相似问题