Search code examples
javascriptjquerytestingcypresse2e-testing

Cypress intercept for dynamic pathParam in api and * functionality


I have few API's that i need to intercept.

'randomApi/import/entity'
'randomApi/import/entity/lead1/tabs'
'randomApi/import/entity/lead2/tabs'

lead1 and lead2 are values that are coming in the 1st api, so the values can be anything.

I was able to intercept the 1st one but not getting a proper solution for second and third one.

cy.intercept({
            pathname: '/randomApi/import/entity'
        }).as("entitiesData")

Also can anyone explain how does * and ** work in these

1. cy.intercept({ pathname: '/randomApi/import/entity*'}).as("entitiesData")
2. cy.intercept({ pathname: '/randomApi/import/entity**'}).as("entitiesData")
3. cy.intercept({ pathname: '/randomApi/import/entity/*'}).as("entitiesData")
4. cy.intercept({ pathname: '/randomApi/import/entity/**'}).as("entitiesData")

Solution

  • The wildcards go between path separators. Try this:

    cy.intercept('**/randomApi/import/entity/*/tabs', {}).as('leads')
    
    cy.window().then(win => {
      win.fetch('https://my-host/randomApi/import/entity/lead1/tabs')
    })
    
    cy.wait('@leads')   // passes
    

    Double stars asterisks are for multiple path sections, for example

    cy.intercept('**/randomApi/import/entity/**/tabs', {}).as('leads')
    
    cy.window().then(win => {
      win.fetch('https://my-host/randomApi/import/entity/leads/lead1/tabs')
    })
    
    cy.wait('@leads')   // passes