🟨Run Tests in Containers

How to run tests in containers like Docker

Configure the Test Run

Regardless of the scaling option you go with (Selenoid, Zalenium, Docker vs Kubernetes, etc.), you will need to connect your tests to a Remote URL.

You can do this two ways:

  • Update remote_url in pylenium.json

  • Pass in the argument when running the tests in the CLI

Run Tests in CLI

This is the most common option since it is what you will use in your pipelines and CI

Terminal
pytest tests/ui -n 2 --remote_url="http://localhost:4444/wd/hub"

Update pylenium.json

pylenium.json
"remote_url": "http://localhost:4444/wd/hub"

You can have multiple pylenium.json files, so you might have:

  • dev-pylenium.json for local development

  • ci-pylenium.json for CI Pipelines

Then pick which config file to use in the CLI. For example, in a CI Pipeline:

Terminal
pytest pylenium_json=ci-pylenium.json

Config Layers

  • Layer 1 - pylenium.json is deserialized into PyleniumConfig

  • Layer 2 - If there are any CLI args, they will override their respective values in PyleniumConfig

Docker Example

With Docker installed, you can easily spin up a Selenium Grid with the docker-compose command.

docker-compose.yml

You will need a docker-compose.yml file and then open a Terminal in the same directory as this file.

docker-compose.yml
version: "3"
services:

  selenium-hub:
    image: selenium/hub
    ports:
      - "4444:4444"
    environment:
        GRID_MAX_SESSION: 16
        GRID_BROWSER_TIMEOUT: 300
        GRID_TIMEOUT: 300

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

  firefox:
    image: selenium/node-firefox
    depends_on:
      - selenium-hub
    environment:
      HUB_PORT_4444_TCP_ADDR: selenium-hub
      HUB_PORT_4444_TCP_PORT: 4444
      NODE_MAX_SESSION: 4
      NODE_MAX_INSTANCES: 4

This configuration will spin up a Hub node (load balancer), a Chrome node with 2 available drivers and a Firefox node with 4 available drivers.

Spin up the Grid

With a single command you will have all of this created for you:

Terminal $
docker-compose up -d

Once complete, you can visually see this Grid by going to http://localhost:4444/grid/console

Now Configure the Test Run (steps at top of this doc) to target the Hub which will balance the tests across its Nodes.

Scale Nodes

With the YAML file example above, it will create 1 chrome Node with 2 available drivers by default. You can easily scale this to the number you need.

Terminal $
docker-compose up -d --scale chrome=5

This will spin up the Grid with 5 chrome Nodes!

Tear Down the Grid

When you're done using the Grid, a single command will tear it completely down.

Terminal $
docker-compose down

Last updated