How to verify file downloads in a dockerized environment?

Vinay Sharma
TestVagrant
Published in
3 min readSep 24, 2021

Have you ever wondered, why does the file download functionality that works in your local system, starts failing in CI environment?

In this blog, We are going to understand why it fails and what are possible solutions that we can implement to fix this.

Problem Statement:

Validation of file download functionality works very well without any issue in your local system. However, when you try to run the same script inside any CI environment, the file gets downloaded but the test case still fails.

The root cause of the failure:

We wrote the script to download a file let say for a chrome browser. Hence in your local setup when you run your test case it usually downloads the file inside the systems downloads folder but when the same script goes inside the docker it starts failing. Why? The reason is the separate OS running inside the docker container. Inside docker (running Linux OS), the file gets downloaded usually in /home/seluser/Download the directory which is a default download directory for the chrome browser. And since in the test script we have written to check the file in the local downloads folder which will be missing in this case. Hence the test cases start failing.

So to get this working we need to modify our script in such a way that it should download the file on the docker/Host machines download folder and then get copied to our local system.

Proposed Solution:

To make it work we will use the feature of mounting the volumes in the docker-compose file.

We have to add this line under volumes

./tempDownload:/home/seluser/Downloads

This will create a directory name tempDownload inside your workspace and the files which are getting downloaded inside /home/seluser/Downloads will be copied to your local workspace tempDownload folder on the go and now you can proceed further with your test case to check if the file which got downloaded is present inside your local workspace.

With these modifications, your script should start working.

Is it still failing?

If still, your test cases are failing on the CI environment check for the permission for /home/seluser/Downloads directory, If it has all the permissions to download the file.

Handling Permissions

You can change the permission just by adding a shell file under your root directory.

This will help to change the permission after you move inside the container.

After the above file is created, let's move inside the container and at the same time execute this shell file. All we need to do is to add the below command in a docker-compose file just when the docker starts spinning.

docker exec -i $(docker ps -qf “name=<containerName>”) bash < fileName.sh

Now run the test case, It should start working.

Conclusion

So whenever this test case will be executed on docker. It will download the file on the host machine and will copy it to your local directory. And the test case will pass.

Hopefully, you were able to follow this setup successfully and able to run the test case.

Happy Learning!!

--

--