使用 rspec / selenium / Firefox 配置 GitLab CI 到 运行 功能测试

Configure GitLab CI to run features tests with rspec / selenium / Firefox

我是 gitlab 的新手 CI,我需要帮助来配置 gitlab-ci 以使用 Firefox 进行 运行 功能测试(Ruby 2.7 / Rails 6 / Rspec / 水豚).

除功能测试外一切正常。 我想我必须用 Firefox 配置一些东西,或者安装一些东西。

感谢您的帮助!

运行ning 测试时的错误消息:

Failure/Error: visit "/meth/methodologies/#{@meth.id}/edit"
     Selenium::WebDriver::Error::WebDriverError:
       Could not find Firefox binary (os=linux). Make sure Firefox is installed or set the path manually with Selenium::WebDriver::Firefox::Binary.path=

文件.gitlab-ci.yml

stages:
  - build
  - test
  - deploy

image: ruby:2.7.1

cache: &global_cache
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - apt-cache/
    - vendor/ruby
    - node_modules
    - .yarn-cache
  policy: pull-push

.base:
  cache:
    # inherit all global cache settings
    <<: *global_cache
  before_script:
    - gem install bundler --no-document
    - bundle install --jobs $(nproc) "${FLAGS[@]}" --path=vendor

.base_db:
  # extends: .base
  services:
    - name: mysql:8.0.21
      command: ['--default-authentication-plugin=mysql_native_password']
    - name: selenium/standalone-firefox
      alias: selenium
  variables:
    MYSQL_ROOT_PASSWORD: xxxx
    DB_USERNAME: xxxx
    DB_PASSWORD: xxxx
    DB_HOST: mysql
    RAILS_ENV: test
    DISABLE_SPRING: 1
    BUNDLE_PATH: vendor/bundle
  cache:
    # inherit all global cache settings
    <<: *global_cache
  before_script:
    # install yarn & dependencies
    - export APT_CACHE_DIR=`pwd`/apt-cache && mkdir -pv $APT_CACHE_DIR
    - wget -q -O - https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
    - echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list
    - apt-get update -qq && apt-get -o dir::cache::archives="$APT_CACHE_DIR" install -y yarn
    - yarn config set cache-folder .yarn-cache
    - yarn install

    - gem install bundler --no-document
    - bundle install --jobs $(nproc) "${FLAGS[@]}" --path=vendor/ruby

    # Setup test database
    - cp config/database.ci.yml config/database.yml
    - RAILS_ENV=test bundle exec rails db:create db:migrate

rubocop:
  extends: .base
  stage: build
  # cache:
  #   policy: pull-push
  script:
    - bundle exec rubocop app --fail-level W

rspec:
  extends: .base_db
  stage: test
  script:
    - bundle exec rspec -t ~type:feature
  artifacts:
    paths:
      - coverage/

features:
  extends: .base_db
  stage: test
  script:
    - bundle exec rspec
  # services:
  #   - name: selenium/standalone-firefox
  #     alias: selenium
  # artifacts:
  #   paths:
  #     - coverage/

pages:
  stage: deploy
  dependencies:
    - rspec
  script:
    - mv coverage/ public/
  artifacts:
    paths:
      - public
    expire_in: 30 days
  # only:
  #   - master

编辑:

我添加了firefox的安装。 使用js的测试还是不行。现在的错误如下:

 Failure/Error: visit "/meth/methodologies/#{@meth.id}/edit"
     Selenium::WebDriver::Error::UnknownError:
       Process unexpectedly closed with status 1

您必须使用带无头选项的浏览器

rails_helper.rb

Capybara.register_driver :headless_firefox do |app|
  browser_options = Selenium::WebDriver::Firefox::Options.new()
  browser_options.args << '--headless'
  Capybara::Selenium::Driver.new(
    app,
    browser: :firefox,
    options: browser_options
  )
end

Capybara.javascript_driver = :headless_firefox

Capybara.configure do |config|
  config.default_max_wait_time = 10 # seconds
  config.default_driver = :headless_firefox
end