Skip to content

Instantly share code, notes, and snippets.

@terrywbrady
Created May 15, 2023 18:06
Show Gist options
  • Save terrywbrady/24f664b55a7883e2a92ae089af782e0a to your computer and use it in GitHub Desktop.
Save terrywbrady/24f664b55a7883e2a92ae089af782e0a to your computer and use it in GitHub Desktop.
Sample Capybara Driver Running Headless Chrome in a Container

Sample Capybara Driver Running Headless Chrome in a Container

To run this script

docker-compose build
docker-compose up -d chrome
docker-compose run --rm my-integ-tests
docker-compose down
version: '3.9'
services:
my-integ-tests:
container_name: my-integ-tests
image: test/my-integ-tests
build:
context: .
dockerfile: Dockerfile
environment:
CHROME_URL: http://chrome:4444/wd/hub
stdin_open: true
tty: true
depends_on:
- chrome
chrome:
container_name: chrome
image: selenium/standalone-chrome
environment:
JAVA_OPTS: -Dwebdriver.chrome.whitelistedIps= -Dwebdriver.chrome.allowedIps=
shm_size: '2gb'
#FROM ruby:2.7
FROM ruby:3
RUN apt-get update -y && \
apt-get -y upgrade && \
apt-get install -y libarchive-tools zip
RUN gem install bundler
COPY Gemfile Gemfile
RUN bundle install
COPY . .
CMD ["bundle", "exec", "rspec", "/spec/test"]
source "https://rubygems.org"
gem 'rspec'
gem 'capybara'
gem 'capybara-webmock'
gem 'colorize'
gem 'webdrivers'
#gem 'chromedriver-helper'
gem 'selenium-webdriver'
gem 'byebug'
gem 'nokogiri'
# save this as spec/spec_helper.rb
require 'colorize'
require 'capybara/dsl'
require 'capybara/rspec'
require 'nokogiri'
RSpec.configure do |config|
config.color = true
config.tty = true
config.formatter = :documentation
config.include Capybara::DSL
end
def create_web_session
Capybara.app_host = "https://www.google.com"
Capybara.run_server = false # don't start Rack
if ENV['CHROME_URL']
Capybara.register_driver :remote do |app|
Capybara::Selenium::Driver.new(app, browser: :remote, options: Selenium::WebDriver::Options.chrome, url: ENV['CHROME_URL'])
end
@session = Capybara::Session.new(:remote)
else
@session = Capybara::Session.new(:selenium_chrome)
end
end
def end_web_session(session)
session.reset!
session.driver.quit
end
# Save this as spec/test/demo_spec.rb
require 'spec_helper.rb'
require 'webdrivers/chromedriver'
require 'cgi'
sleep 1
RSpec.describe 'simple tests', type: :feature do
before(:each) do
@session = create_web_session
end
after(:each) do
end_web_session(@session)
end
it 'Load home page' do
@session.visit '/'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment