gpt4 book ai didi

javascript - 在使用 Cypress 进行测试时,有没有一种方法可以阻止下载

转载 作者:行者123 更新时间:2023-12-04 04:02:00 27 4
gpt4 key购买 nike

我正在 Cypress 中编写一个简单的测试,它单击一个按钮并开始下载。 API 的响应包含 csv 文件的未格式化内容,因此我可以使用它来断言我想要什么 - 但问题是当您使用 Chrome 或 Firefox 进行测试时,下载仍然发生。没有对话提示 - 下载是在点击时发生的 - 但我想知道是否有办法阻止它发生,也许是用 cy.window() 或 cy.stub()?

it('Export list to CSV', () => {
//Click button and assert on the API
cy.findByTestId('btnExport_SystemUsers')
.should('be.visible')
.click()
.wait('@exportToCsv')
.then((xhr) => {
//Assert on some keywords from the response such as Role names, user names, and site group name (that won't change)
expect(xhr.status).to.eq(200)
expect(xhr.response.body.csvFileData).to.contain('CY.User')
expect(xhr.response.body.csvFileData).to.contain('CY.Admin')
expect(xhr.response.body.csvFileData).to.contain('unlockUserAccount')
expect(xhr.response.body.csvFileData).to.contain('CY Administrator')
expect(xhr.response.body.csvFileData).to.contain('Site Group 1')
})
})

谢谢

最佳答案

If your server sends specific disposition headers which cause a browser to prompt for download, you can figure out what URL this request is made to, and use cy.request() to hit that directly. Then you can test that the server send the right response headers.

然后您可以拦截响应,这样它就不会通过 cy.intercept 下载。请参阅:https://docs.cypress.io/api/commands/intercept#Intercepted-responses

引用:https://docs.cypress.io/faq/questions/using-cypress-faq#Is-there-a-way-to-test-that-a-file-got-downloaded-I-want-to-test-that-a-button-click-triggers-a-download

关于javascript - 在使用 Cypress 进行测试时,有没有一种方法可以阻止下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62937810/

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