javascript - 处理 Cypress 网址重定向

标签 javascript reactjs testing cypress end-to-end

我有一个 Cypress 测试,它点击一个图像导致重定向到一个特定的 url。然后测试检查 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 而测试服是运行是导致 Cypress 崩溃/失败的原因,相反 Cypress 尝试转到 https://localhost:9002/__/

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

最佳答案

Cypress 不支持跨域。

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

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

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

目标=“_空白” 这样它将在同一页面中打开。

解决方法 2:

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

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

关于javascript - 处理 Cypress 网址重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57476816/

相关文章:

javascript - 单选按钮无法返回不同状态

angularjs - 了解高级框架。 Angular.js 中 Controller 、指令、CSS、服务按什么顺序执行?

c# - 如何在 NUnit 中用数据填充对象

javascript - 测试咖啡厅 : How to use typeText without selector

javascript - UI-router 未路由到新模板

javascript - 如何更新状态中存在的 JSX 元素的状态?

javascript - 响应式 Quicktime 播放器

javascript - 在javascript中包装函数

reactjs - 无法从 useSWR 错误属性获取响应错误

ruby-on-rails-3 - 测试遗忘变量中的每个循环