Search code examples
pythondockerselenium-webdriverpython-behave

Running behave/selenium in a Docker Container


I have built a Docker container, from which to run behave/selenium tests. The Docker image is based on Oracle Linux 9, and it includes:

/bdds/driver/chromedriver --version
ChromeDriver 114.0.5735.90 (386bc09e8f4f2e025eddae123f36f6263096ae49-refs/branch-heads/5735@{#1052})

 /usr/lib64/chromium-browser/chromium-browser --version
Chromium 122.0.6261.128 Fedora Project

I have an environment.py file, which sets up the driver:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from lib import ohaitestutl as ohai

def before_scenario(context, scenario):
    base_url = ohai.config_property(config_section='url', config_property='base_url')
    context.base_url = base_url
    options = Options()
    headless_browser = context.config.userdata.get('headless')
    if headless_browser and headless_browser.lower() == 'true':
        options.add_argument('--headless')
        context.headless = True
    else:
        context.headless = False
    context.driver = webdriver.Chrome(options=options)
    context.driver.maximize_window()

def after_scenario(context, scenario):
    # Close the browser session after each scenario
    if context.driver:
        context.driver.quit()

Installed to the container is a bdds.sh script:

#!/usr/bin/env bash
# D-Bus initialization command
DBUS=$(ps -ef | grep "dbus-daemon" | grep -v grep)
if [ -z "${DBUS}" ]
then
  mkdir -p /run/dbus
  dbus-daemon --system &
fi
export PATH=$PATH:/bdds/driver:/usr/lib64/chromium-browser
echo behave -D headless=true features/foreg_login.feature
behave -D headless=true features/foreg_login.feature

When executed there is a delay of a minute or so, and then I get this:

PS C:\Development\CICD\UITests> docker run --name TBDDS bdds
behave -D headless=true features/foreg_login.feature

Feature: Login credentials validation for front office registration 
HOOK-ERROR in before_scenario: WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/chromium-browser is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
Stacktrace:
#0 0x55a3ff7ff4e3 <unknown>
#1 0x55a3ff52ec76 <unknown>
#2 0x55a3ff557d78 <unknown>
#3 0x55a3ff554029 <unknown>
#4 0x55a3ff592ccc <unknown>
#5 0x55a3ff59247f <unknown>
#6 0x55a3ff589de3 <unknown>
#7 0x55a3ff55f2dd <unknown>
#8 0x55a3ff56034e <unknown>
#9 0x55a3ff7bf3e4 <unknown>
#10 0x55a3ff7c33d7 <unknown>
#11 0x55a3ff7cdb20 <unknown>
#12 0x55a3ff7c4023 <unknown>
#13 0x55a3ff7921aa <unknown>
#14 0x55a3ff7e86b8 <unknown>
#15 0x55a3ff7e8847 <unknown>
#16 0x55a3ff7f8243 <unknown>
#17 0x7fa57fa82812 start_thread

My Dockerfile:

# =============================================================================
# Selenium Python BDDS Automated Testing
# Date: 25 Mar 2024
#
# This docker file must be run via the BDDS docker_build.sh script.
# =============================================================================

# Stage 1: Use Oracle Linux 9 base image
FROM oraclelinux:9

# Update package lists
RUN dnf update -y
# Enable EPEL repository for Chromium
RUN yum -y update && yum -y install epel-release 
RUN yum -y install chromium

# Install D-Bus package
RUN yum install -y dbus dbus-daemon

# Start D-Bus server at container startup
CMD ["dbus-daemon", "--system"]

# Install dependencies
RUN dnf install -y --setopt=timeout=60000 python3.11 python3-pip wget unzip vim nss-tools libX11

# Create a directory for the bdds framework files and features
RUN mkdir -p /bdds/driver
WORKDIR /bdds/driver

# Deploy the Selenium Chrome driver
COPY chromedriver_linux64.zip /bdds/driver 
RUN unzip -o chromedriver_linux64.zip

RUN python3.11 -m ensurepip
RUN python3.11 -m pip install behave

WORKDIR /bdds
# Install the required Python dependencies
COPY requirements.txt /bdds/
RUN python3.11 -m pip install -r requirements.txt

# Copy the run.sh script
COPY run.sh /etc
COPY bdds.sh /bdds
RUN chmod 755 /bdds/bdds.sh

COPY bdds_temp.zip /bdds/bdds.zip
RUN unzip -o bdds.zip

# Set working directory
WORKDIR /bdds

# Set execute permissions for bash scripts.
RUN chmod 755 /bdds/*.sh
RUN chmod 755 /etc/run.sh


ENTRYPOINT ["/bdds/bdds.sh"]

Any help appreciated. Thanks.


Solution

  • It's hard to know precisely what the problem is but I suspect that you do not have compatible versions of Chrome and ChromeDriver on the image or that there are missing dependencies.

    Here's a simplified Dockerfile:

    FROM oraclelinux:9
    
    RUN dnf update -y && \
        dnf install -y \
            wget \
            unzip \
            python3.11 \
            python3-pip \
            dbus-x11 \
            gtk3 \
            nss \
            atk \
            at-spi2-atk \
            cups-libs \
            libdrm \
            libxkbcommon \
            libXcomposite \
            libXdamage \
            libXrandr && \
        dnf clean all
    
    RUN wget -q -O chrome-linux64.zip https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/121.0.6167.85/linux64/chrome-linux64.zip && \
        unzip chrome-linux64.zip && \
        rm chrome-linux64.zip && \
        mv chrome-linux64 /opt/chrome/ && \
        ln -s /opt/chrome/chrome /usr/local/bin/ && \
        wget -q -O chromedriver-linux64.zip https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/121.0.6167.85/linux64/chromedriver-linux64.zip && \
        unzip -j chromedriver-linux64.zip chromedriver-linux64/chromedriver && \
        rm chromedriver-linux64.zip && \
        mv chromedriver /usr/local/bin/
    
    WORKDIR /bdds
    
    COPY requirements.txt .
    RUN pip3 install -r requirements.txt
    
    COPY bdds.sh .
    COPY features ./features
    
    RUN chmod 755 bdds.sh
    
    CMD ["./bdds.sh"]
    

    IMHO the most important component of this is ensuring that you have consistent and compatible versions of Chrome and ChromeDriver. Chrome also has a bunch of dependencies that need to be installed otherwise the browser won't launch.

    Here's the content of my test setup:

    ├── bdds.sh
    ├── Dockerfile
    ├── features
    │   ├── open_browser.feature
    │   └── steps
    │       └── browser_steps.py
    └── requirements.txt
    

    🗎 requirements.txt

    behave==1.2.6
    selenium==4.19.0
    

    🗎 open_browser.feature

    Feature: Open web browser
      Scenario: Visit Google
        Given I have a web browser
        When I navigate to "http://www.google.com"
        Then I can see the Google search page
    

    🗎 browser_steps.py

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from behave import given, when, then
    
    @given('I have a web browser')
    def step_impl(context):
        chrome_options = Options()
        chrome_options.add_argument("--headless")
        chrome_options.add_argument("--no-sandbox")
        chrome_options.add_argument("--disable-gpu")
        
        context.browser = webdriver.Chrome(options=chrome_options)
    
    @when('I navigate to "{url}"')
    def step_impl(context, url):
        context.browser.get(url)
    
    @then('I can see the Google search page')
    def step_impl(context):
        assert "Google" in context.browser.title
        context.browser.quit()
    

    🗎 bdds.sh

    #!/usr/bin/env bash
    
    dbus-uuidgen > /var/lib/dbus/machine-id
    mkdir -p /var/run/dbus
    dbus-daemon --config-file=/usr/share/dbus-1/system.conf --print-address
    
    chrome --version
    chromedriver --version
    
    behave
    

    I'm not convinced that the DBUS commands are strictly required but they do suppress a few warning messages, so happy to leave them in. The commands to get the versions of Chrome and ChromeDriver can also be excised. Just wanted to confirm that they have a consistent version.

    enter image description here

    This can be updated to use an environment file.

    🗎 environment.py

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    def before_scenario(context, scenario):
        chrome_options = Options()
        chrome_options.add_argument("--headless")
        chrome_options.add_argument("--no-sandbox")
        chrome_options.add_argument("--disable-gpu")
    
        context.driver = webdriver.Chrome(options=chrome_options)
        context.driver.maximize_window()
    
    def after_scenario(context, scenario):
        if context.driver:
            context.driver.quit()
    

    🗎 browser_steps.py

    from behave import given, when, then
    
    @given("I have a web browser")
    def step_impl(context):
        pass
    
    @when('I navigate to "{url}"')
    def step_impl(context, url):
        context.driver.get(url)
    
    @then("I can see the Google search page")
    def step_impl(context):
        assert "Google" in context.driver.title
        context.driver.quit()