Can Cypress.io record outgoing network requests and let me query them? (Not trying to stub requests)
See original GitHub issueDesired behavior:
My use case: I am trying to identify certain outbound network requests on a page, and of the requests that match what I’m looking for, I want to check it string and regex pattern match for certain values to verify the network request is formed correctly.
For example, I need to check all outbound network requests, and any that match https://securepubads.g.doubleclick.net/gampad/ads?*
need to be looped through. In each loop, I need to check if a certain string parameter (via regex or Javascript string methods) in the URL is present.
If I wanted to do this test manually, I have two options. In Chrome, I go to the Network tab in Chrome Dev Tools and examine the network requests I’m looking for using its filter field. Likewise, Charles and Fiddler also record network requests, and I can find the requests I need using its filter field.
I’m trying to find out if Cypress.io can give me all the outbound network calls automatically that match a criteria. I was once told on StackOverflow (https://stackoverflow.com/q/50820829/432089) that I should be able to use
cy.route({
onRequest: (req) => {
// Some code or test here, but they don't work
// I tried cy.log() and expect(true).to.equal(false); and neither has any noticeable effect.
}
});
to automate this. But, when I set up cy.server() and then this cy.route() example up, I don’t see anything inside of this callback executing.
Using the example above, all I can see is that the route to https://securepubads.g.doubleclick.net/gampad/ads?*
is checkmarked, but I see that one of the calls to https://securepubads.g.doubleclick.net/gampad/ads?*
returns 400 by the server from Chrome Dev Tools’ network tab. This 400 error doesn’t happen if I hit the page normally.
It seems cy.route is for stubbing requests and responses. I don’t need to stub any requests or responses. I want an automated way to see and query the network requests going out without modifying them like I would using Chrome Dev Tools, Fiddler, and Charles. Does Cypress.io support this use case currently? I see this ticket (https://github.com/cypress-io/cypress/issues/687) but I don’t know if it would address my use case.
Thank you.
Versions
Cypress 3.0.1, Chrome 67, Mac
Issue Analytics
- State:
- Created 5 years ago
- Comments:19 (9 by maintainers)
Top GitHub Comments
The
cy.route()
is syntax set up to accept several variations of options, but what you have passed is essentiallycy.route(url, options)
, which is not one of the ways that the arguments can be passed. Check the syntax in the docs. You want to pass the arguments likecy.route(options)
, so you would write it this way.Let me know if this works!
Try this…