Skip to content

#150: Create a Dynamic Selenium Grid

While preparing the code samples to record videos of my test runs, I run into a severe problem: Selenium Grid cannot do what I need it to do - at least not with the approach I had so far. Let's regroup and switch the way we create the Selenium Grid.

Stumbling block: The video container

Selenium Grid offers video containers that will do the recording of the tests. However, if we add them to our existing Docker compose file, we get a video for the entire time the container was running. This may be better than nothing, but if your container is running for hours, it becomes tedious to find the right spot in the video.

A much better way is to use a dynamic Grid that throws away the containers as soon as our test finishes and by doing so creates a video per test run as we require.

Create a dynamic Grid

The dynamic Grid offers us a lot of flexibility. We only need to define the types of browsers we want to use; the rest is done by the Grid. We can define the browsers in a config.toml file like this one from the docker-selenium repository:

[docker]
# Configs have a mapping between the Docker image to use 
# and the capabilities that need to be matched to
# start a container with the given image.
configs = [
    "selenium/standalone-firefox:4.5.3-20221024", "{\"browserName\": \"firefox\"}",
    "selenium/standalone-chrome:4.5.3-20221024", "{\"browserName\": \"chrome\"}",
    "selenium/standalone-edge:4.5.3-20221024", "{\"browserName\": \"MicrosoftEdge\"}"
    ]

# URL for connecting to the docker daemon
url = "http://host.docker.internal:2375"
# Docker image used for video recording
video-image = "selenium/video:ffmpeg-4.3.1-20221104"

From the same repository we can get the docker-compose-v3-dynamic-grid.yml file and modify it slightly to work on Windows:

version: "3"
services:
  node-docker:
    image: selenium/node-docker:4.5.3-20221024
    volumes:
      - ./assets:/opt/selenium/assets
      - ./config.toml:/opt/bin/config.toml
      # - /var/run/docker.sock:/var/run/docker.sock
    depends_on:
      - selenium-hub
    environment:
      - SE_EVENT_BUS_HOST=selenium-hub
      - SE_EVENT_BUS_PUBLISH_PORT=4442
      - SE_EVENT_BUS_SUBSCRIBE_PORT=4443

  selenium-hub:
    image: selenium/hub:4.5.3-20221024
    container_name: selenium-hub
    ports:
      - "4442:4442"
      - "4443:4443"
      - "4444:4444"

I changed the absolute path for the assets directory to a relative one and commented out the entry for docker.sock in the volume section. That was enough to get it working on Windows.

Unfortunately, the original version 4.6.0-20221104 got me only errors. Therefore, I downgraded on the previous version 4.5.3-20221024 for all containers.

As a final point of preparation, you should verify that Docker Desktop exposes the daemon without TLS in the settings dialog:

Check the option to expose the daemon without TLS.

Run the dynamic Grid

We can run our dynamic Grid with this command:

docker-compose -f docker-compose-v3-dynamic-grid.yml  up  

If everything works, we can go to http://localhost:4444/ui# where we should see 20 browsers per defined type:

The Grid now should have 20 Firefox, 20 Edge and 20 Chrome browsers ready for us.

Don't panic, the Grid did not start 60 containers on your machine. Only when we ask for a browser will Selenium Grid create a container – that is the dynamic part.

Next

We can run our existing tests without any changes against the newly created dynamic Grid. Everything should work as it did with the static Grid. Next week we look at the dials we must turn on to record the videos for our test runs.