我在拦截同一规范文件中的 2 个 api 时遇到问题。端点是
问题:它捕获 ipuser json 中的用户响应。有人可以帮助了解如何在端点中使用正则表达式吗?
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')
似乎有几个问题,
您有两个拦截
@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)
您将使用正则表达式匹配您的网址结尾,并且需要转义斜杠。
cy.intercept('GET', /\/client\/users\/ipuser$/, { fixture: 'ipuser.json' }).as('ipuser')
cy.intercept('GET', /\/client\/users$/, { fixture: 'users.json' }).as('user')