Search code examples
apicypresscypress-intercept

Cypress - intercept two endpoints in same spec file


I am having trouble intercepting 2 api's in the same spec file. The endpoints are

  1. client/users
  2. client/users/ipuser

Issue: It captures the users response in ipuser json. Can someone please help on how to use regex in the endpoints.

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')        

Solution

  • You will use regex match your urls by the ending and will need to escape the slashes.

    cy.intercept('GET', /\/client\/users\/ipuser$/, { fixture: 'ipuser.json' }).as('ipuser')
    cy.intercept('GET', /\/client\/users$/, { fixture: 'users.json' }).as('user')