3

I'm trying to set up continuous Gitlab integration for a very simple Rails project and, despite all my searching, cannot find any workable solution for getting system tests to work using headless Chrome.

Here's my .gitlab-ci.yml file:

image: 'ruby:2.6.3'


before_script:
  - curl -sL https://deb.nodesource.com/setup_11.x | bash -
  - apt-get install -y nodejs
  - apt-get install -y npm
  - gem install bundler --conservative
  - bundle install
  - npm install -g yarn
  - yarn install

stages:
  - test

test:
  stage: test
  variables:
    MYSQL_HOST: 'mysql'
    MYSQL_DATABASE: 'cwrmb_test'
    MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
    SYSTEM_EMAIL: 'test@example.com'
    REDIS_URL: 'redis://redis:6379/'
    SELENIUM_URL: "http://selenium__standalone-chrome:4444/wd/hub"
  services:
    - redis:latest
    - selenium/standalone-chrome:latest
    - name: mysql:latest
      command: ['--default-authentication-plugin=mysql_native_password']
  script:
    - RAILS_ENV=test bin/rails db:setup
    - bin/rails test:system

Here's my application_system_test_case.rb:

require 'test_helper'

def selenium_options
  driver_options = {
    desired_capabilities: {
      chromeOptions: {
        args: %w[headless disable-gpu no-sandbox disable-dev-shm-usage]
      }
    }
  }
  driver_options[:url] = ENV['SELENIUM_URL'] if ENV['SELENIUM_URL']
  driver_options
end

class ApplicationSystemTestCase < ActionDispatch::SystemTestCase
  driven_by :selenium, using: :chrome, screen_size: [1400, 1400], options: selenium_options
end

However, this configuration yields the following error for every system test:

Selenium::WebDriver::Error::UnknownError: java.net.ConnectException: Connection refused (Connection refused)

I don't believe there are any other errors (to do with Redis or MySQL) in this configuration file, because as soon as I omit system tests, everything works perfectly.

By the way, if anyone has any better configuration files for achieving the same goal, I would love to see what others do. Thanks in advance.

Vadim
  • 1,916
  • 2
  • 19
  • 39

2 Answers2

0

In how services are linked to the job and accessing the services it says that if you start a tutum/wordpress container (via a service stanza);

tutum/wordpress will be started and you will have access to it from your build container under two hostnames to choose from:

  • tutum-wordpress
  • tutum__wordpress

Note: Hostnames with underscores are not RFC valid and may cause problems in 3rd party applications

So here's how I'd proceed:

  • try with http://selenium-standalone-chrome:4444/wd/hub although this seems like a low probability solution..
  • output SELENIUM_URL in your test driver. Is it getting set correctly?
  • review the logs as in how the health check of services works. Is standalone-chrome coming up?
  • add ping or nslookup in there somewhere. Is selenium-standalone-chrome (or the alternative) resolving? It seems like it does otherwise we'd get a "hostname unknown" rather than the "connection refused", but you can never be too sure.
Community
  • 1
  • 1
EightyEight
  • 3,430
  • 4
  • 36
  • 67
  • I tried all of the steps, but no luck so far. Changing the SELENIUM_URL did not help. `SELENIUM_URL` is indeed set correctly when I `echo` it. `standalone-chrome` is coming up. Adding `ping selenium-standalone-chrome` resulted in `64 bytes from selenium__standalone-chrome (172.17.0.4): icmp_seq=1 ttl=64 time=0.121 ms`, although interestingly the URL from ping is different than in the actual command. I'm at a loss as to what I can try next. :( – Vadim Jul 09 '19 at 08:30
  • @vadim, looks like the hostname is set correctly. `selenium-standlone` is likely an alias for `selenium__standalone`. Ping is returning the authoritative response..so it looks like the hostname is set correctly. The next step is to verify that that ports are exposed correctly. I couldn't find how it's set in the GitLab docs. So instead of your test runner, `apt-get install nmap` and port scan the `selenium-standalone-chrome`. It seems like port 4444 is not available (obviously), but maybe some other port is going to be appropriate. – EightyEight Jul 10 '19 at 14:31
  • According to `selenium-standalone-chrome` documentation, port 4444 is the one I need to use. I will try to set up a brand new template Rails project to see if any of my setup is causing this error. – Vadim Jul 15 '19 at 18:17
0

I have a similar problem and my solution was:

#/test/application_system_test_case.rb
driven_by :selenium, using: :headless_chrome do |option|
    option.add_argument(“no-sandbox”)
end
inye
  • 1,786
  • 1
  • 23
  • 31