Cypress: How to verify that file is downloaded with cy-verify-downloads

Yevhen Laichenkov
3 min readAug 10, 2021

--

In this article I’ll show how you can verify that file is downloaded with Cypress.

Problem 🔴

First, imagine situation that you have a functionality where user clicks on the Download button, and file is starting to download to your computer. So, you have to test download functionality and you’re using cypress as a tool for testing.

simple download functionality

Before we start, I’d like to mention that there’re a lot of ways to test this, so it depends. Now, let’s talk about how Cypress team suggests us to test download file functionality. In Cypress example recipes we can see that we have to use cy.readFile method to ensure that the file has been downloaded. Besides that we have to add timeout to make sure that the file is downloaded before reading it and also we have to add expect for assertion. Even more, don’t forget to set up downloads folder. So, it will be:

There are some disadvantages:

  1. You have to know the actual file size
  2. readFile method can’t handle large files (e.g 1GB and more)

I know it sounds silly, but that’s the truth. 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? Yeah, just one command, but also it requires some installation.

Solution 🟢

Installation

Let’s install custom cypress command to verify downloaded file:

npm i -D cy-verify-downloads

Also, you need to add this line to your project’s cypress/support/commands.js:

require('cy-verify-downloads').addCustomCommand();

And add the following lines to your project’s cypress/plugins/index.js:

Usage

Then, in your test, we can update our example with the following line of code:

After running it you will see something like this in the log:

cy-verify-downloads log

Conclusion

If you want to find out more about cy-verify-downloads custom command, you can read its documentation here. Code examples you can find here. If you need to verify file size or exact content read more cypress example recipes.

If you enjoyed this article, please tell a friend about it! Share it on Twitter, LinkedIn or Facebook.

Don’t forget to subscribe to Test Automation Weekly digest to keep up with latest news in test automation world.

--

--

Yevhen Laichenkov
Yevhen Laichenkov

Written by Yevhen Laichenkov

Software Development Engineer In Test / Open source creator

Responses (10)