Scale up docker images for Selenium Parallel Tests

This post helps you scale up docker images to do parallel selenium tests. And, if you’re a Docker newbie, keep this as a hierarchy in your mind before getting into below concepts – Docker Machine > Docker Image > Docker containers

  • Download and install Docker
for MAC
# download complete docker community edition
brew cask install docker-toolbox

Optional (not important)
# download docker-machine 
curl -L https://github.com/docker/machine/releases/download/v0.15.0/docker-machine-$(uname -s)-$(uname -m) >/usr/local/bin/docker-machine &
# convert docker-machine file as executable 
chmod +x /usr/local/bin/docker-machine

For Linux and Windows,
refer: https://github.com/docker/machine/releases

# download docker-compose 
curl -L https://github.com/docker/compose/releases/download/1.23.0-rc2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
# convert docker-compose file as executable 
chmod +x /usr/local/bin/docker-compose

For Linux and Windows,
refer: https://github.com/docker/compose/releases
  • Download and install VirtualBox
MAC
brew cask install virtualbox
  • Create a Docker machine
# to create docker machine
docker-machine create tests

# list down all the available docker machines
docker-machine ls

# to delete docker machine
docker-machine rm tests
  • Kickstart the Docker machine
docker-machine start tests
  • Set environment variables to command docker towards a specific docker machine; say, tests
eval $(docker-machine env tests)
  • Create docker-compose.yml in the project’s root folder and add all the possible images required for the tests. There are different variants to create images and containers; say, docker run -d -p 4444:4444 --net grid --name selenium-hub selenium/hub​; however, creating them through docker-compose.yml is much appreciated for real-time projects
version: "3"
services:
  selenium-hub:
    image: selenium/hub
    container_name: selenium-hub
    ports:
      - "4444:4444"
  chrome:
    image: selenium/node-chrome
    depends_on:
      - selenium-hub
    environment:
      - HUB_HOST=selenium-hub
      - HUB_PORT=4444
  firefox:
    image: selenium/node-firefox
    depends_on:
      - selenium-hub
    environment:
      - HUB_HOST=selenium-hub
      - HUB_PORT=4444
  • Run docker compose to create containers with hub and nodes for the tests to run
docker-compose up

# scale up multiple containers
docker-compose up -d --scale chrome=2
(say, I need 2 containers with chrome instances)
  • Get IP address of the docker machine and use it as a Selenium remote hub url
docker-machine ip tests

# get machine ip with details
env | grep DOCKER

# get container ip 
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' selenium-hub
  • Copy & paste docker machine IP as below; here, xxx.xxx.xxx.xxx is the docker-machine IP (say, 192.168.99.100)
@driver = Selenium::WebDriver.for :remote, url: "http://xxx.xxx.xxx.xxx:4444/wd/hub"
  • As a teardown case, stop/delete/manipulate the containers & images if needed
# list complete details about a specific container
docker logs --details CONTAINER_ID
(e.g., docker logs --details 39d9d7475bf1)
docker logs CONTAINER_NAME
(e.g., docker logs selenium-hub)

# delete a container of an image
docker container ls -a
docker ps -a
(here, docker ps is the alias for above cmd to list containers)
docker container rm CONTAINER_ID
(e.g., docker container rm 39d9d7475bf1)

# stop/restart a container of an image
docker container stop CONTAINER_ID
docker container restart CONTAINER_ID

# start all the previously stopped/exited containers
docker start $(docker ps -a -q --filter "status=exited")

# delete an image of a docker machine
docker image ls -a
docker image rm IMAGE_NAME
(e.g., docker image rm nginx)

# log into a docker container
docker exec -it CONTAINER_NAME/ID bash
  • Once the docker-compose up is triggered, the selenium grid initiates hub and nodes as shown below

  • Now, run your parallel tests through command line; say, ruby parallel_tests
parallel_tests feature/scenarios/**/*.feature
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s