简体   繁体   English

处理 Cypress url 重定向

[英]Handling Cypress url redirect

I have a Cypress test which clicks on an image causing a redirect to a specific url.我有一个 Cypress 测试,它点击一个图像导致重定向到一个特定的 url。 The test then checks the url contains a specific string.然后测试检查 url 是否包含特定字符串。

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: Cypress 测试非常简单:

/* 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/__/我的测试在http://localhost:9002上运行,似乎在测试套件运行时重定向到http://localhost:9002/product/productId是导致 Cypress 崩溃/失败的原因,而 Cypress 尝试转到https://localhost:9002/__/

I am wondering how I can click this image and redirect to the url without causing this crash/fail in Cypress.我想知道如何单击此图像并重定向到 url 而不会在 Cypress 中导致此崩溃/失败。

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示例:第 1 步:您导航到 google 第 2 步:搜索 Gmail 第 3 步:点击 gmail 链接

You are switching from Google.com to gmail.com - cypress doesn't support this.您正在从 Google.com 切换到 gmail.com - cypress 不支持此功能。

Workaround 1: You can remove set href attribute value to blank as below:解决方法 1:您可以将 set href 属性值删除为空白,如下所示:

target="_blank" so that it will open in same page. target="_blank" 以便在同一页面中打开。

Workaround 2:解决方法 2:

put step 1 and step 2 in one test iteration将第 1 步和第 2 步放在一个测试迭代中

and put step 3 in another iteration并将步骤 3 放在另一个迭代中

Their is an issue of http to https .他们是httphttps的问题。

见附件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM