From d8c5db497f5d897471af0f6e2de2d25bf1f5ad35 Mon Sep 17 00:00:00 2001 From: Vivian Brown Date: Fri, 6 Apr 2018 16:40:28 -0400 Subject: [PATCH 1/9] Revert "Remove ES6 syntax not supported by capybara-webkit" This reverts commit 21948995f91e844624b569ac1ed6c73949747b36. --- app/assets/javascripts/application/glossary.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/application/glossary.js b/app/assets/javascripts/application/glossary.js index ca658c90..02e9efad 100644 --- a/app/assets/javascripts/application/glossary.js +++ b/app/assets/javascripts/application/glossary.js @@ -1,18 +1,18 @@ -$(document).on('mouseover', '.glossary-term, .glossary-definition', function(e) { +$(document).on('mouseover', '.glossary-term, .glossary-definition', (e) => { var def = $(e.target).closest('.glossary-term').find('.glossary-definition'); clearTimeout(def.data('timeout')); def.data('timeout', null).show(); }); -$(document).on('mouseout', '.glossary-term, .glossary-definition', function(e) { +$(document).on('mouseout', '.glossary-term, .glossary-definition', (e) => { var def = $(e.target).closest('.glossary-term').find('.glossary-definition'); if (!def.data('timeout')) { - def.data('timeout', setTimeout(function() { + def.data('timeout', setTimeout(() => { def.data('timeout', null).hide(); }, 200)); } }); -$(document).on('click', '.glossary-term', function(e) { +$(document).on('click', '.glossary-term', (e) => { window.open($(e.target).find('a').attr('href')); }); From 94366077b57e5758565211ccbae889dadee552d3 Mon Sep 17 00:00:00 2001 From: Vivian Brown Date: Fri, 6 Apr 2018 16:58:42 -0400 Subject: [PATCH 2/9] Switch from webkit to headless chrome webdriver Capybara-webkit does not currently support ES6 syntax. Headless chrome appears to require full file paths. --- Gemfile | 2 +- Gemfile.lock | 12 +++++++----- spec/rails_helper.rb | 5 +++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Gemfile b/Gemfile index 624dc726..fe6a2d46 100644 --- a/Gemfile +++ b/Gemfile @@ -95,11 +95,11 @@ end group :test do gem "capybara", "~> 2.13" - gem "capybara-webkit" gem "factory_girl_rails" gem "rack_session_access" gem "rspec-core" gem "rspec-rails" + gem "selenium-webdriver" end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem diff --git a/Gemfile.lock b/Gemfile.lock index 4532d5e2..0fc3b1c4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -85,13 +85,12 @@ GEM rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) - capybara-webkit (1.15.0) - capybara (>= 2.3, < 4.0) - json carrierwave (1.2.1) activemodel (>= 4.0.0) activesupport (>= 4.0.0) mime-types (>= 1.16) + childprocess (0.9.0) + ffi (~> 1.0, >= 1.0.11) ckeditor (4.2.4) cocaine orm_adapter (~> 0.5.0) @@ -168,7 +167,6 @@ GEM rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) - json (2.1.0) kaminari (1.1.1) activesupport (>= 4.1.0) kaminari-actionview (= 1.1.1) @@ -313,6 +311,7 @@ GEM rubocop (~> 0.49) ruby-progressbar (1.9.0) ruby_dep (1.5.0) + rubyzip (1.2.1) sass (3.4.25) sass-rails (5.0.6) railties (>= 4.0.0, < 6) @@ -322,6 +321,9 @@ GEM tilt (>= 1.1, < 3) select2-rails (4.0.3) thor (~> 0.14) + selenium-webdriver (3.11.0) + childprocess (~> 0.5) + rubyzip (~> 1.2) sentry-raven (2.7.1) faraday (>= 0.7.6, < 1.0) spring (2.0.2) @@ -376,7 +378,6 @@ DEPENDENCIES acts-as-taggable-on (~> 5.0) byebug capybara (~> 2.13) - capybara-webkit carrierwave ckeditor delayed_job_active_record @@ -408,6 +409,7 @@ DEPENDENCIES rubocop-github sass-rails (~> 5.0) select2-rails + selenium-webdriver sentry-raven spring spring-watcher-listen (~> 2.0.0) diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index a5fb9ef6..31953cd8 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -30,7 +30,7 @@ ActiveRecord::Migration.maintain_test_schema! require "capybara/rspec" -Capybara.javascript_driver = :webkit +Capybara.javascript_driver = :selenium_chrome_headless RSpec.configure do |config| # Load authentication helpers @@ -65,7 +65,8 @@ config.filter_rails_from_backtrace! # arbitrary gems may also be filtered via: # config.filter_gems_from_backtrace("gem name") -end + config.file_fixture_path = Rails.root.join("spec", "fixtures", "files") +end Lesson.skip_callback(:save, :after, :enqueue_pdf_update) From 121bed7db2b99ae9fac9f908d64e049a4b2b200f Mon Sep 17 00:00:00 2001 From: Vivian Brown Date: Fri, 6 Apr 2018 17:04:15 -0400 Subject: [PATCH 3/9] Add chrome and chromedriver to travis --- .travis.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.travis.yml b/.travis.yml index f86e7184..677c99e5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,10 +8,17 @@ rvm: services: - postgresql addons: + chrome: stable apt: packages: - wkhtmltopdf before_install: + - wget -N http://chromedriver.storage.googleapis.com/2.37/chromedriver_linux64.zip -P ~/ + - unzip ~/chromedriver_linux64.zip -d ~/ + - rm ~/chromedriver_linux64.zip + - sudo mv -f ~/chromedriver /usr/local/share/ + - sudo chmod +x /usr/local/share/chromedriver + - sudo ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver - gem install bundler install: - bundle install --path vendor/bundle From 122375bcd04fa071fc89a1fc5357a3b068415ed1 Mon Sep 17 00:00:00 2001 From: Vivian Brown Date: Mon, 9 Apr 2018 10:56:30 -0400 Subject: [PATCH 4/9] Add chromedriver to Dockerfile --- Dockerfile | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 7bd1e334..c1924b7c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,12 +25,14 @@ RUN echo "@edge http://nl.alpinelinux.org/alpine/edge/main" >>/etc/apk/repositor wkhtmltopdf@edgetesting \ nodejs \ yarn \ + chromium \ + chromium-chromedriver \ # Needed for wkhtmltopdf dbus \ - # Needed for capybara-webkit - qt-dev@edge \ + # Set up chromedriver. + && ln -s /usr/lib/chromium-browser/chromedriver /usr/local/bin/chromedriver \ # Set up crontab. && echo "*/15 * * * * su -s/bin/sh www-data -c \ @@ -40,6 +42,8 @@ RUN echo "@edge http://nl.alpinelinux.org/alpine/edge/main" >>/etc/apk/repositor 'cd /opt/trainers-hub && bundle exec rake glossary:update' >>/proc/1/fd/1 2>&1" >>/etc/crontab ENV DISPLAY=:99 +ENV CHROME_BIN=/usr/bin/chromium-browser +ENV CHROME_PATH=/usr/lib/chromium/ COPY Gemfile* ./ RUN bundle install From 55b893b99617d127e1dbfd57b70f95bf1d9ca35b Mon Sep 17 00:00:00 2001 From: Vivian Brown Date: Mon, 9 Apr 2018 11:39:27 -0400 Subject: [PATCH 5/9] Add install instructions for chromedriver --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index da99ecde..15c18176 100644 --- a/README.md +++ b/README.md @@ -16,3 +16,6 @@ After running `rake db:setup` you can navigate to `http://localhost:3000/admin` * Install Ruby, Rails, and other gems the usual way. * [install Yarn](https://yarnpkg.com/lang/en/docs/install/) and run `yarn install` to install Javascript dependencies. +* Install chromedriver to run browser tests with headless Chrome/Chromium. + * `sudo apt install chromium chromium-chromedriver` + * `sudo ln -s /usr/lib/chromium-browser/chromedriver /usr/local/bin/chromedriver` From 4c6a27f1cf7b39ca19ca3a045e759f48a3b28468 Mon Sep 17 00:00:00 2001 From: Vivian Brown Date: Mon, 9 Apr 2018 14:52:07 -0400 Subject: [PATCH 6/9] Install chromedriver using gem helper --- .travis.yml | 6 ------ Dockerfile | 4 ---- Gemfile | 1 + Gemfile.lock | 7 +++++++ README.md | 4 +--- 5 files changed, 9 insertions(+), 13 deletions(-) diff --git a/.travis.yml b/.travis.yml index 677c99e5..f4a3a10d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,12 +13,6 @@ addons: packages: - wkhtmltopdf before_install: - - wget -N http://chromedriver.storage.googleapis.com/2.37/chromedriver_linux64.zip -P ~/ - - unzip ~/chromedriver_linux64.zip -d ~/ - - rm ~/chromedriver_linux64.zip - - sudo mv -f ~/chromedriver /usr/local/share/ - - sudo chmod +x /usr/local/share/chromedriver - - sudo ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver - gem install bundler install: - bundle install --path vendor/bundle diff --git a/Dockerfile b/Dockerfile index c1924b7c..41a8a3ce 100644 --- a/Dockerfile +++ b/Dockerfile @@ -26,14 +26,10 @@ RUN echo "@edge http://nl.alpinelinux.org/alpine/edge/main" >>/etc/apk/repositor nodejs \ yarn \ chromium \ - chromium-chromedriver \ # Needed for wkhtmltopdf dbus \ - # Set up chromedriver. - && ln -s /usr/lib/chromium-browser/chromedriver /usr/local/bin/chromedriver \ - # Set up crontab. && echo "*/15 * * * * su -s/bin/sh www-data -c \ 'cd /opt/trainers-hub && bundle exec rake blog:update' >>/proc/1/fd/1 2>&1" >>/etc/crontabs/root \ diff --git a/Gemfile b/Gemfile index fe6a2d46..c05f06c1 100644 --- a/Gemfile +++ b/Gemfile @@ -100,6 +100,7 @@ group :test do gem "rspec-core" gem "rspec-rails" gem "selenium-webdriver" + gem "chromedriver-helper" end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem diff --git a/Gemfile.lock b/Gemfile.lock index 0fc3b1c4..4b6f5395 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -68,6 +68,8 @@ GEM public_suffix (>= 2.0.2, < 4.0) arbre (1.1.1) activesupport (>= 3.0.0) + archive-zip (0.11.0) + io-like (~> 0.3.0) arel (8.0.0) ast (2.3.0) babel-source (5.8.35) @@ -91,6 +93,9 @@ GEM mime-types (>= 1.16) childprocess (0.9.0) ffi (~> 1.0, >= 1.0.11) + chromedriver-helper (1.2.0) + archive-zip (~> 0.10) + nokogiri (~> 1.8) ckeditor (4.2.4) cocaine orm_adapter (~> 0.5.0) @@ -160,6 +165,7 @@ GEM responders invisible_captcha (0.10.0) rails (>= 3.2.0) + io-like (0.3.0) jbuilder (2.7.0) activesupport (>= 4.2.0) multi_json (>= 1.2) @@ -379,6 +385,7 @@ DEPENDENCIES byebug capybara (~> 2.13) carrierwave + chromedriver-helper ckeditor delayed_job_active_record devise diff --git a/README.md b/README.md index 15c18176..7e7aa56d 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,4 @@ After running `rake db:setup` you can navigate to `http://localhost:3000/admin` * Install Ruby, Rails, and other gems the usual way. * [install Yarn](https://yarnpkg.com/lang/en/docs/install/) and run `yarn install` to install Javascript dependencies. -* Install chromedriver to run browser tests with headless Chrome/Chromium. - * `sudo apt install chromium chromium-chromedriver` - * `sudo ln -s /usr/lib/chromium-browser/chromedriver /usr/local/bin/chromedriver` +* If you don't already have it, install or Chrome or Chromium for browser tests, eg `sudo apt install chromium-browser`. From a942ee127a3fe2017e1512e28c1802a1aed167da Mon Sep 17 00:00:00 2001 From: Vivian Brown Date: Mon, 9 Apr 2018 15:15:28 -0400 Subject: [PATCH 7/9] Run Chrome with --no-sandbox flag Runs chrome with the --no-sandbox flag in order to pass tests in Travis, as mentioned in https://github.com/travis-ci/travis-ci/issues/8836#issuecomment-348248951 The additional changes to spec/rails_helper.rb in this commit are just to reproduce the behavior of the predefined :selenium_chrome_headless driver. --- Gemfile | 3 ++- spec/rails_helper.rb | 13 ++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index c05f06c1..be5b1db6 100644 --- a/Gemfile +++ b/Gemfile @@ -99,8 +99,9 @@ group :test do gem "rack_session_access" gem "rspec-core" gem "rspec-rails" - gem "selenium-webdriver" + # Run browser tests with selenium and headless chrome gem "chromedriver-helper" + gem "selenium-webdriver" end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 31953cd8..2051dd0a 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -30,7 +30,18 @@ ActiveRecord::Migration.maintain_test_schema! require "capybara/rspec" -Capybara.javascript_driver = :selenium_chrome_headless + +capabilities = Selenium::WebDriver::Remote::Capabilities.chrome( + 'chromeOptions' => { + 'args' => ['--headless', '--disable-gpu', '--no-sandbox'] + } +) + +Capybara.register_driver :chrome_headless do |app| + Capybara::Selenium::Driver.new(app, browser: :chrome, desired_capabilities: capabilities) +end + +Capybara.javascript_driver = :chrome_headless RSpec.configure do |config| # Load authentication helpers From dce44eb88681c95e68b85f418b33149204bd5d8a Mon Sep 17 00:00:00 2001 From: Vivian Brown Date: Tue, 10 Apr 2018 16:09:35 -0400 Subject: [PATCH 8/9] Only run chrome without sandbox in Travis --- spec/rails_helper.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 2051dd0a..2141f8a5 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -33,7 +33,9 @@ capabilities = Selenium::WebDriver::Remote::Capabilities.chrome( 'chromeOptions' => { - 'args' => ['--headless', '--disable-gpu', '--no-sandbox'] + 'args' => ['--headless', '--disable-gpu'].tap do |a| + a.push('--no-sandbox') if ENV['TRAVIS'] + end } ) From a68ec626c0acaf2c84a52fee448ecae2a9cb7163 Mon Sep 17 00:00:00 2001 From: Vivian Brown Date: Tue, 10 Apr 2018 16:16:34 -0400 Subject: [PATCH 9/9] Not supporting browser testing in Docker yet --- Dockerfile | 4 ---- README.md | 2 ++ 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 41a8a3ce..7736842c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,7 +25,6 @@ RUN echo "@edge http://nl.alpinelinux.org/alpine/edge/main" >>/etc/apk/repositor wkhtmltopdf@edgetesting \ nodejs \ yarn \ - chromium \ # Needed for wkhtmltopdf dbus \ @@ -38,9 +37,6 @@ RUN echo "@edge http://nl.alpinelinux.org/alpine/edge/main" >>/etc/apk/repositor 'cd /opt/trainers-hub && bundle exec rake glossary:update' >>/proc/1/fd/1 2>&1" >>/etc/crontab ENV DISPLAY=:99 -ENV CHROME_BIN=/usr/bin/chromium-browser -ENV CHROME_PATH=/usr/lib/chromium/ - COPY Gemfile* ./ RUN bundle install diff --git a/README.md b/README.md index 7e7aa56d..93b8c864 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,8 @@ After running `rake db:setup` you can navigate to `http://localhost:3000/admin` - **User**: admin@example.com - **Password**: password +Running browser tests from within Docker is not currently supported. See [#477](https://github.com/EFForg/sec/pull/447) for more information. + ## Development without Docker * Install Ruby, Rails, and other gems the usual way.