Cypress, how do you visit two different domains for a single test?

Viewed 1000

I have the following:

describe('Page not authenticated', () => {
    it('Page has authentication', () => {
        cy.visit('https://myauthsite.com/logout');
        cy.visit('/', { failOnStatusCode: true });
        cy.url().should('include', 'https://myauthsite.com/');
    });
})

For internal reasons, the package is set up so that it always has a persistent session, so I need a test to visit a specific URL that would explicitly logout and "kill" the session and then visit the main page that I'm trying to test to ensure that it does redirect me to the right page. However, I'm running into this error

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

The previous URL you visited was:

  > 'https://myauthsite.com'

You're attempting to visit this URL:

  > 'https://myactualsite.com'

You may need to restructure some of your test code to avoid this problem.

https://on.cypress.io/cannot-visit-different-origin-domain

A lot of the workarounds that's listed mostly addresses checking for the attribute instead of visiting the url, or splitting the visits into two different test case, but in my case I really need it to be in its own encapsulated test, is that not possible?

2 Answers

cy.request() isn't bound by the same cross-origin request policies, so if you can log out via an HTTP request that should work.

I tried using chromeWebSecurity=false, but that didn't help.

I'm currently using the API login, setting the cookies, and later read "Google Sign in with Cypress"''

cy.request({
      method:"POST",
      url:API_URL,
      headers: {
         'Content-Type': 'application/json',
      },
      body: api_body}).then((response =>{
      cy.log(response.body)
      cy.setCookie("token_name",response.body['token']);
      cy.visit(destination_url);
   }))

API_URL is your API login URL:

token_name: Login manually to UI, Using the web developer tool you can get the required token_name

This resolved my multi-domain login issue, and you can use it for multi-domain navigation.

Related