Search code examples
cypresscypress-intercept

How to intercept a specific URL with wildcards


I have an app, two different URLs are fetched. Part of the URL is a hash which needs wildcard pattern, and I want to capture just one URL in an intercept.

But the similarity of the string makes it difficult to get a pattern that works.

/api/v1/payment/duedate?type=payment&cache_buster=...
/api/v1/payment/6309503a5c058a702224?cache_buster=...   // capture this one

I tried

cy.intercept('/api/v1/payment/*?cache_buster')

It seems I need to negate specific parts of pathname or query params, but it does not seem possible to do so.


Solution

  • You can indeed negate a section of the URL, but not in the query parameter parts.

    This will select any URL with /payment/* but exclude the one with /payment/duedate.

    cy.intercept('/api/v1/payment/!(duedate*)')
    

    You could also try a regex, or use javascript code in a routeHandler callback.