art
art

Reputation: 1412

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 timing out and the test case fails.

Upvotes: 6

Views: 638

Answers (1)

Paolo
Paolo

Reputation: 5461

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

Upvotes: 11

Related Questions