Gitlab ci selenium 测试 docker 未连接到 RemoteWebDriver

Gitlab ci selenium testing with docker not connecting to RemoteWebDriver

我想 运行 使用 gitlab 自动进行 selenium 测试-ci, docker.

本地一切正常,但似乎 docker 和 selenium 存在一些连接问题。

作业失败

selenium.test.dashboard.MyTest > myFirstTest FAILED
    org.openqa.selenium.remote.UnreachableBrowserException
        Caused by: java.net.ConnectException
            Caused by: java.net.ConnectException
    java.lang.NullPointerException

我尝试更改不同的 url 以连接到 selenium 服务器,但我认为可能存在端口问题。但是我尝试过的每一种组合最终都得到了相同的结果。

.gitlab-ci.yml

image: gradle:alpine

variables:
  GRADLE_OPTS: "-Dorg.gradle.daemon=false"

before_script:
  - export GRADLE_USER_HOME=`pwd`/.gradle

stages:
  - build
  - seleniumTesting


build:
  stage: build
  script: 
    - echo $CI_JOB_STAGE
    - echo $CI_COMMIT_REF_NAME
    - gradle --build-cache war
  artifacts:
    paths:
      - public
  cache:
    key: "$CI_COMMIT_REF_NAME"
    policy: push
    paths:
      - build
      - .gradle


seleniumTestingChrome:
  stage: seleniumTesting
  script: gradle integrationTest
#  services:
#    - selenium/standalone-chrome:latest 
  services:
    - name: selenium/standalone-chrome:latest 
  artifacts:
    paths:
      - build/reports/tests/
  cache:
    key: "$CI_COMMIT_REF_NAME"
    policy: push
    paths:
      - build
      - .gradle

Java RemoteWebDriver 代码

DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setBrowserName(DesiredCapabilities.chrome().getBrowserName());

            try {
//              driver = new RemoteWebDriver( new URL("http://selenium_standalone-chrome:4444/wd/hub"), capabilities);
                WebDriver driver = new RemoteWebDriver( new URL("http://127.0.0.1:4444/wd/hub"), capabilities);

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

在 运行ner

上创建容器
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
92f018da4cbe        8017d8c2ba74        "sh -c 'if [ -x /usr…"   20 seconds ago      Up 19 seconds                           runner-Y2QWpCBd-project-4-concurrent-0-build-4
9dfdc838a7af        9e599fb82f84        "/opt/bin/entry_poin…"   40 seconds ago      Up 38 seconds       4444/tcp            runner-Y2QWpCBd-project-4-concurrent-0-selenium__standalone-chrome-0

docker 在 运行ner

上记录命令
019-08-30 17:06:02,099 INFO Included extra file "/etc/supervisor/conf.d/selenium.conf" during parsing
2019-08-30 17:06:02,101 INFO supervisord started with pid 7
2019-08-30 17:06:03,106 INFO spawned: 'xvfb' with pid 10
2019-08-30 17:06:03,109 INFO spawned: 'selenium-standalone' with pid 11
17:06:03.826 INFO [GridLauncherV3.parse] - Selenium server version: 3.141.59, revision: e82be7d358
2019-08-30 17:06:03,830 INFO success: xvfb entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
2019-08-30 17:06:03,830 INFO success: selenium-standalone entered RUNNING state, process has stayed up for > than 0 seconds (startsecs)
17:06:04.065 INFO [GridLauncherV3.lambda$buildLaunchers] - Launching a standalone Selenium Server on port 4444
2019-08-30 17:06:04.200:INFO::main: Logging initialized @1058ms to org.seleniumhq.jetty9.util.log.StdErrLog
17:06:04.804 INFO [WebDriverServlet.<init>] - Initialising WebDriverServlet
17:06:05.050 INFO [SeleniumServer.boot] - Selenium Server is up and running on port 4444

我确实希望测试在 docker 容器中的 gitlab-ci-运行ner 上 运行ning。连接到 selenium-server 并使用 public 可用 url

执行 selenium 测试

正如@Sascha Frinken 指出的那样

连接到 RemoteWebDriver 的 URL 错误。我漏了一个下划线。

"http://selenium_standalone-chrome:4444/wd/hub"

VS

"http://selenium__standalone-chrome:4444/wd/hub"

gitlab 中有一个主题 https://docs.gitlab.com/ee/ci/services/#accessing-the-services,它指出

Everything after the colon (:) is stripped.
Slash (/) is replaced with double underscores (__) and the primary alias is created.
Slash (/) is replaced with a single dash (-) and the secondary alias is created (requires GitLab Runner v1.1.0 or higher).