Running Capybara spec (Selenium) on Gitlab CI

49 Views Asked by At

I am trying to run Capybara spec for my Rails application on Gitlab CI but no success so far. I don't know if it's possible in alphine image or not.

Below is part of my .gitlab.yml file

RSpec Tests:
 stage: test
 image: ruby:3.2.2-alpine
 services:
   - name: selenium/standalone-chrome:latest
     alias: chrome
   - postgres:14.10
   - redis:7.0-alpine
 variables:
   POSTGRES_DB: postgres
   POSTGRES_USER: postgres
   POSTGRES_PASSWORD: postgres
   POSTGRES_DB_HOST: postgres
   DATABASE_NAME: my_test_db
   DATABASE_URL: postgres://postgres:postgres@postgres/my_test_db
   SELENIUM_REMOTE_URL: http://chrome:4444/wd/hub
   RAILS_ENV: test
 before_script:
   - cp .env.test .env
   - apk add --no-cache build-base yarn postgresql-dev git nodejs tzdata curl-dev
   - cp config/database.sample.yml config/database.yml
   - bundle install
   - bin/rails db:create
   - bin/rails db:migrate
   - bin/rails db:test:prepare
   - bin/rails assets:clobber
   - bin/rails assets:precompile
 script:
   - bin/rspec spec/features
 only:
   - branches
   - merge_requests

And below in my Capybara settings

    Capybara.register_driver :custom_chrome do |app|
  options = ::Selenium::WebDriver::Chrome::Options.new.tap do |opts|
    opts.args << '--headless'
    opts.args << '--disable-site-isolation-trials'
    opts.args << '--disable-gpu'
    opts.args << '--no-sandbox'
    opts.args << '--window-size=1280,800'
  end

  if ENV['SELENIUM_REMOTE_URL'].present?
    Capybara::Selenium::Driver.new(app, browser: :remote, url: ENV['SELENIUM_REMOTE_URL'], timeout: 120, options:)
  else
    Capybara::Selenium::Driver.new(app, browser: :chrome, timeout: 120, options:)
  end
end


RSpec.configure do |config|
  config.before(:each, type: :feature) do
    Capybara.current_driver = :custom_chrome
    Capybara.default_driver = :custom_chrome
  end
end

Below are the gems I am using

gem 'capybara'
gem 'selenium-webdriver'
gem 'webdrivers'
0

There are 0 best solutions below