Skip to content

Instantly share code, notes, and snippets.

@galliani
Forked from bbonamin/Brewfile
Last active September 26, 2021 11:21
Show Gist options
  • Save galliani/a7f20241b648ab949949a7ef1178d154 to your computer and use it in GitHub Desktop.
Save galliani/a7f20241b648ab949949a7ef1178d154 to your computer and use it in GitHub Desktop.
Capybara Selenium Webdriver: Headless Chrome (with file downloads!) & Headless Firefox
brew tap "homebrew/cask"
brew install google-chrome
brew install firefox
brew install chromedriver
brew install geckodriver
require 'capybara/rspec'
require 'selenium/webdriver'
options = Selenium::WebDriver::Chrome::Options.new
options.add_preference(:download, prompt_for_download: false,
default_directory: '/tmp/downloads')
options.add_preference(:browser, set_download_behavior: { behavior: 'allow' })
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end
Capybara.register_driver :headless_chrome do |app|
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--window-size=1280,800')
driver = Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
### Allow file downloads in Google Chrome when headless!!!
### https://bugs.chromium.org/p/chromium/issues/detail?id=696481#c89
bridge = driver.browser.send(:bridge)
path = '/session/:session_id/chromium/send_command'
path[':session_id'] = bridge.session_id
bridge.http.call(:post, path, cmd: 'Page.setDownloadBehavior',
params: {
behavior: 'allow',
downloadPath: '/tmp/downloads'
})
###
driver
end
Capybara.javascript_driver = ENV['GUI'] ? :chrome : :headless_chrome
require 'capybara/rspec'
require 'selenium/webdriver'
Capybara.register_driver :firefox do |app|
Capybara::Selenium::Driver.new(app, browser: :firefox)
end
Capybara.register_driver :headless_firefox do |app|
options = Selenium::WebDriver::Firefox::Options.new
options.headless! # added on https://github.com/SeleniumHQ/selenium/pull/4762
Capybara::Selenium::Driver.new app,
browser: :firefox,
options: options
end
Capybara.javascript_driver = ENV['GUI'] ? :firefox : :headless_firefox
source 'https://rubygems.org'
group :development, :test do
gem 'factory_bot_rails' # Sample data, skip if you intend to use fixtures
gem 'rspec-rails', '~> 5.0.0'
end
group :test do
# For enabling database-cleaning on js:true system/feature spec
# because the automatic transaction done by rspec will not be capable of
# cleaning it on js: true. Setting the cleaning method of Rspec to :truncation will work
# but it will significantly increase the suite runtime.
# Read more: https://avdi.codes/configuring-database_cleaner-with-rails-rspec-capybara-and-selenium/
# and more in-depth technical nitty-gritty: https://stackoverflow.com/a/15976982
gem 'database_cleaner'
gem 'capybara'
# Drivers
gem 'selenium-webdriver'
# To keep the webdrivers above up-to-date by auto installing the updates
gem 'webdrivers', '~> 4.0', require: false
gem 'webmock'
end
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../config/environment', __dir__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
require 'capybara/rspec'
require 'selenium/webdriver'
require 'webmock/rspec'
# these lines below required to make chromedriver works
WebMock.disable_net_connect!(
allow: ['chromedriver.storage.googleapis.com', 'localhost', '127.0.0.1']
)
#
# You can put the Capybara driver settings here if you want
#
# Important line to uncomment if you want to include all support files by default
Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
begin
ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
puts e.to_s.strip
exit 1
end
RSpec.configure do |config|
### === SAMPLE data generation === ###
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
# config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.include FactoryBot::Syntax::Methods
### === END SAMPLE data generation === ###
### === DATABASE_CLEANER gem setting === ###
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = false # CHANGE from true to false
# You can move this utilities to support folder if you want, like `database_cleaner.rb`
# then add `require 'database_cleaner'`
# This says that before the entire test suite runs, clear the test database out completely.
# This gets rid of any garbage left over from interrupted or poorly-written tests
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
# This part sets the default database cleaning strategy to be transactions.
# Transactions are very fast, and for all the tests where they do work—that is,
# any test where the entire test runs in the RSpec process—they are preferable.
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
# By default, these are the only tests for which Capybara fires up a test server process and
# drives an actual browser window via the Selenium backend. For these types of tests,
# transactions won’t work, so this code overrides the setting and chooses the “truncation” strategy instead.
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
# These lines hook up database_cleaner around the beginning and end of each test,
# telling it to execute whatever cleanup strategy we selected beforehand.
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
### === END DATABASE_CLEANER gem setting === ###
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment