I would like to check that a file is downloaded as part of my test. I only need to confirm that the file is downloaded after clicking a button. I have no need to read the file and its contents. All the files I am downloading are zips, not sure if that makes them harder to read?
it('Initiate download', () => {
cy.get('[id="download-button"]')
.should('be.visible')
.click()
});
it('Verify the downloaded file', () => {
cy.readFile('/Downloads/fileName.zip')
.should('exist')
});
In Cypress, you can download a file from a web app using the cypress-downloadfile NPM package. The downloaded file is available in the ./Downloads directory within the BrowserStack machine running your Cypress tests. If such a directory does not exist, Cypress creates it and saves the file in that directory.
You can now test file downloads in Cypress without the download prompt displaying. Any files downloaded while testing file downloads will be stored in the downloadsFolder which is set to cypress/downloads by default. The downloadsFolder will be deleted before each run unless trashAssetsBeforeRuns is set to false.
This is a Cypress custom command to wait and to verify that a file has been successfully downloaded. cy-verify-downloads extends Cypress' cy command. So, you need to add this line to your project's cypress/support/commands.js: And add the following lines to your project's cypress/plugins/index.js:
We can’t read large file. However, we don’t need it. To verify that the file has been downloaded we can install simple Cypress command, which will set up downloads directory, will wait and will verify that the file is downloaded. Oh, wait, just one command?
File download is now supported in Cypress 6.3.0. You can now test file downloads in Cypress without the download prompt displaying. Any files downloaded while testing file downloads will be stored in the downloadsFolder which is set to cypress/downloads by default.
You can try this
const path = require("path");
it('Verify the downloaded file', () => {
const downloadsFolder = Cypress.config("downloadsFolder");
cy.readFile(path.join(downloadsFolder, "fileName.zip")).should("exist");
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With