在 Docker 容器中运行Behavior/Selenium

问题描述 投票:0回答:1

我构建了一个 Docker 容器,可以从中运行行为/selenium 测试。 Docker 镜像基于 Oracle Linux 9,它包括:

/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

我有一个环境.py 文件,用于设置驱动程序:

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

安装到容器的是bdds.sh脚本:

#!/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

执行时会有一分钟左右的延迟,然后我得到:

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

我的 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"]

任何帮助表示赞赏。谢谢。

python docker selenium-webdriver python-behave
1个回答
0
投票

这是一个简化的

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"]

恕我直言,其中最重要的部分是确保您拥有一致且兼容的 Chrome 和 ChromeDriver 版本。 Chrome 还有一堆需要安装的依赖项,否则浏览器将无法启动。

这是我的测试设置的内容:

├── 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()

© www.soinside.com 2019 - 2024. All rights reserved.