Cypress - How to verify if a file is downloaded?
Asked Answered
S

6

24

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')
 });
Singleness answered 4/3, 2021 at 15:23 Comment(3)
#47931381Attribution
Checking that a file exists in the downloads folder does not verify that the file has just been downloaded as it may be there from a previous testDamiano
...unless Cypress downloads it to its own downloads folder and overwrites any previous files. I had to use a plugin to leverage this featureDamiano
H
36

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");
});
Hectare answered 30/4, 2021 at 6:39 Comment(3)
Be aware of this issue though...Maletta
Some other approaches like this and this use cy.task(...), because they use node modules like fs.existsSync(...). This answer is elegant because it uses cy.readFile(...); it doesn't need cy.task(...) so it's short and easy. However, for me, the downloaded file is a large PDF, and so readFile (or logging the file contents to cy log as part of the assertion) slowed down my test, maybe I should use {log: false}. Or use "exists" approach instead of "read"Causey
Works without using the path. cy.readFile(`${Cypress.config('downloadsFolder')}/fileName`)Gregggreggory
T
10

According to the Cypress documentation (https://docs.cypress.io/api/commands/readfile#Existence), it is enough to just call

cy.readFile(myFile.txt) // GOOD

Do not call

cy.readFile(myFile.txt).should('exist') // BAD

I was experiencing some performance problem with Cypress 10, when I called .should('exist'). This was very slow and ended in some timeouts.

Tubule answered 10/11, 2022 at 18:28 Comment(1)
In addition I used the path "cypress/downloads/myFile.txt" to make it workLeverrier
J
2

I also ran into the same issue but got resolved by using a double backslash for the download folder path and this resolved my issue

it('Verify the downloaded file', () => {
   cy.readFile('cypress\\Downloads\\fileName.zip')
   .should('exist')
 });
Jarid answered 12/5, 2022 at 10:32 Comment(0)
H
1

You can checkout and use this template that cypress provides for your case: https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/testing-dom__download

Hectoliter answered 18/8, 2023 at 13:24 Comment(0)
F
0

All that syntactic sugar can be avoided by using cy-verify-download:

cy.verifyDownload('picture.png');

// verify download by file extension or partial filename
cy.verifyDownload('.png', { contains: true });
cy.verifyDownload('pic', { contains: true });

// or increase timeout
cy.verifyDownload('archive.zip', { timeout: 25000 });

// or increase timeout and interval pooling
cy.verifyDownload('archive.zip', { timeout: 25000, interval: 600 });
Fluency answered 28/6, 2023 at 17:22 Comment(0)
D
0

Add this in your config.js

on('task', 
   { deleteFolder(folderName) { return new Promise((resolve, reject) => {
         rm(folderName, { maxRetries: 10, recursive: true }, (err) => {
               if (err) { console.error(err) } resolve(null) }) 
           }) 
      } 
   }
)

And then add this in your test:

beforeEach(() => { cy.task('deleteFolder', 'cypress//downloads//'); });
Diggings answered 28/5, 2024 at 12:21 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.