Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent/Stub network request #1370

Closed
tschaka1904 opened this issue Feb 23, 2018 · 7 comments · Fixed by #4176
Closed

Prevent/Stub network request #1370

tschaka1904 opened this issue Feb 23, 2018 · 7 comments · Fixed by #4176

Comments

@tschaka1904
Copy link

tschaka1904 commented Feb 23, 2018

Current 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
@benpolinsky
Copy link

benpolinsky commented Feb 23, 2018

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 a beforeEach method.

Edit: although your local scripts may not work with this solution.

@brian-mann
Copy link
Member

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: #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 its src 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.

@tschaka1904
Copy link
Author

tschaka1904 commented Feb 27, 2018

Okay, I would blacklist my <script src="script/I/want/to/stub.js"></script>, which basically returns an object which gets used in the global context. But for some reason, I can't create in my test something what is accessible from the application/window context. How would that work?

@brian-mann
Copy link
Member

brian-mann commented Feb 28, 2018

@tschaka1904 Can you rephrase your question I don't really understand it. Are you asking: "how do I access the application's window from my test?"

You have all kinds of way - you have native access to the window.

If you want to do something before any of your code runs:

Cypress.on("window:before:load", (win) => {
  win.foo = "bar"
})

// or ...

cy.visit("/app", {
  onBeforeLoad: (win) => {
    win.foo = "bar"
  }
})

// or ...

cy.window().then((win) => { win.foo = "bar" })

https://docs.cypress.io/api/events/catalog-of-events.html

@bsmithEG
Copy link

bsmithEG commented May 21, 2018

@brian-mann

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 its src attribute to something you control.

Can you elaborate a little bit more on this approach? I've tried listening to the "window:before:load" event, but when trying to get the document page scripts, it seems i'm getting the cypress window content and not the apps:

    cy.on("window:before:load", win => {
        console.log(win.document.documentElement.outerHTML)
        const scripts = win.document.getElementsByTag("script") // 1
        console.log("scripts => ", scripts.length) // showing 1, which I assume is the singular script element in the output below
    })
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"><head> <script type="text/javascript"> document.domain = 'myappdomain.com'; var Cypress = window.Cypress = parent.Cypress; if (!Cypress){ throw new Error('Something went terribly wrong and we cannot proceed. We expected to find the global Cypress in the parent window but it is missing!. This should never happen and likely is a bug. Please open an issue!'); }; Cypress.action('app:window:before:load', window); </script></head></html>

If I use the onLoad instead, potentially in the cy.visit config options, I get the right dom content, but by then it's too late:

            onLoad(win) {
                console.log("doc => ", win.document.documentElement.outerHTML) // shows complete page
                console.log("elems =>", win.document.getElementsByTag("script") // 54
            }

@jennifer-shehane jennifer-shehane added stage: proposal 💡 No work has been done of this issue existing workaround and removed type: question labels Jan 28, 2020
@cypress-bot cypress-bot bot added stage: needs review The PR code is done & tested, needs review and removed stage: proposal 💡 No work has been done of this issue labels Jun 24, 2020
@cypress-bot cypress-bot bot added stage: work in progress stage: needs review The PR code is done & tested, needs review and removed stage: needs review The PR code is done & tested, needs review stage: work in progress labels Aug 25, 2020
@cypress-bot cypress-bot bot added stage: pending release and removed stage: needs review The PR code is done & tested, needs review labels Aug 31, 2020
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Sep 1, 2020

Released in 5.1.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v5.1.0, please open a new issue.

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Sep 1, 2020
@jennifer-shehane
Copy link
Member

The features requested in this issue are now possible as part of cy.route2().

cy.route2() is currently experimental and requires being enabled by passing "experimentalNetworkStubbing": true through your Cypress configuration. This will eventually be merged in as part of our standard API.

Please see the cy.route2() docs for full details: https://on.cypress.io/route2

If you encounter any issues or unexpected behavior while using cy.route2() we encourage you to open a new issue so that we can work out all the issues before public release. Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants