【问题标题】:Is there a way I can prevent downloads when testing with Cypress在使用赛普拉斯进行测试时,有没有办法可以防止下载
【发布时间】:2020-11-06 07:20:39
【问题描述】:

我正在 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')
      })
  })

谢谢

【问题讨论】:

    标签: javascript testing automation download cypress


    【解决方案1】:

    如果您的服务器发送特定的处置标头导致浏览器提示下载,您可以确定该请求发往哪个 URL,并使用 cy.request() 直接点击该 URL。然后你可以测试服务器是否发送了正确的响应头。

    然后您可以拦截响应,使其不会通过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

    【讨论】:

      猜你喜欢
      • 2022-08-02
      • 2021-10-29
      • 2018-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-10
      • 2020-06-24
      • 2023-02-14
      相关资源
      最近更新 更多