Running Cypress Tests in Docker (Part 9)

Running Cypress Tests in Docker (Part 9)
Running Cypress Tests in Docker

こんにちは Kon'nichiwa,

This article series have been using Cypress v5.2.0; in this installment we will be using Cypress v9.2.0 (Why? I'll explain later)

There are multiple ways of how we can leverage Docker with a Cypress setup, in this article I'm mostly focused on how to integrate it best with the framework we have been building, so not entirely without prejudice.

Why Docker?

if you are an absolute beginner in terms of Docker, I would suggest you take a look at this nice article on InfoWorld.

Why you should use Docker and containers
Learn how lightweight, portable, self-contained Docker containers improve software development, application deployment, and business agility

To highlight a few points:

  1. Docker enables more efficient use of system resources
  2. You create your image once and run it anywhere on your local, inside ci/cd, or any other setups
  3. With Cypress, you can use Docker in your Continuous Testing setups achieving better results

So before we start you can also take a look at this brief article to get some know-how of how it works.

So you want to get Cypress running in Docker?
so, this is going to be from an absolute beginners perspective, Cypress has great docs, but they assumed I had any idea how to set up…

In the beginning

First, you can clone this repo here and run the following command in the terminal opened in your folder path

$ npm install
npm install

this should install all the required node dependencies.

we're not going to create a docker file for this project from scratch rather use cypress-docker-images if you want to explore that take a look at this article

There are three different types of cypress-docker-images and we'll be using cypress/included for one simple reason - it has it all!

cypress-docker-images categories
cypress-docker-images/included/9.2.0 at master · cypress-io/cypress-docker-images
Docker images with Cypress dependencies and browsers - cypress-docker-images/included/9.2.0 at master · cypress-io/cypress-docker-images

Assuming you already have Docker installed on your machine, let’s dive into running already built image cypress/included:9.2.0 which is also the sole reason we first upgraded our project to Cypress v9.2.0

In the terminal opened in your project location

$ docker run -it -v $PWD:/e2e -w /e2e cypress/included:9.2.0

or if you are running windows like me then this command is slightly changed t0:

$ docker run -it -v ${PWD}:/e2e -w /e2e cypress/included:9.2.0

This command is broken down into the following arguments

Explanation of the “docker run” command-line arguments:

this command will run everything by getting the test specs details from cypress.json file and the result is as below:

Run Results

A bit explanation of what just happened:

cypress/included:9.2.0 has entry point set to ["cypress", "run"] which is equivalent of running npx cypress run on your local machine

which in our case is not that useful as everything is driven using runner.js which internally calls Cypress Module API.

Keep the container (Running from within the container)

Every time you run docker run you spawn a new container. That container then stops after the tests finish, but there is nothing Cypress can do about it - it is the Docker command docker run ... that controls this behavior.

If you are running a lot of tests, again and again, you might start the container once using Bash as the entry point, instead of the default cypress command. Then you can execute the cypress run or any other commands, while still in the same container:

In the terminal opened in your project location

PS D:\My____\Workspace\Cypress-TestFramework\Part 09> docker run -it -v ${PWD}:/e2e -w /e2e   --entrypoint=/bin/bash cypress/included:9.2.0

root@BE4400671ae7:/e2e#
npm install

further checking the content of the directory, it is the same as the one on your local machine

root@BE4400671ae7:/e2e# ls
cypress  cypress.json  node_modules  package-lock.json  package.json  reports  runner.js
npm install

now you can go ahead and run the following command inside the docker, which is nothing but a script alias for command "node runner.js cypress run --env TAGS="@API" configFile=qa"

root@c239a1b9c99b:/e2e# npm run cy:test:qa
Executing tests inside Cypress docker container with ENTRYPOINT /bin/bash

As you can see using our custom script we only ran the tests tagged with @API.

Run Result

Once all the tests are finished results will be stored inside the originally mapped /e2e folder

Reports Directory

Closing Notes:

In this article, we solely saw how we might run our Cypress Tests from the Cypress-TestFramework within a Docker container. How we actually leverage this in the ci/cd we will dive into that in an upcoming article "Running Cypress Tests in Github Actions (Part 10)".

Lest the reader complain; I didn't cover many different aspects of Docker such as building custom images and pushing them into Docker registry such as DockerHub, I might take a dig at that in the future. (Edit: you can read this article for more info )

さよなら Sayonara

Useful links:

Run Cypress included from Docker container
In the blog post “Run Cypress with a single Docker command” I gave several examples running Cypress Docker image cypress/included using docker-compose command. I also showed how to run Cypre
Docker | Cypress Documentation
Images https://github.com/cypress-io/cypress-docker-images This repo holds various Docker images for running Cypress locally and in CI. There are Docker
Run Cypress with a single Docker command
If you are a Node.js developer, installing Cypress as a dev dependency in your package.json file or even via direct download seems simple enough. Yet for developers working in other languages like Python or Go, using npm can be an obstacle. We often hear this question from developers:

Github Code:
https://github.com/far11ven/Cypress-TestFramework/tree/develop/Part 09