Prevent/Stub network request
See original GitHub issueCurrent behavior:
I’ve in my application several <script>...</script>
like pulling in jQuery and also a few internal things. Cypress gets errors that it is not authorised to get certain things. Now this question would be answered (if authorised or not) by an external js file. But this fails, as the “cypress user” is unknown.
Desired behavior:
A way of either overriding or mocking this <script>...</script>
-tag to be able to influence the response.
- Operating System: Mac OS High Sierra
- Cypress Version: 2.0.3
- Browser Version: electron
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Network Requests - Cypress Documentation
With Cypress, you can stub network requests and have it respond instantly ... This prevents the next commands from running until responses come...
Read more >Recommended Way to Stub Out Network Requests When UI ...
As the title suggests, what is Apple's recommended approach to stubbing out network requests when doing UI tests? Making real network requests in...
Read more >How to stub network requests on UITesting - Stack Overflow
You'll have to stub the NSURLSession to inject the response you want, and use some launch arguments to identify when to use the...
Read more >How to Stub External Services in Tests - Thoughtbot
Another approach for preventing external requests is to record a live interaction and 'replay' it back during tests. The VCR gem has a...
Read more >Network Stubbing options for XCTest and XCUITest in Swift
We can still use those libraries to stub out the network requests but ... so that we don't need to explicitly start or...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
This feature is part of part of our large rewrite of the network layer that’s actively being worked on - and you’ll be able to fully stub all network requests including ones for script tags. Read this issue: https://github.com/cypress-io/cypress/issues/687
For today - you could either blacklist these requests, or you could use some combination of
document.currentScript
to get access to the<script>
and then swap itssrc
attribute to something you control.You have native access to everything in Cypress - and you can use that to alter your scripts before they are fetched.
Similar situation, I think:
I ended up blacklisting the host in my cypress.json, and then mocking it during the
window:before:load
event inside of abeforeEach
method.Edit: although your local scripts may not work with this solution.