如何在 docker 中 运行 selenium-side-运行ner?

How to run selenium-side-runner in docker?

我需要 运行selenium-side-运行ner in docker.I wrote in the dockerfile to install Google Chrome and googledrive.但是执行代码时,报错如下:

 WebDriverError: unknown error: Chrome failed to start: exited abnormally.
 (unknown error: DevToolsActivePort file doesn't exist)
 (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
 at Object.throwDecodedError (../../usr/lib/node_modules/selenium-side-runner/node_modules/selenium-webdriver/lib/error.js:550:15)
 at parseHttpResponse (../../usr/lib/node_modules/selenium-side-runner/node_modules/selenium-webdriver/lib/http.js:560:13)
 at Executor.execute (../../usr/lib/node_modules/selenium-side-runner/node_modules/selenium-webdriver/lib/http.js:486:26)

这是我的 dockerfile

FROM python:3.6-stretch

# install google chrome
RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> 
/etc/apt/sources.list.d/google-chrome.list'
RUN apt-get -y update
RUN apt-get install -y google-chrome-stable

# install chromedriver
RUN apt-get install -yqq unzip
RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS 
chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip
RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/

# set display port to avoid crash
ENV DISPLAY=:99

RUN apt-get update
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash
RUN apt-get install -y nodejs
RUN apt-get install -y npm
RUN npm install -g selenium-side-runner

WORKDIR /app

ENTRYPOINT ["/entrypoint.sh"]

这是我的代码:

for ind, file in enumerate(test_file_list):
    file_path = os.path.join(self.save_path, file)
    start_time = timezone.now()
    result = subprocess.run(
        ['selenium-side-runner', '-c', "goog:chromeOptions.args=[--headless,--nogpu] 
         browserName=chrome",file_path])
    end_time = timezone.now()
    result_code = result.returncode

黑客攻击

与其手动安装浏览器,我建议使用已经可用的图像,例如 https://github.com/SeleniumHQ/docker-selenium

一种解决方案(推荐)

从架构的角度来看,每个 Docker 容器应该只有一个用途。在您的情况下,容器负责 运行 浏览器和测试。这就是为什么我建议将解决方案分成两个容器:

  • 包含 selenium-side-runner.side 文件的第一个容器将 运行 测试
  • 第二个容器运行是一个浏览器

基于 the docsselenium-side-runner 可以使用 Selenium Grid。

所以,解决方案看起来像

  1. 准备 Dockerfile 安装 selenium-side-runner,添加 .side 文件,并配置 ENTRYPOINT。在这里,您还可以为 Selenium Grid 设置默认值 URL,例如 selenium-side-runner -w 10 --server http://selenium-browsers:4444/wd/hub,其中 selenium-browsers 将是带有浏览器或 Selenium Grid 的容器的名称。
  2. 构建您的自定义 Docker 图像
  3. docker network create selenium-tests
  4. 创建 Docker 网络
  5. 运行 浏览器或 Selenium Grid
    • 选项 1:浏览器。 docker run -id --shm-size=2g --network selenium-tests --name selenium-browsers selenium/standalone-chrome:3.141.59-2020040 基于 SeleniumHQ/docker-selenium
    • 选项 2:Selenium 网格。 SeleniumHQ/docker-selenium provides instructions how to do this. Also, you may look at Zalenium or Selenoid 提供视频录制、测试执行预览等附加功能
  6. 运行 你的容器 docker run -it --network selenium-tests --name my-tests <your custom image>

由于两个容器都在同一个自定义网络中 selenium-tests,两个容器可以通过容器名称进行通信。因此,my-tests 容器可以将请求发送到 selenium-browsers 容器(参见 #1 和 --server 选项)。

如果需要,您可以稍后创建 docker-compose.yaml 文件以简化解决方案的使用。

您可以使用 this project,将您的 .side 文件放在 side 目录中,然后 运行 docker-composer up