admin管理员组

文章数量:1335386

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 ponent = 'product-card'
const productCardImage = '[data-test=ponent-product-card_imageContainer]'

describe(`${ponent} ponent interaction tests`, () => {
  it('clicking the image should open the products page', () => {
    loadStory(ponent, '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.

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 ponent = 'product-card'
const productCardImage = '[data-test=ponent-product-card_imageContainer]'

describe(`${ponent} ponent interaction tests`, () => {
  it('clicking the image should open the products page', () => {
    loadStory(ponent, '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.

Share Improve this question asked Aug 13, 2019 at 11:28 alexr89alexr89 4101 gold badge5 silver badges14 bronze badges 7
  • You can set follow redirect = false in visit or request – N.. Commented Aug 13, 2019 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 Commented Aug 14, 2019 at 10:25
  • Have you try to do similar this ? cy.location('pathname').should('eq', '/newthing/:id') – N.. Commented Aug 15, 2019 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 Commented Aug 16, 2019 at 12:00
  • In this case, you should start your test with authentication calls and open 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.. Commented Aug 16, 2019 at 15:17
 |  Show 2 more ments

2 Answers 2

Reset to default 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. to gmail. - 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

Their is an issue of http to https.

本文标签: javascriptHandling Cypress url redirectStack Overflow