Search code examples
cypresscypress-intercept

Why cy.wait doesn't handle request, with cy.intercept property entered via variable?


In case, if 'Panasonic' is entered directly in cy.intercept property, it works ok

cy.intercept('GET', '**/part-number/search?q=Panasonic**').as('getPNList')

but in case if this value added via variable into cy.intercept property, it doesn't work and cy.wait falls with timeout. What could be the reason?

let pnName = 'Panasonic'
    cy.intercept('GET', '**/part-number/search?q={@pnName}**').as('getPNList')
    cy.get('.select2-search__field').click().type(pnName)
    cy.wait('@getPNList').then((interception) => {
        expect(interception.response.statusCode).eq(200)
    })

Solution

  • I'm fairly certain your string interpolation is incorrect, and as such, your intercept is just looking for '**/part-number/search?q={@pnName}**' instead of replacing {@pnName} with the variable.

    Try this instead:

    cy.intercept('GET', `**/part-number/search?q=${pnName}**`).as('getPNList')
    

    Check out more about string templates/string interpolation at MDN.