29

E2E Testing of excel file downloads with cypress - Vivek Nayyar - Medium

 4 years ago
source link: https://medium.com/@VivekNayyar/e2e-testing-of-excel-downloads-with-cypress-d6e46ccdc232
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

E2E Testing of excel file downloads with cypress

Image for post
Image for post
Illustration by Maggie Appleton for egghead.io

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.

Disclaimer

If you haven’t tried e2e testing with cypress before, I would highly recommend skipping over to the `References` section and following some of the getting started guides.

📦 Installation

  1. Assuming you have a repo named dashboard-ui , create a new folder in it named e2e.
  2. cd into that folder and execute the following commands inside it.

3) Update the project’s scripts by opening package.json and updating your scripts to the following:

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

Image for post
Image for post

5) The test that we will write will go inside integration folder.

⚙️ 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.

Image for post
Image for post

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

Image for post
Image for post
Image for post
Image for post
gif to demo excel file download

🧪 E2E Testing

  1. Create a new file inside integration folder with the name ExcelDownload.spec.js .
  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.

5) After this, inside your plugins/index.js file we will create a new task to parse excel.

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

6) Finally let’s complete our test for excel file download

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

Test Execution

Image for post
Image for post
demo of running e2e test in cypress

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 plugins/index.js file we will create another task.

Here we are changing the default directory of download to a directory named excelDownloads inside of cypress folder.

https://docs.cypress.io/api/plugins/browser-launch-api.html#Change-download-directory

💡 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 Tomasz Łakomy 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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK