Sunday 6 December 2020

How can I test a client-side redirect to a 3rd party site with Cypress?

Cypress offers a simple way to test for server-side redirects using request:

cy.request({
  url: `/dashboard/`,
  followRedirect: false, // turn off following redirects
}).then((resp) => {
  expect(resp.redirectedToUrl).to.eq('http://example.com/session/new')
})

However this doesn't work for client-side redirects because the page is loaded successfully before the redirect happens, meaning the response is for the page, not for the redirect.

How can I test a client-side redirect?

I need a way of catching the redirect and verifying that:

  • it occurred
  • was to the correct URL.

Note:

  • I don't want to follow the redirect away from the app that is being tested. I'm not testing the whole auth flow. I just need to know there was a redirect.
  • I can't change this auth flow. The redirect is unavoidable.
  • The redirect happens during initialisation, not as a result of any user interaction.
  • The redirect uses: window.location.href = url
  • See my answer below for an attempt at resolving this.


from How can I test a client-side redirect to a 3rd party site with Cypress?

No comments:

Post a Comment