gpt4 book ai didi

javascript - 在 Chrome 中运行时如何使用 Cypress 测试文件下载

转载 作者:搜寻专家 更新时间:2023-11-01 04:43:09 24 4
gpt4 key购买 nike

我有以下测试需要验证单击链接是否下载了 PDF。这一点尤其重要,因为我们正在使用 Gatsby,而 Gatsby 又使用 Reach Router 的链接组件,并且相对容易配置错误,以便路由器接管链接并导航到 404 页面而不是启动下载。

describe.skip(`Downloads`, () => {
it(`Downloads the expected file`, () => {
cy.visit(pagePath)
cy.getByHref(downloadPath)
.should(`have.attr`, `target`, `_blank`)
.click()
cy.location(`pathname`).should(`eq`, pagePath)
})
})

虽然这并不完美,但它至少会检查单击链接后是否没有导航。

问题是,当使用在 Chrome 中运行测试的 cy run 运行此测试时,由于 Chrome 的下载对话框,测试挂起。

如何防止测试挂起?

请注意,下载路径解析为静态目录中的 pdf,例如 /static/example.pdf。没有服务器组件。

另请注意,这是一个不同的问题:How can I use Cypress.io to assert that a file download has been initiated without actually downloading?

最佳答案

实际上我搜索了很多,发现无法在安装了浏览器扩展的 headless 模式下运行测试,因为在 headless 模式下唯一受支持的浏览器是 Electron,而 Electron 不支持文档中所述的扩展。

尚不支持运行 headless Chrome。见本期:#488 https://github.com/cypress-io/cypress/issues/488

这是一个不太老的问题,它在 2019 年 2 月被标记

https://github.com/cypress-io/cypress/issues/832 https://github.com/cypress-io/cypress/issues/1235

关于javascript - 在 Chrome 中运行时如何使用 Cypress 测试文件下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55827547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com