Can cy.origin wait longer for a redirect?

211 Views Asked by At

I have some code that visits my page, which then redirects for authentication. The issue is the page takes too long to do the redirect, so cy.origin times out before the domain it is targeting is loaded. Is there a way (other than an evil hard coded wait) to get cy.origin to wait longer for the domain it's targeting?

I tried cy.url().should('match', /^authurl.com/) before the cy.origin call, but of course that won't work because cy.url() is cross domain by the time authurl.com is loaded (cy.url() seems to return blank at that time). I tried putting cy.url().should('match', /^authurl.com/) inside the origin call, but I think that's redundant, as it won't execute until the authurl.com domain is loaded.

Any suggestions?

  cy.visit(Cypress.config().baseUrl)
  
  cy.origin( 'authurl.com', { args: { username, password } }, 
   ({ username, password }) => {   
      // Enter username and submit.
      cy.get('input[type="email"]').type(username, { log: false })
      cy.get('input[type="submit"]').click()

      // Enter password and submit.
      cy.get('input[type="password"]').type(password, { log: false })
      cy.get('input[type="submit"]').click()
    }
2

There are 2 best solutions below

0
Andrew On BEST ANSWER

Working solution similar to the first option suggested by @aladin-spaz, just using url() instead of location(), and located at the top of the origin instead of the bottom.

 cy.visit(Cypress.config().baseUrl)
  
  // Login to your AAD tenant.
  cy.origin( 'authurl.com', { args: { username, password } }, 
   ({ username, password }) => {
      cy.url().should('match', /^https:\/\/authurl\.com/, { timeout: 90000 })

      // Enter username and submit.
      cy.get('input[type="email"]').type(username, { log: false })
      cy.get('input[type="submit"]').click()

      // Enter password and submit.
      cy.get('input[type="password"]').type(password, { log: false })
      cy.get('input[type="submit"]').click()
    }
  )
4
Aladin Spaz On

I guess you might make an assertion on the location, ref cy.location - options

cy.origin( 'authurl.com', { args: { username, password } }, 
   ({ username, password }) => {   
      // Enter username and submit.
      cy.get('input[type="email"]').type(username, { log: false })
      cy.get('input[type="submit"]').click()

      // Enter password and submit.
      cy.get('input[type="password"]').type(password, { log: false })
      cy.get('input[type="submit"]').click()

     
      cy.location({timeout:10_000})
        .should((loc) => {
          expect(loc.href).to.eq(Cypress.config().baseUrl)
        })
    }

Or if it's the auth page that's slow, just add timeout to the email query

cy.origin( 'authurl.com', { args: { username, password } }, 
   ({ username, password }) => {   

      // Try with redirecting cy.visit() inside the origin
      cy.visit(Cypress.config().baseUrl)

      // Add timeout here to wait for auth page to load
      cy.get('input[type="email"]', {timeout:10_000})
        .type(username, { log: false })

      cy.get('input[type="submit"]').click()

      // Enter password and submit.
      cy.get('input[type="password"]').type(password, { log: false })
      cy.get('input[type="submit"]').click()
    }