当我想要测试在作业镜像上运行的 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
引用文献:
关于kotlin - Gitlab CI Pipeline - Selenium 服务无法连接到主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72170537/