question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can Cypress.io record outgoing network requests and let me query them? (Not trying to stub requests)

See original GitHub issue

Desired 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:closed
  • Created 5 years ago
  • Comments:19 (9 by maintainers)

github_iconTop GitHub Comments

11reactions
jennifer-shehanecommented, Jun 26, 2018

The cy.route() is syntax set up to accept several variations of options, but what you have passed is essentially cy.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 like cy.route(options), so you would write it this way.

cy.route({
  url: 'https://securepubads.g.doubleclick.net/gampad/ads?*',
  onRequest: (req) => {
    cy.log('HELLO!! INSIDE cy.route!');
    expect(true).to.equal(false);
  }
});

Let me know if this works!

3reactions
brian-manncommented, Jun 26, 2018

Try this…

cy.route({
  url: /gampad\/ads\?/,
  onRequest: (req) => {
    debugger
  }
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Network Requests - Cypress Documentation
Within Cypress, you have the ability to choose whether to stub responses or allow them to actually hit your server. You can also...
Read more >
A Practical Guide to Intercepting Network Requests in Cypress
First things first - matching our url · Properly pace a test that is going too fast · Testing your app's API ·...
Read more >
Cypress cy.intercept Problems - Gleb Bahmutov
The command cy.intercept can match requests using a substring, a minimatch, or a regular expression. By default, it intercepts requests matching ...
Read more >
Network Requests with Cypress by Cecelia Martinez - GitNation
We can see the status code. We can see the endpoint as well as how we may or may not be interacting with...
Read more >
Mocking HTTP Calls in Cypress End-to-End Tests
Don't let unreliable test data keep your new features from making it to ... “stub”) those API calls instead of hoping the stars...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found