Cypress capture all requests cy.Route()

不羁的心 提交于 2021-01-27 19:30:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!