Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress: How to visit a url of a different origin?

I'm new to cypress and have ran into an issue. I have my base URL set to the domain I want to test, the issue is when I want to test the ability to login on my base url site I need to verify the user on another site, once I click apply on site number 2 the page on my base url reloads and I would then be able to test the rest of the site.

When I try to visit site 2 from my test I get an error

cy.visit() failed because you are attempting to visit a URL that is of a different origin.

The new URL is considered a different origin because the following parts of the URL are different:

superdomain

You may only cy.visit() same-origin URLs within a single test.

I read this https://docs.cypress.io/guides/guides/web-security.html#Set-chromeWebSecurity-to-false I've tried setting "chromeWebSecurity": false in cypress.json but I still get the same issue (I'm running in chrome)

Is there something I am missing?

like image 275
Doctor Who Avatar asked Jan 25 '23 00:01

Doctor Who


1 Answers

As a temporary but solid work around, I was able to find this script in one of the Cypress Git issue threads (I don't remember where I found it so I can't link back to it)

Add the below to your cypress commands file

Cypress.Commands.add('forceVisit', url => {
    cy.window().then(win => {
        return win.open(url, '_self'); 
      });
});

and in your tests you can call

cy.forceVisit("www.google.com")
like image 80
calcazar Avatar answered Feb 18 '23 21:02

calcazar