Running Capybara spec using remote url in different container in Gitlab

37 Views Asked by At

I am able to install google-chrome in gitlab container and spec works fine but I want to use selenium/standalone-chrome image in different container and run my spec like that.

Below is part of my Gitlab yml file

stages:
  - validate
  - test

image: ruby:3.2.2-slim

selenium:
  stage: test
  services:
    - name: selenium/standalone-chrome:latest
      alias: selenium
    - postgres:latest
  variables:
    POSTGRES_USER: postgres
    POSTGRES_PASSWORD: password
    DB_HOST: postgres
    RAILS_ENV: test
    SELENIUM_REMOTE_URL: "http://selenium:4444/wd/hub"
  before_script:
    - apt-get update && apt-get install build-essential postgresql-client libpq-dev -y
    - bundle install
    - bin/rails db:create
    - bin/rails db:migrate
    - bin/rails db:test:prepare
    - apt-get update && apt-get install -y curl
    - curl http://selenium:4444/wd/hub/status # Check if Selenium is up
    - bin/rails assets:clobber
    - bin/rails assets:precompile
  script:
    - bundle exec rspec spec/features

Below is my Capybara config

Capybara.register_driver :selenium_chrome_headless do |app|
    options = Selenium::WebDriver::Chrome::Options.new
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    options.add_argument('--disable-gpu')
    options.add_argument('--window-size=1920,1080')

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

  Capybara.javascript_driver = :selenium_chrome_headless
  Capybara.default_driver = :selenium_chrome_headless

Below are my Gems

group :test do
  gem 'capybara'
  gem 'rspec-rails', '~> 6.1.0'
  gem 'selenium-webdriver'
end

But I keep on getting below error

Failure/Error: before { visit users_path }
     Net::ReadTimeout:
       Net::ReadTimeout with #<TCPSocket:(closed)>

Any idea how to fix this ? I am running out of ideas. I tried few things like setting up Capybara.server_host = '0.0.0.0' increase timeout limit etc but nothing is working. I think I am missing some config in gitlab yml or Capybara config.

** UPDATE **

Add test code as requested. Spec run fine locally and if I install google-chrome in same container. This is just dummy spec I wrote so I can test it.

# frozen_string_literal: true

require 'rails_helper'

describe 'Visit Users Page', :js do
  before { visit users_path }

  it 'will have 0 user created' do
    expect(page).to have_content('User Count: 0')
  end

  describe 'Create New User' do
    before { visit users_path }

    context 'when user is valid' do
      before do
        click_link 'New User'
        fill_in 'First name', with: 'Test'
        fill_in 'Last name', with: 'Test'
        click_button 'Create User'
      end

      it 'will display user count on screen' do
        expect(page).to have_content('User Count: 1')
      end

      it 'will display success flash message' do
        expect(page).to have_content('User Created')
      end
    end

    context 'when user is not valid' do
      before do
        click_link 'New User'
        click_button 'Create User'
      end

      it 'will not create new user' do
        expect(page).to have_content('User Creation failed')
      end
    end
  end
end
0

There are 0 best solutions below