20

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')
 });
Finney14
  • 261
  • 2
  • 3
  • 8
  • https://stackoverflow.com/questions/47930881/how-can-i-use-cypress-io-to-assert-that-a-file-download-has-been-initiated-witho – Tom1425 Mar 04 '21 at 22:41
  • 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 test – josef Mar 29 '23 at 16:46
  • ...unless Cypress downloads it to its own downloads folder and overwrites any previous files. I had to use a plugin to leverage this feature – josef Mar 31 '23 at 11:37

5 Answers5

31

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");
});
Husnul Aman
  • 489
  • 3
  • 11
  • Be aware of [this issue](https://github.com/cypress-io/cypress/issues/14857) though... – Marc-André Lafortune Dec 22 '21 at 23:28
  • 1
    Some other approaches like [this](https://stackoverflow.com/a/62787541/1175496) and [this](https://docs.cypress.io/api/commands/task#Read-a-file-that-might-not-exist) 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" – Nate Anderson Jan 21 '22 at 05:24
8

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.

René Winkler
  • 6,508
  • 7
  • 42
  • 69
1

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')
 });
shreyas
  • 37
  • 1
  • 11
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 });
-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

timoblume
  • 76
  • 3