From 9eae73c7e42d0a05822189e51726a4532c15d9d9 Mon Sep 17 00:00:00 2001 From: James Mason Date: Thu, 25 Oct 2018 10:50:13 -0700 Subject: [PATCH] Goodbye phantomjs, hello (again) selenium. --- Gemfile | 5 +++-- Gemfile.lock | 25 ++++++++++++++++--------- dotenv.example | 9 +++++++++ spec/spec_helper.rb | 39 ++++++++++++++++++++++++++++----------- 4 files changed, 56 insertions(+), 22 deletions(-) diff --git a/Gemfile b/Gemfile index 72d300c4..517ec5cb 100644 --- a/Gemfile +++ b/Gemfile @@ -252,10 +252,11 @@ end group :test do # as test framework gem 'capybara' + gem 'chromedriver-helper' gem 'database_cleaner' - gem 'phantomjs', :require => 'phantomjs/poltergeist' - gem 'poltergeist' + gem 'geckodriver-helper' gem 'rspec-rails', '~> 3.5', '>= 3.5.2' + gem 'selenium-webdriver' # for measuring test coverage gem 'codecov', require: false # for describing models diff --git a/Gemfile.lock b/Gemfile.lock index edef7650..8eb630d0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -64,6 +64,8 @@ GEM airbrake-ruby (2.12.0) ajax-datatables-rails (0.4.3) railties (>= 4.0) + archive-zip (0.11.0) + io-like (~> 0.3.0) arel (7.1.4) ast (2.4.0) autoprefixer-rails (9.3.1) @@ -104,10 +106,14 @@ GEM chart-js-rails (0.1.6) railties (> 3.1) chartkick (3.0.1) + childprocess (0.9.0) + ffi (~> 1.0, >= 1.0.11) + chromedriver-helper (2.1.0) + archive-zip (~> 0.10) + nokogiri (~> 1.8) chronic (0.10.2) chunky_png (1.3.8) climate_control (0.2.0) - cliver (0.3.2) cloudinary (1.9.1) aws_cf_signer rest-client @@ -179,6 +185,8 @@ GEM actionpack (>= 3.2.13) formtastic-bootstrap (3.1.1) formtastic (>= 3.0) + geckodriver-helper (0.21.0) + archive-zip (~> 0.7) globalid (0.4.1) activesupport (>= 4.2.0) gravtastic (3.2.6) @@ -225,6 +233,7 @@ GEM i18n_data (0.8.0) inversion (1.1.1) loggability (~> 0.12) + io-like (0.3.0) iso-639 (0.2.8) jaro_winkler (1.5.1) jquery-datatables-rails (3.4.0) @@ -334,16 +343,10 @@ GEM ast (~> 2.4.0) pdf-core (0.2.5) pg (1.1.2) - phantomjs (2.1.1.0) piwik_analytics (1.0.2) actionpack activesupport rails (>= 3.0.0) - poltergeist (1.9.0) - capybara (~> 2.1) - cliver (~> 0.3.1) - multi_json (~> 1.0) - websocket-driver (>= 0.2.0) powerpack (0.1.2) prawn (1.0.0) pdf-core (~> 0.2.2) @@ -502,6 +505,9 @@ GEM sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) selectize-rails (0.12.6) + selenium-webdriver (3.14.1) + childprocess (~> 0.5) + rubyzip (~> 1.2, >= 1.2.2) sexp_processor (4.11.0) shellany (0.0.1) shoulda-matchers (2.8.0) @@ -602,6 +608,7 @@ DEPENDENCIES carrierwave-bombshelter chart-js-rails chartkick + chromedriver-helper climate_control cloudinary cocoon @@ -621,6 +628,7 @@ DEPENDENCIES font-awesome-rails formtastic (~> 3.1.5) formtastic-bootstrap + geckodriver-helper gravtastic guard-rspec haml-rails @@ -645,9 +653,7 @@ DEPENDENCIES omniauth-openid paper_trail pg - phantomjs piwik_analytics (~> 1.0.1) - poltergeist prawn-qrcode (~> 0.2.2.1) prawn_rails puma (~> 3.0) @@ -680,6 +686,7 @@ DEPENDENCIES ruby-oembed (~> 0.12.0) sass-rails (>= 4.0.2) selectize-rails + selenium-webdriver shoulda-matchers skylight spring-commands-rspec diff --git a/dotenv.example b/dotenv.example index 225116fa..e2b5c93b 100644 --- a/dotenv.example +++ b/dotenv.example @@ -115,3 +115,12 @@ # Speeds up turn around times of tests # OSEM_FACTORY_LINT=false +# How should browser tests be performed? +# For headless Chrome (default): +# OSEM_TEST_DRIVER=chrome_headless +# For Chrome: +# OSEM_TEST_DRIVER=chrome +# For headless Firefox: +# OSEM_TEST_DRIVER=firefox_headless +# For Firefox: +# OSEM_TEST_DRIVER=firefox diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4d926c70..82cdb53d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -18,9 +18,7 @@ require 'shoulda/matchers' # all migrations applied ActiveRecord::Migration.maintain_test_schema! -# Add poltergeist to use it as JS driver -require 'capybara/poltergeist' -require 'phantomjs' +require 'selenium/webdriver' # Adds rspec helper provided by paper_trail # makes it easier to control when PaperTrail is enabled during testing. @@ -59,18 +57,37 @@ RSpec.configure do |config| # --seed 1234 config.order = 'random' - # poltergeist as a underlying mech for Capybara - Capybara.javascript_driver = :poltergeist + Capybara.register_driver :firefox do |app| + Capybara::Selenium::Driver.new(app, browser: :firefox) + end - Capybara.register_driver :poltergeist do |app| - Capybara::Poltergeist::Driver.new( - app, - phantomjs: Phantomjs.path, - js_errors: false, - window_size: [1920, 1080] + Capybara.register_driver :chrome do |app| + Capybara::Selenium::Driver.new(app, browser: :chrome) + end + + Capybara.register_driver :firefox_headless do |app| + options = Selenium::WebDriver::Firefox::Options.new + options.args << '--headless' + options.args << '--window-size=1920,1080' + Capybara::Selenium::Driver.new(app, browser: :firefox, options: options) + end + + Capybara.register_driver :chrome_headless do |app| + capabilities = Selenium::WebDriver::Remote::Capabilities.chrome( + chromeOptions: { args: %w(headless disable-gpu) } + ) + Capybara::Selenium::Driver.new( + app, browser: :chrome, desired_capabilities: capabilities ) end + Capybara.default_max_wait_time = 10 # seconds + + # use a real browser for JS tests + Capybara.javascript_driver = ( + ENV['OSEM_TEST_DRIVER'].try(:to_sym) || :chrome_headless + ) + # Includes helpers and connect them to specific types of tests config.include FactoryBot::Syntax::Methods config.include OmniauthMacros