🟨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.jsonPass in the argument when running the tests in the CLI
Run Tests in CLI
pytest tests/ui -n 2 --remote_url="http://localhost:4444/wd/hub"Update pylenium.json
"remote_url": "http://localhost:4444/wd/hub"You can have multiple pylenium.json files, so you might have:
dev-pylenium.jsonfor local developmentci-pylenium.jsonfor CI Pipelines
Then pick which config file to use in the CLI. For example, in a CI Pipeline:
pytest pylenium_json=ci-pylenium.jsonConfig Layers
Layer 1 -
pylenium.jsonis deserialized into PyleniumConfigLayer 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.
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: 4This 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:
docker-compose up -dNow 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.
docker-compose up -d --scale chrome=5This 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.
docker-compose downLast updated