How to use Cypress tests with docker?

Reading Time: 3 minutes

Let us first understand what docker is and the use of Cypress tests with Docker. The topic is fascinating so stay tuned.

What is Docker?

Docker is open-source containerization. Docker is a virtual environment. Developers will be molding it according to the requirement. It creates a virtual environment inside containers. We can customize these containers.

Why use Docker?

  • Docker does not need machine-specific configurations. Docker container runs without modification across any desktop.
  • This is lightweight as one container will use one process to run.
  • Docker builds its container automatically with the help of base code.
  • The existing container is reusable.

Hence because of so many electrifying features, docker is used across the globe.

Why do we need Cypress with Docker?

I am assuming we all know how to run Cypress tests via Test Runner or CLI. Hence, while executing Cypress uses the host machines resources like memory, browser, etc. Firstly, the host machine will allow using the browser version it is having. There might be a case, where we have to run on different browser versions. We cannot think of taking the whole code to some other machine and run. Finally, this will definitely make the job tedious.

Docker makes this case easy by providing containers. Therefore, we can run with as many different browser versions without creating different environments.

What are the advantages of running Cypress in Docker

  • Firstly, cross-browser testing becomes easy
  • Secondly, the reuse of containers
  • Thirdly, parallel testing
  • Finally, create containers of our own choice with different requirements.

How to run cypress tests in Docker?

  • Prerequisite: Install Cypress and create a cypress test. Follow the below blog to solve this.
https://blog.knoldus.com/data-driven-framework/
  • Download and install Docker: Download docker from https://www.docker.com/. Start docker once it is installed by the command docker ps
  • Creating our yml file: On the root of the project create a file and name it anything with the extension .yml”, example: docker-compose.yml. It should have ‘.dockerfile’ as an extension. You can visit the following URL, where you can find the cypress images.

https://hub.docker.com/u/cypress . It is recommended to use cypress/include as it includes all the other cypress images.

  • Add the following code in the yml file. We have used cypress/included as per the
  • Run the following command in the cypress VS terminal. The command can be found in the below link

https://hub.docker.com/r/cypress/included

docker run -it -v $PWD:/e2e -w /e2e –entrypoint=cypress cypress/included:3.2.0 run

The output will look like

So, this is how we execute the Cypress tests inside Docker containers in a simple way.

References

https://www.ibm.com/in-en/cloud/learn/docker

https://hub.docker.com/u/cypress

https://hub.docker.com/r/cypress/included

Written by 

I work with Knoldus and have experience of 8+ years in quality assurance I have good experience on SeleniumWebdriver(C#), Selenium IDE, Nightwatch,Appium, Jira, TFS, ZAP, Fiddler, Jmeter, Jenkins, Selenium Webdriver. I am also ITIL certified.

Leave a Reply