kotlin - Gitlab CI Pipeline - Selenium 服务无法连接到主机

标签 kotlin selenium-webdriver gitlab-ci

当我想要测试在作业镜像上运行的 Web 应用程序时,我遇到了 WebDriver 错误未知错误:net::ERR_NAME_NOT_RESOLVED

org.openqa.selenium.WebDriverException: unknown error: net::ERR_NAME_NOT_RESOLVED
  (Session info: chrome=101.0.4951.41)
Build info: version: '4.1.4', revision: '535d840ee2'
System info: host: 'runner-j1aldqxs-project-35693309-concurrent-0', ip: '172.17.0.4', os.name: 'Linux', os.arch: 'amd64', os.version: '5.4.109+', java.version: '11.0.15'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Command: [0a63e715d2499336585640d785b516c6, get {url=http://runner-j1aldqxs-project-35693309-concurrent-0:8080/}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 101.0.4951.41, chrome: {chromedriverVersion: 101.0.4951.41 (93c720db8323..., userDataDir: /tmp/.com.google.Chrome.0mp2co}, goog:chromeOptions: {debuggerAddress: localhost:32985}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: LINUX, platformName: LINUX, proxy: Proxy(), se:cdp: ws://172.17.0.3:4444/sessio..., se:cdpVersion: 101.0.4951.41, se:vnc: ws://172.17.0.3:4444/sessio..., se:vncEnabled: true, se:vncLocalAddress: ws://172.17.0.3:7900, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
Session ID: 0a63e715d2499336585640d785b516c6

有人有这样的经历吗?我应该在 Selenium 服务中使用哪个主机才能访问在 Gitlab 作业中运行的应用程序?

.gitlab-cy.yml的内容:

image: openjdk:11-jdk

stages:
  - debug

debug:
  stage: debug
  services:
    - name: selenium/standalone-chrome:latest
  before_script:
    - chmod +x ops/waitForHost.sh
  script:
    # hostname for container of current job, it is something like "runner-j1aldqxs-project-35693309-concurrent-0"
    - export APP_TESTS_HOSTNAME="$(hostname)"
    # where selenium service is reachable
    - export APP_TESTS_REMOTE_WEB_DRIVER_URL="http://selenium__standalone-chrome:4444"
    # starts web application on image openjdk:11-jdk
    - ./gradlew run --parallel > /dev/null 2>&1 &
    # waiting until application is on and running
    - bash ops/waitForHost.sh localhost:8080 300
    # running tests using webdriver
    - ./gradlew jvmTest --info

调用正在运行的应用程序的简单代码片段:

// host, where application is placed
val hostname = System.getenv("APP_TESTS_HOSTNAME")

// this should open homepage in Selenium browser, but error appears
// generated URL is: http://runner-j1aldqxs-project-35693309-concurrent-0:8080/
webDriver.get("http://${hostname}:8080/")

创建网络驱动程序的代码:

// this works, it connects to Selenium service on http://selenium__standalone-chrome:4444
webDriver = RemoteWebDriver(
    URL(System.getenv("APP_TESTS_REMOTE_WEB_DRIVER_URL")),
    ChromeOptions(),
)

有人有这样的经历吗?我应该在 Selenium 服务中使用哪个主机才能访问在 Gitlab 作业中运行的应用程序?

谢谢

最佳答案

您需要为作业设置FF_NETWORK_PER_BUILD标志。之后,可以从其他服务容器以“build”名称访问运行作业的容器。您可能还需要考虑为 Selenium 服务容器设置别名。

类似的事情应该有效:

debug:
  stage: debug
  services:
    - name: selenium/standalone-chrome:latest
      alias: chrome
  variables:
    FF_NETWORK_PER_BUILD: "true"
    APP_TESTS_HOSTNAME: "build"
    APP_TESTS_REMOTE_WEB_DRIVER_URL: "http://chrome:4444"
  before_script:
    - chmod +x ops/waitForHost.sh
  script:
    # starts web application on image openjdk:11-jdk
    - ./gradlew run --parallel > /dev/null 2>&1 &
    # waiting until application is on and running
    - bash ops/waitForHost.sh localhost:8080 300
    # running tests using webdriver
    - ./gradlew jvmTest --info

引用文献:

  1. https://docs.gitlab.com/runner/executors/docker.html#create-a-network-for-each-job

关于kotlin - Gitlab CI Pipeline - Selenium 服务无法连接到主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72170537/

相关文章:

generics - 如何使用泛型处理函数的重载解析歧义?

android - 我无法在 kotlin webview 中上传图片、视频和文件

gitlab - 如何从 GitLab CI 创建 Jira 任务?

android - 如何强制应用进入全屏模式(切入友好状态)

java - 所有网络请求完成或其中一个请求失败后如何执行操作?

python - 使用 selenium/python 进行复杂的 XPATH 导航

java - 如何通过 Jenkins 定位特定的 Cucumber 功能文件(自动化测试)?

c# - IELaunchURL() 返回 HRESULT 80070012 ('There are no more files.' )

git - 通过gitlab api或者gitlab-cli获取gitlab项目的 `id`

Docker GitLab 管道返回 "-t, --tag"标志 : invalid reference format 的无效参数