One of the greatest benefits of Selenium Grid is to run Selenium against multiple browsers. This can be multiple instances of one web browser that we run in parallel, or browsers from different vendors. Let’s see how we can configure a flexible solution without much effort.
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.
Multiple containers
We can use the official browser-specific images and combine them to a flexible grid. However, starting those containers one by one will be a pain. With docker-compose we already have a tool that is great to orchestrate a few containers. All we need to do is to create a docker-compose.yaml file and start it.
Create a docker-compose.yaml file
To create a flexible Selenium grid, we need one hub container that coordinates everything and for each browser instance we add a container called a node. The nodes need to know where the hub is and how they can reach it.
This docker-compose.yaml file creates one hub and four nodes (1 for Chrome, 1 for Edge, and 2 for Firefox):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# Creates a Selenium Grid with one Hub and 4 Nodes # (1x Chrome, 1x Edge, 2x Firefox) version: "3" services: chrome: image: selenium/node-chrome:4.6.0-20221104 shm_size: 2gb depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4442 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 edge: image: selenium/node-edge:4.6.0-20221104 shm_size: 2gb depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4442 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 firefox: image: selenium/node-firefox:4.6.0-20221104 shm_size: 2gb depends_on: - selenium-hub environment: - SE_EVENT_BUS_HOST=selenium-hub - SE_EVENT_BUS_PUBLISH_PORT=4442 - SE_EVENT_BUS_SUBSCRIBE_PORT=4443 firefox2: image: selenium/node-firefox:4.6.0-20221104 shm_size: 2gb 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.6.0-20221104 container_name: selenium-hub ports: - "4442:4442" - "4443:4443" - "4444:4444" |
Run the grid
Since we use Docker compose, we can use this command to start our grid:
1 |
docker-compose up |
If you go to the hub (http://localhost:4444/ui#), you should see something like this:
Use a specific browser
If we want to run our code against a Firefox browser, we can reuse the code from the last post:
1 2 3 4 5 |
firefox_options = webdriver.FirefoxOptions() driver = webdriver.Remote( command_executor='http://localhost:4444', options=firefox_options ) |
If we want to run with Chrome, we can change it to this:
1 2 3 4 5 |
chrome_options = webdriver.ChromeOptions() driver = webdriver.Remote( command_executor='http://localhost:4444', options=chrome_options ) |
And for Edge, we can use this code:
1 2 3 4 5 |
edge_options = webdriver.EdgeOptions() driver = webdriver.Remote( command_executor='http://localhost:4444', options=edge_options ) |
Everything else in our code is the same and works for each browser.
Next
We can extend our docker-compose.yaml and add more containers as our requirements grow. Next week we look at creating a more dynamic Selenium Grid.
Hi, have you tried the playwright lib?
I have recently moved my projects to that lib and I found it much easier to use. as well, it handles lots of brower’s events.
worth giving it a look.
Best
Hi nono,
Yes, I tried the Playwright lib. You can find the posts here: https://improveandrepeat.com/tag/playwright/
In a few weeks I will write about Playwright with Python, so far I run it with C#.
Regards,
Johnny