英文:
Network request fails when clicking link during Cypress test and forcing it to remain in the same tab
问题
在我的Cypress测试中,我需要点击一个链接,该链接会重定向并(通常)在新标签页中打开正确的页面(该页面位于不同的域)。因为Cypress无法管理多个标签页,我已经强制它在同一个标签页中打开链接,使用以下代码:
.invoke('removeAttr', 'target').click()
然而,在这样做时,请求失败了。我可以在DevTools的网络选项卡中看到请求失败的情况:
如果我允许它在新标签页中打开,就不会发生这种情况:
此外,如果我获取重定向的URL并将其粘贴到第一个标签页的地址栏中,一切都按预期工作。我尝试使用cy.visit()
访问重定向的URL,而不是使用.invoke('removeAttr', 'target')
点击链接,但结果相同。
我已经找到了一个不太好的解决方法,但仍然想知道是否有更好的解决方案 - 如果我将重定向的URL保存到文件中,然后在单独的测试中使用cy.visit()
导航到保存的URL,它就可以正常工作。只是它不允许我在同一个测试中执行这个操作。
英文:
In my Cypress test I need to click a link that redirects and (normally) opens the correct page (which has a different domain) in a new tab. Because Cypress can't manage multiple tabs, I have forced it to open the link in the same tab using:
.invoke('removeAttr', 'target').click()
However, when doing this the request fails. I can see the failed request in the Network tab of DevTools:
This does not happen if I allow it to open in the new tab:
Also, if I get the redirect URL and paste it into the address bar of the first tab, everything works as expected. I have tried to do cy.visit() to the redirect URL instead of clicking the link with .invoke('removeAttr', 'target'), but get the same result.
I have managed to find a nasty work-around to this, but would still like to know if anyone has a better solution - if I save the redirect URL to a file, then in a separate test navigate to the saved url using cy.visit(), it works fine. It just won't let me do it within the same test.
答案1
得分: 2
看起来你需要将chromeWebSecurity设置为关闭,因为你的引用策略是strict-origin-when-cross-origin
。
要做到这一点,进入cypress.config.js
文件
const { defineConfig } = require('cypress')
module.exports = defineConfig({
e2e: {
baseUrl: 'http://localhost:1234',
chromeWebSecurity: false,
},
})
参考配置。
英文:
It looks like you need to set chromeWebSecurity off, since you have strict-origin-when-cross-origin
as Referer Policy.
To do this, go to cypress.config.js
const { defineConfig } = require('cypress')
module.exports = defineConfig({
e2e: {
baseUrl: 'http://localhost:1234',
chromeWebSecurity: false,
},
})
Ref Configuration
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论