Python Friday #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.

This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.

 

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:

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

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:

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.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.