Cypress - How to verify if a file is downloaded?

Viewed 14394

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')
 });
2 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");
});

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')
 });
Related