Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress - How to verify if a file is downloaded?

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')
 });
like image 465
Finney14 Avatar asked Mar 04 '21 15:03

Finney14


People also ask

How does Cypress handle file download?

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.

How do I test file downloads in Cypress without the download prompt?

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.

How do I use Cy-verify-downloads in Cypress?

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:

How to read large file in Cypress?

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?

What's new in Cypress 6?

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.


1 Answers

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");
});
like image 68
Husnul Aman Avatar answered Dec 05 '22 11:12

Husnul Aman