问题
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:
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 on www.instagram.com
:
来源:https://stackoverflow.com/questions/62213442/cypress-capture-all-requests-cy-route