Search code examples
cypresscypress-intercept

Cypress intercept() fails when the network call has parameters with '/'


I need to add cy.wait() for some network call which has parameters having forward slashes in it.

eg: http://example.com/myPage1?id=598dccc6&startDate=10/01/2023&endDate=11/01/2023

For this, I've added the following intercept,

cy.intercept('http://example.com/myPage1**').as('myPage1');

However, cy.wait('@myPage1').its('response.statusCode').should('eq',200); is getting timed out and the test case fails.

What should I do?

Thanks.


Reply to @agoff

Somehow this doesn't work. My baseUrl is like http://192.168.43.82/font-end/#/ and api calls are made to http://192.168.43.82/rest/api/myPage with query parameters.

I've tried

cy.intercept(
          {
            pathname:'/rest/api/myPage',
            method:'POST'
        }).as('myPage');

what's wrong with this?


Solution

  • It's possible to catch it with a regex expression for the URL.

    You don't need to specify the base part http://example.com.

    Chars / and ? are operators in regex, so preceed them with \ to indicate the literal character.

    cy.intercept(/\/myPage1\?/).as('myPage1')
    

    Alternatively,

    cy.intercept({pathname: '**/myPage1'}, {}).as('myPage1')
    

    Tested with baseUrl:

    const { defineConfig } = require("cypress");
    
    module.exports = defineConfig({
      e2e: {
        setupNodeEvents(on, config) {
          // implement node event listeners here
        },
        baseUrl: 'http://192.168.43.82/font-end/#/'
      },
    });
    

    and the app fetching

    fetch('http://192.168.43.82/rest/api/myPage1?id=598dccc6&startDate=10/01/2023&endDate=11/01/2023')