Search code examples
kotlinselenium-webdrivergitlab-ci

Gitlab CI Pipeline - Selenium service cant connect to host


I've reached WebDriver error unknown error: net::ERR_NAME_NOT_RESOLVED when I want to test my Web application running on job image.

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

Has somebody experience like this? Which host should I use within Selenium service to be able to access application running in Gitlab job?

Content of .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

Simple code snippet of calling running application:

// 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/")

Code creating webdriver:

// 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(),
)

Has somebody experience like this? Which host should I use within Selenium service to be able to access application running in Gitlab job?

Thank you


Solution

  • You need to set the FF_NETWORK_PER_BUILD flag for the job. Afterwards, the container running the job is accessible under the name “build” from other service containers. You also might want to consider setting an alias for the Selenium service container.

    Something along the lines of this should work:

    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
    

    References:

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