【问题标题】:Handling Cypress url redirect处理 Cypress url 重定向
【发布时间】:2019-12-19 22:56:09
【问题描述】:

我有一个赛普拉斯测试,它点击图片导致重定向到特定网址。然后测试会检查 url 是否包含特定的字符串。

但是,单击此图片会导致测试停止/失败,并显示“糟糕,没有要运行的测试”。重定向发生时的消息。

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')
  })
})

我的测试在http://localhost:9002 上运行,似乎在测试套件运行时重定向到http://localhost:9002/product/productId 是导致赛普拉斯崩溃/失败的原因,而赛普拉斯却试图转到https://localhost:9002/__/

我想知道如何单击此图像并重定向到 url 而不会导致 Cypress 崩溃/失败。

【问题讨论】:

  • 访问或请求中可以设置follow redirect = false
  • 我一直在玩,现在将图像放在带有 href 的 a 标签内。这并没有明确地解决问题,但是明确地将目标 _self 添加到 a 标记已经解决了它。到目前为止,我不知道为什么会这样。
  • 您是否尝试过类似的操作? cy.location('pathname').should('eq', '/newthing/:id')
  • 是的,我有 - 因为赛普拉斯似乎重定向了整个浏览器,寻找位置总是失败。出于某种原因,目标 _self 是解决此问题的唯一方法。
  • 在这种情况下,您应该使用身份验证调用开始测试并打开localhost:9002/product/productId。如果您不知道 ID,在这种情况下,请进行 API 调用并添加产品并使用该 ID 打开

标签: javascript reactjs testing cypress end-to-end


【解决方案1】:

他们的问题是httphttps

【讨论】:

    【解决方案2】:

    赛普拉斯不支持跨域。

    示例: 第 1 步:您导航到谷歌 第 2 步:搜索 Gmail 第三步:点击gmail链接

    您正在从 Google.com 切换到 gmail.com - cypress 不支持此操作。

    解决方法 1: 您可以将设置的 href 属性值删除为空白,如下所示:

    target="_blank" 这样它将在同一页面中打开。

    解决方法 2:

    将步骤 1 和步骤 2 放在一个测试迭代中

    并将步骤 3 放入另一个迭代中

    【讨论】:

    • 感谢拉詹的回答。它不是跨域的,它在同一个域上。
    猜你喜欢
    • 2010-10-18
    • 1970-01-01
    • 2011-04-08
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-18
    相关资源
    最近更新 更多