【问题标题】:How to run Selenium sind runner in GitLabCI?如何在 GitLab CI 中运行 Selenium side runner?
【发布时间】:2022-06-10 22:09:14
【问题描述】:

我目前正在评估 Selenium 与 GitLab CI 的结合,作为我们网站的测试工具。这是我目前的.gitlab-ci.yml

variables:
    GIT_STRATEGY: clone
    GIT_DEPTH: 0

stages:
    - tests

test:
    stage: tests
    image: node:latest
    tags:
        - linux
    before_script:
        - apt-get update
        - apt-get install -y chromium
        - npm install -g selenium-side-runner
        - npm install -g chromedriver
    script:
        - selenium-side-runner My-UI-Test.side

我收到以下错误:

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

我搜索了错误消息DevToolsActivePort file doesn't exist,似乎 Chrome 不喜欢以 root 权限运行。许多答案建议使用--no-sandbox--disable-dev-shm-usage 标志。但这些是 Chrome 标志,因为我没有直接调用 Chrome,所以我不能使用它们。有问题的网站也是从不同的项目部署的,所以我没有代码可以使用。我唯一可以更改的文件是My-UI-Test.side.side.yaml

【问题讨论】:

  • But those are Chrome flags, and since I'm not calling Chrome directly 您可以使用 selenium 添加命令行选项 webdriver api 用于 chrome 选项或 specify them in side。如果您在无头(无屏幕)环境中运行 chrome,您可能还需要添加无头参数。
  • @sytech 我尝试了selenium-side-runner -c "goog:chromeOptions.args=[no-sandbox,disable-dev-shm-usage,headless]" My-UI-Test.side,但得到了错误Target browser must be a string, but is <undefined>; did you forget to call forBrowser()?。我在.side.yaml 中有浏览器名称,还尝试将其作为-c 选项,但没有任何改变。

标签: selenium selenium-chromedriver gitlab-ci selenium-side-runner


【解决方案1】:

我有一个单独的项目用于我的 e2e 测试,我在其中添加了一个 Dockerfile、我的 selenium .side 文件以及一个配置文件 .side.conf。本项目使用 gitlab docker registry 将项目作为镜像上传,可以直接加载到 gitlab-ci 中。

这是我的 e2e 测试项目文件:

  • package.json
...
  "scripts": {
    "test": "selenium-side-runner test.side"
  },
...
  "dependencies": {
    "selenium-side-runner": "^3.17.0",
    "chromedriver": "^101.0.0"
  }

这些是我正在使用的选项,您可能想在这里和那里调整一些东西。不过,这些功能几乎是您想要的。

我还将 baseUrl 键添加到此文件中,而不是直接添加到 package.json 中,因为我在几个具有更改 URL 的环境中使用相同的图像,我会在需要时在我的 before_script 中替换它。 (我在下面省略了这个,因为您的用例可能不同)

  • .side.yml
capabilities:
  browserName: "chrome"
  goog:chromeOptions:
    binary: /usr/bin/google-chrome-stable
    args:
      - no-sandbox
      - disable-dev-shm-usage
      - headless
      - nogpu
output-directory: results
output-format: junit
baseUrl: <baseURL>

Dockerfile 可能包含一些无用的依赖项,您可以删除其中的很多。其中许多只是从我的 puppeteer Dockerfile 复制过来的,因为它们使用的 google-chrome-stable 二进制文件非常相似。在您的情况下,也可能不需要使用 google-chrome-stable 二进制文件下载字体。因此,只需根据您的需要进行调整即可。

  • Dockerfile
FROM node:14

RUN apt update

RUN apt install -y \
    rsync \
    grsync \
    gnupg \
    ca-certificates \
    fonts-liberation \
    libappindicator3-1 \
    libasound2 \
    libatk-bridge2.0-0 \
    libatk1.0-0 \
    libc6 \
    libcairo2 \
    libcups2 \
    libdbus-1-3 \
    libexpat1 \
    libfontconfig1 \
    libgbm1 \
    libgcc1 \
    libglib2.0-0 \
    libgtk-3-0 \
    libnspr4 \
    libnss3 \
    libpango-1.0-0 \
    libpangocairo-1.0-0 \
    libstdc++6 \
    libx11-6 \
    libx11-xcb1 \
    libxcb1 \
    libxcomposite1 \
    libxcursor1 \
    libxdamage1 \
    libxext6 \
    libxfixes3 \
    libxi6 \
    libxrandr2 \
    libxrender1 \
    libxss1 \
    libxtst6 \
    lsb-release \
    wget \
    xdg-utils

RUN apt-get update \
    && apt-get install -y wget gnupg \
    && wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
    && sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
    && apt-get update \
    && apt-get install -y google-chrome-stable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 \
      --no-install-recommends \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /app

COPY package.json /app
COPY .side.yml /app
COPY test.side /app
COPY results /app

RUN npm i -g chromedriver --unsafe-perm
RUN npm i -g selenium-side-runner --unsafe-perm

RUN npm install

在这里我将它包含到我的 gitlab CI 中

  • gitlab-ci.yml:
e2e:
  stage: test
  image: <image-of-above-project>:1.0
  variables:
    GIT_STRATEGY: none
  script:
    - cat .side.yml
    - npm run test

如果您需要有关容器注册表的更多信息,请访问此处:https://docs.gitlab.com/ee/user/packages/container_registry/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-13
    • 2019-11-29
    • 1970-01-01
    • 1970-01-01
    • 2022-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多