У меня проблемы с перехватом 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')
Вы будете использовать регулярное выражение, соответствующее вашим URL-адресам по окончанию, и вам нужно будет избегать косых черт.
cy.intercept('GET', //client/users/ipuser$/, { fixture: 'ipuser.json' }).as('ipuser')
cy.intercept('GET', //client/users$/, { fixture: 'users.json' }).as('user')
Кажется, есть пара проблем,
у вас есть два перехвата для @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)