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.

Some Calls to cy.visit() Fail with a Parse Error when experimentalNetworkStubbing is Turned On

See original GitHub issue

Current behavior:

Some (but not all) of my calls cy.visit are failing with a generic Parse Error when I have experimentalNetworkStubbing turned on. One such failing call is in the provided below.

Desired behavior:

Calls to cy.visit succeed regardless of whether or not experimentalNetworkStubbing is on.

Test code to reproduce:

it('Issue with cy.visit when experimentalNetworkStubbing is On', function () {
  // This test will pass when experimentalNetworkStubbing is off, but will fail when it is on.
  cy.visit('https://secure.vidyard.com/user/sign_in')
})

image

Versions:

Cypress version 5.1.0 on macOS 10.14.6 using Chrome 85.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
rbell-mfjcommented, Sep 29, 2020

I ran into this issue as well. Building on the workaround from @alim16, I added the following to cypress/support/commands.js. This should “fix” the built-in visit command to work around the issue so you don’t have to change any of your tests.

Caveats:

  • It assumes your server can handle HTTP HEAD requests (if not you can just change the method to ‘GET’)
  • It will apply to all visit calls, so it may slow things down a bit and/or have unintended side effects on tests that don’t experience the redirect issue at all
  • EDIT: It only follows a single redirect, as written, per visit command
/**
 * Temporary patch for cy.visit() to address redirects causing errors with experimentalNetworkStubbing
 * See https://github.com/cypress-io/cypress/issues/8497
 */
Cypress.Commands.overwrite('visit', (originalFn, url, options) => {
  const timeout = { timeout: options?.timeout || Cypress.config('pageLoadTimeout') }
  Cypress.log({ name: 'visit', message: url })

  cy.request({ url, followRedirect: false, method: 'HEAD', log: false })
    .then(timeout, resp => {
      if ([301, 302, 303, 307, 308].includes(resp.status)) {
        url = resp.headers['location']
        Cypress.log({ name: 'visit', displayName: ' ', message: 'redirected to ' + url })
      }
      return originalFn(url, Object.assign({}, options, { log: false }))
    })
})
1reaction
alim16commented, Sep 21, 2020

in case anyone is looking for a temporary workaround, here is mine I use cy.request to find the redirect url and then cy.visit

//my function looks like this

export const loadAppWithRedirect = (path:string) => cy.request({
  url: path,
  followRedirect: true
}).then((resp) => {
  const urlRegex = /(https?:\/\/[^ ]*)/
  //ts-ignore because ts claims"redirects" does not exist, but it does for me
  //@ts-ignore
  const stringWithRedirectedUrl = resp.redirects[resp.redirects.length - 1] //get the last link in the redirect chain
  const url = stringWithRedirectedUrl.match(urlRegex)[1].toString()
  cy.visit(url)
})

//and my test would look like this

   it("should go to my site even if it redirects", () => {
        loadAppWithRedirect('https://secure.vidyard.com/user/sign_in') 
        cy.contains("Welcome back").should('exist')
    })

@todd-m-kemp I needed an example url and can’t use mine, I hope you don’t mind

my experience with cypress is limited so if anyone has better/easier way, please let me know

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error Messages
cy.visit() failed because you are attempting to visit a different origin domain. Note. This error only pertains to Cypress version v11.0.0 ...
Read more >
Cypress - cy.visit() failed trying to load
Maybe someone had the same issue we were facing which is that the cypress.json file was not in the root. We had it...
Read more >
1000 Foot Overview of Writing Cypress Tests #frontend ...
To see if an element contains or does not contain some text, ... An example of making a cy.request() call is demonstrated here....
Read more >
cypress: Versions
Previously the command would incorrectly fail with a syntax error. ... When experimentalNetworkStubbing is enabled, using cy.visit() to URLs that redirect ...
Read more >
Cypress cy.intercept Problems
Thus, it makes the call right at the end of cy.visit command. ... cy.intercept happens in the Command Log was a user experience...
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