5

I have a Cypress test which clicks on an image causing a redirect to a specific url. The test then checks the url contains a specific string.

However, clicking this image causes the tests to stop/fail with a "Whoops, there is no test to run." message when the redirect happens.

The Cypress test is very simple:

/* global describe, it, cy */
import loadStory from '../../../config/cypress/helpers/loadStory'

const component = 'product-card'
const productCardImage = '[data-test=component-product-card_imageContainer]'

describe(`${component} component interaction tests`, () => {
  it('clicking the image should open the products page', () => {
    loadStory(component, 'Default')
    cy.get(productCardImage).should('be.visible')
    cy.get(productCardImage).click()
    cy.url().should('contain', '/product')
  })
})

My tests run on http://localhost:9002 and it seems that redirecting to http://localhost:9002/product/productId while the test suit is running is what causes Cypress to crash/fail and instead Cypress tries to go to https://localhost:9002/__/

I am wondering how I can click this image and redirect to the url without causing this crash/fail in Cypress.

alexr89
  • 381
  • 1
  • 4
  • 14
  • You can set follow redirect = false in visit or request – N.. Aug 13 '19 at 18:04
  • Ive been playing around and now have the image inside an a tag with an href. This didn't explicitly fix the problem, however explicitly adding target _self to the a tag has fixed it. As of yet I have no idea why that is. – alexr89 Aug 14 '19 at 10:25
  • Have you try to do similar this ? cy.location('pathname').should('eq', '/newthing/:id') – N.. Aug 15 '19 at 14:54
  • Yes I have - because cypress redirected the entire browser it seemed, looking for the location always failed. For some reason target _self is the only way to get around this. – alexr89 Aug 16 '19 at 12:00
  • In this case, you should start your test with authentication calls and open http://localhost:9002/product/productId. If you don't know ids, in this case, make API calls and add products and open with that Ids – N.. Aug 16 '19 at 15:17
  • Did you ever find a fix for this? – John Nov 13 '19 at 21:22
  • Hi @John, I did yes - I had to explicitly add target _self to the a tag. I am still unsure why this was but it seemed to fix it. – alexr89 Nov 20 '19 at 11:58

2 Answers2

1

Cross domain is not supported in Cypress.

Example: step 1: You navigate to google step 2: Search for Gmail step 3: clicked on gmail link

You are switching from Google.com to gmail.com - cypress doesn't support this.

Workaround 1: You can remove set href attribute value to blank as below:

target="_blank" so that it will open in same page.

Workaround 2:

put step 1 and step 2 in one test iteration

and put step 3 in another iteration

Rajan Domala
  • 133
  • 1
  • 8
0

Their is an issue of http to https.

See Attached

4b0
  • 21,981
  • 30
  • 95
  • 142