cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.66k stars 3.16k forks source link

[Multi-domain]: Support nested multi-domain commands #20718

Open chrisbreiding opened 2 years ago

chrisbreiding commented 2 years ago

Add support for nested multi-domain commands, for cases where you visit multiple domains in a row before ending up back on the test's primary domain.

cy.visit('http://domain1.com')
cy.get('a').click() // takes you to domain2.com
cy.origin('domain2.com' () => {
  cy.get('a').click() // takes you to domain3.com
  cy.origin('domain3.com', () => {
    cy.get('[data-cy="username"]').type('myusername')
    cy.get('[data-cy="login"]').click() // takes you back to domain1.com
  })
})
cy.get('h1').invoke('text').should('equal', 'Welcome, myusername!')
davidmastreamline commented 2 years ago

Im trying to use cy.origin() to log in through auth0 (which works) but then I need to nest another cy.origin() to sign in with google in Auth0. Im getting this error

Screen Shot 2022-05-13 at 1 47 15 PM
maddocash commented 2 years ago

We have a similar use case where we need to go through a couple of origins before redirecting back to the the test domain. We saw the same warning as @davidmastreamline

modern-sapien commented 2 years ago

workaround for nested origins/domains

cy.visit('http://domain1.com/')
cy.get('a').click() // takes you to domain2.com
cy.origin('domain2.com' () => {
  cy.get('a').click() // takes you to domain3.com
})
cy.origin('domain3.com', () => {
  cy.get('[data-cy="username"]').type('myusername')
  cy.get('[data-cy="login"]').click() // takes you back to domain1.com
})
cy.get('h1').invoke('text').should('equal', 'Welcome, myusername!')
AleksiUu commented 2 years ago

@modern-sapien This solution works perfectly and allows multi-domain login flow to be implemented finally! Thank you!

modern-sapien commented 2 years ago

@AleksiUu, don't thank me, I stole this from @mjhenkes.

jelena9412 commented 2 years ago

@modern-sapien I am trying to use the recommended steps above, but for some strange reason when I want to login to office 365 online, after the second domain my page doesn't load and I have a blank screen. If you have any ideas on how to work this out, I would appreciate it 🤩

it.only('microsoft login', () => {
    cy.visit('https://www.office.com/')
    cy.get('#hero-banner-sign-in-to-office-365-link').click()

    cy.origin('https://login.microsoftonline.com', () => {
        cy.get('#i0116').type('email')
        cy.get('#idSIButton9').click()
    })
    //after the last click the page is never loaded therefore we cannot continue with the password
    cy.origin('https://login.live.com', () => {
            cy.get('#i0118').type('password')
            cy.get('#idSIButton9').click()
    })
})
cupcakepanda89 commented 1 year ago

do you have any update on this issue because I encounter the same issue even after trying the work around?

cy.origin('https://login.abc.com', () => {
    cy.visit('https://auth.xyz.ca');
    cy.get('#username').type(this.username);
    cy.get('#password').type(this.password);
    cy.get('#signIn').click();
});
cy.origin('https://auth.xyz.ca', () => {
    cy.get('body');
    cy.pause();
});
cy.pause();
  1. user go to 'https://auth.xyz.ca'
  2. it redirect user to 'https://login.abc.com'
  3. user enter username and password there and sign in
  4. after click sign in, it gets the token and auto-sign in then redirect user back 'https://auth.xyz.ca'
jackie-greenbaum commented 1 year ago

Adding another bump for this issue. We are facing the exact scenario described in the original comment when trying to login via SSO.

modern-sapien commented 1 year ago

@jelena9412

re: office 365 online More than likely there is an issue here with browser busting / automation blocking, but I am unsure. There are some sites that make traversal more difficult.

Especially big corporate websites, I would check their specific documentation for how test automation should be handled for their services if you have built your application to rely on it.

modern-sapien commented 1 year ago

@cupcakepanda89

How are you handling passing the token? How are you handling the redirect? Are you opening another origin? or travelling back to the initial page?

Cypress has a pretty active and huge community in Discord. You may find more assistance there versis GitHub issues.

https://discord.gg/qKWKF75u

sleepingmonk commented 7 months ago

Another up vote. I have a scenario where SSO happens on one origin but redirects to a second origin for users in a specific group. My application doesn't have direct access to the second authenticator and must be handed off through the first.