Cypress capture all requests cy.Route()

Viewed 482

enter image description hereenter image description here

I want to capture all requests, however cy.Route() does not seem to accept wildcards. So, for example, I want to navigate to Reddit" and capture all the requests, however, I also want the code to be reusable so I can navigate to stack overflow and also capture all requests.

Is this possible?

I have tried * wildcard but it does not work

cy.route('*').as('GETS');

cy.route(GET, '*').as('GETS');
1 Answers

Cypress automatically includes minimatch and exposes it as Cypress.minimatch. According to minimatch documentation, you need to use "Globstar" ** matching

Correct ways for all get and post requests:

cy.route('GET', '**').as('gets'); cy.route('POST', '**').as('posts');

Or,

cy.route({
    method: 'GET',
    url: '**'
}).as('gets');


cy.route({
    method: 'POST',
    url: '**'
}).as('posts');

Note: cy.route() should be set before cy.visit(). To read the response use cy.wait('@gets').then and cy.wait('@posts').then

cy.wait('@posts').then((xhr) => {
    cy.log('Intercepted: ' + xhr.url);
    cy.log('Intercepted: ' + JSON.stringify(xhr.response.body));
});

Test on www.google.com:

Test screenshot

Test on www.instagram.com: Test screenshot

Related