I'm writing a simple test in Cypress which clicks a button and starts a download. The API's response contains the unformatted contents of the csv file, so I can use that to assert what I want - but the problem is that when you're using Chrome or Firefox to test with, the download still occurs. There is no dialogue prompt - the download happens on click - but I was wondering if maybe there was a way I could prevent it from happening, maybe with cy.window() or 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')
})
})
Thanks
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.
You can then intercept the response so it doesn't download via cy.intercept
. See: https://docs.cypress.io/api/commands/intercept#Intercepted-responses
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.