Docker + Compose + Selenium Grid = Automation awesomeness!


I have been trying to get my hands dirty with Docker and Selenium for a while. Finally what inspired me was a recent meetup where I saw some cool test automation reporting frameworks. No I did not see Docker there, but when I researched about Allure the test reporting framework I stumbled upon this cool video where the developer has used Docker Selenium and Allure:



Why Selenium Grid & Docker?

If you have been through the journey of CI (continuous integration), as an automation engineer you would know the challenges of building a reliable framework is time consuming.
This concept has revolutionized our way of thinking of how you build a selenium Grid, no more config mgmt/provisioning machines. All you need is a VM that can run the docker images as a container.

Contributors to this project who have made it a reality:
Matt Smith
Leo Galluci 


Selenium grid has been there for a while and matured with time. What it does really well is speed up your CI massively! How? By running them in parallel.Now for anyone who has used the grid before knows that maintaining the hubs /nodes / os/ browser combinations is a challenge.
The bigger challenge than that is the 
  1. Virtual Machines / Networked physical machines / VDI etc 
  2. They need to have the selenium server running correctly on them
  3. If something goes wrong debugging the node
  4. Collating data and reporting
Solutions:
The answer to the above problems is what we are looking at unless you want to spend some money to get SauceLabs/ BrowserStack / Rainforest. Here is a good diff between the three: (A topic for some other blog post)

Docker for the newbies:
If you haven't heard of Docker you haven't been reading blogs/attending meetups/conferences. In simple words Docker is a scaled down Virtual Machine that allows you to package all that you need - aaps - database - dependencies - configs - libraries - frameworks and so on into a standardized portable container. 
For detailed info read on at the Docker's website - Build, Ship, Run

What happens when the two meet?
If you want speed , efficiency and something cool in the test automation world - Enter Docker Compose. Once you have read about Compose on the official site you would know where we are heading to: Pre-configured selenium clusters that run on newly spun Docker images

The three amigos of test automation:
  1. Selenium Grid - that manages routing of tests in a hub/nodes format
  2. Docker - that configures browser and apps 
  3. Compose - The hub of the docker world that acts as central point from where everything is spun up on the go!
Fun facts:
  • Docker images run as user-space processes on a shared OS
  • These images are nothing but plain Dockerfiles
    • http://odewahn.github.io/docker-jumpstart/building-images-with-dockerfiles.html
    • Dockerfile
  • Therefore these images share same resources
  • but are still isolated and require far fewer resources to run than a VM
Let's get to the point:

1. Install Docker Compose - https://docs.docker.com/compose/install/
2. Verify your installation
The installer places Docker Toolbox and VirtualBox in your Applications folder. In this step, you start Docker Toolbox and run a simple Docker command.
  1. On your Desktop, find the Docker Toolbox icon.
    Desktop
  2. Click the icon to launch a Docker Toolbox terminal.
    If the system displays a User Account Control prompt to allow Virtual-box to make changes to your computer. Choose Yes.
    The terminal does several things to set up Docker Toolbox for you. When it is done, the terminal displays the $ prompt.
    Desktop
    The terminal runs a special bash environment instead of the standard Windows command prompt. The bash environment is required by Docker.
  3. Type the docker run hello-world command or $ docker info
  3. Selenium Grid Hub
Create the hub on localhost - download and run container from Docker repository with selenium hub:
   $ docker pull selenium/hub
   $ docker pull selenium/node-chrome
   $ docker pull selenium/node-firefox
$ docker run -d ‐‐name seleniumAdy -hub -p 4444:4444 selenium/hub
When the container is downloaded navigate to http://localhost:4444/grid/console and you should see an empty grid console

  4. Selenium Grid Nodes
Firefox profile
$ docker run -d -P ‐‐link selenium-hub:hub selenium/node-firefox
$ docker run -d --link selenium-hub:hub selenium/node-chrome:2.53.0

Chrome Profile:
$ docker run -d -P ‐‐link selenium-hub:hub selenium/node-chrome
$ docker run -d --link selenium-hub:hub selenium/node-chrome:2.53.0

All docker images here: There are 11 of them:
https://hub.docker.com/r/selenium/

  5. Validate the containers
$ docker logs hub
$ docker logs firefox
$ docker logs chrome
$ docker ps

Will list out all the containers(processes - ps) running. A hub and two nodes with Chrome and firefox



  6. Bring it all together with docker-compose
  1. Stop all running containers $ docker stop $(docker ps -a -q)
  2. create docker-compose.yml file that will decide how the images interact - The nodes need to be linked to the hub and the ports need to be defined:
  3. seleniumhub:
      image: selenium/hub
      ports:
        - 4444:4444
    
    firefoxnode:
      image: selenium/node-firefox
      ports:
        - 8000
      links:
        - seleniumhub:hub
    
    chromenode:
      image: selenium/node-chrome
      ports:
        - 8000
      links:
        - seleniumhub:hub
  4. Run it! $ docker-compose up -d
  5. Navigate to http://localhost:4444/grid/console and you should see everything as before , but with configs in a file 
  7. Scale it up!
$ docker-compose scale chromenode=20
$ docker-compose scale firefoxnode=30
Adds more nodes to the hub!

  8. Stop Docker
$ docker stop seleniumAdy

Happy Selenium Dockering!

Comments

  1. Excellent goods from you, man. I’ve understand your stuff previous to and you’re just too excellent. I actually like what you’ve acquired here, certainly like what you are stating and the way in which you say it. You make it enjoyable and you still take care of to keep it sensible. I can not wait to read far more from you. This is actually a tremendous site..

    Linux Training in Chennai

    ReplyDelete
  2. Nice post. This is the Docker image I use because it has VNC and video recording built in: https://github.com/elgalu/docker-selenium

    ReplyDelete
    Replies
    1. Thanks Francis!
      Oh yes I saw this one and that's the next thing to do.. I love how the video recordings have been embedded in reports.

      Cheers,
      Ady

      Delete
  3. is there a docker compose yml file just to spin up nodes and attached to a hub both running on differnet machine

    ReplyDelete
  4. Thanks for the post! It led me down the path of using a standalone selenium Docker container for my project: https://bitbucket.org/dorianpula/rookeries/. This approach was a lot easier and faster to setup than the SauceLabs route I was going on previously.

    ReplyDelete
  5. Thank you so much for your excellent blog! I really enjoy to visit your very interesting post, Well done!
    Chapter 7 Bankruptcy Lawyers Near Me
    Cost of Uncontested Divorce in Virginia

    ReplyDelete

Post a Comment

Popular posts from this blog

Software Testing @ Microsoft

Trim / Remove spaces in Xpath?