Hackernoon logoE2E Testing of Excel Downloads with Cypress by@vivek-nayyar

E2E Testing of Excel Downloads with Cypress

Vivek Nayyar Hacker Noon profile picture

@vivek-nayyarVivek Nayyar

๐Ÿ‘‹ Senior Software Engineer with 5 years' of experience building products for numerous domains.

Recently while working on a project, I had to create a functionality where users can download an excel file consisting of some data.

In this process, since I am a big fan of E2E Testing with Cypress.io, I decided to write a test suite that could make sure that the excel is being downloaded correctly and also consists of the correct data that our users would expect.

This article assumes a basic understanding of cypress.


If you haven't tried e2e testing with cypress before, I would highly recommend skipping over to the

section and following some of the getting started guides.

๐Ÿ“ฆ Installation

1) Assuming you have a repo named

, create a new folder in it named


into that folder and execute the following commands inside it

npm init
npm install cypress --save-dev

3) Update the projectโ€™s scripts by opening

and updating your scripts to the following:

"scripts": {
   "cy:run": "cypress run --headless -b chrome",
   "cy:open": "cypress open"

4) You should also see some default folders and files created by cypress for you after you have installed cypress.

5) The test that we will write will go inside


โš™๏ธ Functionality

For the sake of simplicity, let's say our application only has one feature where users can click on a

download template
button, which can download an excel file comprising of some data in it.

On click of the download template button a file gets downloaded which looks something like this:


๐Ÿงช E2E Testing

1) Create a new file inside

folder with the name

2) Inside this file our test would first start with checking for existence of our button and then we will click on it.


3) After clicking on it, the file should ideally have been downloaded and now we need to somehow read that file and check if it exists and also check if it consists of the right data in it.

4) To do that, we will first need to install another npm package which can parse the excel and convert it to a json

npm install node-xlsx --save-dev

5) After this inside your

file we will create a new task

const xlsx = require("node-xlsx").default;
const fs = require("fs");
const path = require("path");

module.exports = (on, config) => {
  // `on` is used to hook into various events Cypress emits
  on("task", {
    parseXlsx({ filePath }) {
      return new Promise((resolve, reject) => {
        try {
          const jsonData = xlsx.parse(fs.readFileSync(filePath));
        } catch (e) {

This function will parse our excel file and convert it to json.

6) Finally letโ€™s complete our test for excel file download

// data to check against
const data = [
// check for existence of the button on the ui and then click it
// arbitrary wait so that the download can complete
// call the parseXlsx task we created above to parse the excel and return data as json
  jsonData => {
    // finally we write the assertion rule to check if that data matches the data we expected the excel file to have.

7) With this we can be sure that our excel download functionality is working as expected.

Test Execution

8) The only caveat here is how do you know the download path for your CI/CD Pipeline or how can you change that path to something else.

The solution to that is built into cypress. Again inside

file we will create another task.

on("before:browser:launch", (browser = {}, launchOptions) => {
  const downloadDirectory = path.join(__dirname, '..', 'excelDownloads')

  if (browser.family === 'chromium') {
   launchOptions.preferences.default['download'] = { default_directory: downloadDirectory }
  return launchOptions;

Here we are changing the default directory of download to a directory named

inside of cypress folder.


๐Ÿ’ก Conclusion

In conclusion, E2E tests are really important for every app and you should write one too because this is the closest you can get to test how an actual user will use your application.

And as @tlakomy says:

Sleep better at night with e2e tests and cypress.io

๐Ÿ‘ฏ Share this article if you found it helpful!

You can follow me on twitter @VivekNayyar09 for more updates.

Also please don't forget to maintain social distance to prevent the virus from spreading and wash your hands regularly. Stay safe and stay at home.

๐Ÿš€ References

Previously published at https://dev.to/viveknayyar/e2e-testing-of-excel-downloads-with-cypress-21fb

Vivek Nayyar Hacker Noon profile picture
by Vivek Nayyar @vivek-nayyar. ๐Ÿ‘‹ Senior Software Engineer with 5 years' of experience building products for numerous domains.Read my stories


Join Hacker Noon

Create your free account to unlock your custom reading experience.