Skip to content

#149: Multiple Browsers in Selenium Grid

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.

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):

# 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:

docker-compose up

If you go to the hub (http://localhost:4444/ui#), you should see something like this:

There should be 4 nodes waiting

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.