【发布时间】: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