Javascript 处理Cypress url重定向

Javascript 处理Cypress url重定向,javascript,reactjs,testing,cypress,end-to-end,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' con

我有一个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:导航到谷歌 第二步:搜索Gmail 第三步:点击gmail链接

您正在从Google.com切换到gmail.com-cypress不支持此功能

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

target=“\u blank” 这样它就可以在同一页中打开

解决方法2:

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


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

他们的问题是
http
https


您可以在visit中设置follow redirect=false,或者requestIve一直在玩,现在将图像放在带有href的a标记中。这并没有显式地解决这个问题,但是显式地将target\u self添加到a标记已经解决了这个问题。到目前为止,我还不知道这是为什么。你有没有尝试过这样做?cy.location('pathname')。should('eq','/newthing/:id')是的,我有-因为cypress似乎重定向了整个浏览器,所以查找位置总是失败。出于某种原因,target\u self是解决这个问题的唯一方法。在这种情况下,您应该通过身份验证调用开始测试并打开。如果您不知道ID,在本例中,请调用API并添加产品,然后使用该ID打开。谢谢Rajan的回答。它不是跨域的,而是在同一个域上。