Selenium testing in docker compose environment

I have 3 docker containers:

  1. Web app
  2. End to end tests: These are selenium tests for the above web app
  3. Selenium: I am using this: https://hub.docker.com/r/selenium/standalone-chrome

I am spinning up the containers with compose as follows:

  web-app:
    build:
      context: .
      dockerfile: web.dockerfile
    image: web-app
    command: poetry run ./manage.py runserver 0.0.0.0:8000
    ports:
      - 8000:8000

  e2e-tests:
    build:
      context: .
      dockerfile: e2etests.dockerfile
    image: e2e-tests
    volumes:
      - ./e2e_tests:/app

  selenium:
    image: selenium/standalone-chrome:91.0
    ports:
      - 4444:4444

The e2etests.dockerfile simply installs python and the selenium python package. Nothing fancy.

Here’s a very basic selenium test in python:

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--window-size=1420,1080')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver_1 = webdriver.Remote("selenium:4444/wd/hub", options=chrome_options)

driver_1.get(‘http://web-app:8000/')

assert 'Api Root' in driver_1.title

driver_1.quit()

The above test does not work. It hangs on the web driver.Remote() step. It looks like that step is unable to resolve the IP address or dns of the other container. I figured this out by typing docker inspect container_id | grep IPAddress to get the IP address of the selenium container.

I then updated that step to use the IP address as follows:

driver_1 = webdriver.Remote("http://ip_address_here:4444/wd/hub", options=chrome_options)

The tests now works.

So my question is, how do I spin up the containers from compose in such a way that I can use their host names in the web driver.Remote() step instead of having to manually figure out what the IP address is.

Source: Docker Questions

LEAVE A COMMENT