【问题标题】:Gitlab CI Config for Rails System Tests with Selenium and Headless Chrome使用 Selenium 和 Headless Chrome 进行 Rails 系统测试的 Gitlab CI 配置
【发布时间】:2019-07-04 22:40:30
【问题描述】:

我正在尝试为一个非常简单的 Rails 项目设置持续的 Gitlab 集成,尽管我进行了所有搜索,但找不到任何可行的解决方案来使用无头 Chrome 进行系统测试。

这是我的.gitlab-ci.yml 文件:

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

这是我的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

但是,此配置会为每个系统测试产生以下错误:

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

我不相信这个配置文件中还有任何其他错误(与 Redis 或 MySQL 有关),因为只要我省略系统测试,一切都会正常运行。

顺便说一句,如果有人有更好的配置文件来实现相同的目标,我很想看看其他人是怎么做的。提前致谢。

【问题讨论】:

    标签: ruby-on-rails selenium-webdriver gitlab-ci google-chrome-headless system-testing


    【解决方案1】:

    how services are linked to the jobaccessing the services 中,它说如果你启动一个tutum/wordpress 容器(通过service 节);

    tutum/wordpress 将被启动,您将可以从您的构建容器中以两个主机名访问它以供选择:

    • tutum-wordpress
    • tutum__wordpress

    注意:带有下划线的主机名不是 RFC 有效的,可能会导致 3rd 方应用程序出现问题

    下面是我的处理方式:

    • 尝试使用http://selenium-standalone-chrome:4444/wd/hub,尽管这似乎是一个低概率的解决方案..
    • 在您的测试驱动程序中输出SELENIUM_URL。设置是否正确?
    • 查看how the health check of services works 中的日志。 standalone-chrome 来了吗?
    • 在某处添加pingnslookupselenium-standalone-chrome(或替代方案)是否正在解决?似乎确实如此,否则我们会得到一个“hostname unknown”而不是“connection denied”,但你永远不能太确定。

    【讨论】:

    • 我尝试了所有步骤,但到目前为止没有运气。更改 SELENIUM_URL 没有帮助。当我echo 时,SELENIUM_URL 确实设置正确。 standalone-chrome 即将到来。添加ping selenium-standalone-chrome 导致64 bytes from selenium__standalone-chrome (172.17.0.4): icmp_seq=1 ttl=64 time=0.121 ms,尽管有趣的是来自ping 的URL 与实际命令中的不同。我不知道接下来可以尝试什么。 :(
    • @vadim,看起来主机名设置正确。 selenium-standlone 可能是 selenium__standalone 的别名。 Ping 正在返回权威响应..所以看起来主机名设置正确。下一步是验证端口是否正确暴露。我在 GitLab 文档中找不到它是如何设置的。所以不是你的测试运行器,apt-get install nmap 和端口扫描selenium-standalone-chrome。似乎端口 4444 不可用(显然),但也许其他一些端口将是合适的。
    • 根据selenium-standalone-chrome 文档,我需要使用端口4444。我将尝试设置一个全新的模板 Rails 项目,看看我的任何设置是否导致此错误。
    猜你喜欢
    • 2023-01-03
    • 1970-01-01
    • 2018-05-12
    • 2018-11-24
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多