How to create multiple selenium docker nodes dynamically?

1.7k views Asked by At

I wanted to create a Jenkins Job that could remove and create Selenium grid HUB/Node before my automation execution starts. The job should be able to create number of nodes based on the job parameter. Request if some one could provide the code to perform the same ?

1

There are 1 answers

0
Yorgos Katsaros On

If I understand correctly, what you describe isn't exactly dynamic container creation/removal, since you will specify the number of nodes in the job parameter.

However, one way to achieve this is with docker-compose. You could have a step in your jenkins job where you would spin up the grid via docker-compose (scaled with the number of containers you want) and then remove the grid again at the post-stage. This could be achieved with jenkins pipelines (jenkinsfile).

So, the docker-compose.yml could be something like the following:

version: '3'
services:

  hub:
    image: selenium/hub
    container_name: automation_hub
    ports:
      - "4444:4444"

    environment:
      GRID_MAX_SESSION: 16
      GRID_BROWSER_TIMEOUT: 3000
      GRID_TIMEOUT: 3000

  chrome:
    image: selenium/node-chrome
    depends_on:
      - hub
    environment:
      HUB_PORT_4444_TCP_ADDR: hub
      HUB_PORT_4444_TCP_PORT: 4444
      NODE_MAX_SESSION: 2
      NODE_MAX_INSTANCES: 2
    volumes:
      - /dev/shm:/dev/shm
    links:
      - hub

Then, you should add a shell step in your jenkins job with the parameter for the number of chrome containers you want to create.

docker-compose -f <docker-compose.yml filepath> up --scale chrome=$CONTAINERS

Now, you should have your grid running. When you trigger your build you should include the parameter CONTAINERS. Just point your tests at "http://localhost:4444/wd/hub" and they should run on the grid

Finally, create a shell step with the command:

docker-compose down

to remove your containers from the grid. Ideally, this step should be on the post stage.