From ac6366318741be06da14e5ee676ff133d5b1eba3 Mon Sep 17 00:00:00 2001 From: Lars Olesen Date: Thu, 16 Apr 2020 07:15:18 +0000 Subject: [PATCH] Initial try to add testing --- .rspec | 1 + Gemfile | 11 +++++++++ spec/index_spec.rb | 9 +++++++ spec/spec_helper.rb | 57 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 .rspec create mode 100644 spec/index_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/.rspec b/.rspec new file mode 100644 index 000000000000..c99d2e7396e1 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/Gemfile b/Gemfile index 72f225daa13d..3aa8cef836f9 100644 --- a/Gemfile +++ b/Gemfile @@ -16,3 +16,14 @@ end gem 'classifier-reborn' gem 'gsl' gem 'html-proofer' + +group :development, :test do + gem 'html-proofer' + gem "rspec" + gem "selenium-webdriver" + gem "webdrivers" + gem "puma" + gem "capybara" + gem "rack-jekyll" + gem "pry" +end diff --git a/spec/index_spec.rb b/spec/index_spec.rb new file mode 100644 index 000000000000..ea54f9a4006d --- /dev/null +++ b/spec/index_spec.rb @@ -0,0 +1,9 @@ +describe "index", type: :feature, js: true do + it "has the page title" do + visit '/' + # `binding.pry` is useful for crafting the right selector + # or checking the actual state of the page + binding.pry # test will pause here + expect(find('.post-link').text).to eq('Welcome to Jekyll!') + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 000000000000..619bc7c94fe4 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,57 @@ +# Require all of the necessary gems +require 'rspec' +require 'capybara/rspec' +require 'rack/jekyll' +require 'rack/test' +require 'pry' +require 'webdrivers' + +RSpec.configure do |config| + config.expect_with :rspec do |expectations| + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + config.mock_with :rspec do |mocks| + mocks.verify_partial_doubles = true + end + # Configure Capybara to use Selenium. + Capybara.register_driver :selenium do |app| + chrome_prefs = { + 'download' => { + 'default_directory' => './support/', + 'prompt_for_download' => false + }, + 'profile' => { + 'default_content_settings' => { 'multiple-automatic-downloads': 1 }, # for chrome version olde ~42 + 'default_content_setting_values' => { 'automatic_downloads': 1 }, # for chrome newe 46 + 'password_manager_enabled' => false + }, + 'safebrowsing' => { + 'enabled' => false, + 'disable_download_protection' => true + } + } + + # Set headless with docker friendly args. + chrome_args = %w[window-size=1024,768 disable-gpu no-sandbox disable-translate no-default-browser-check disable-popup-blocking] + # To run full browser instead of headless mode, run this command: HEADLESS=false rspec spec + unless ENV.fetch('HEADLESS', 'true') == 'false' + chrome_args += %w[headless] + end + + # Initialize chromedriver. + capabilities = Selenium::WebDriver::Remote::Capabilities.chrome( + chromeOptions: { + prefs: chrome_prefs, + args: chrome_args + } + ) + driver = Capybara::Selenium::Driver.new(app, browser: :chrome, desired_capabilities: capabilities) + + driver + end + # Configure Capybara to load the website through rack-jekyll. + # (force_build: true) builds the site before the tests are run, + # so our tests are always running against the latest version + # of our jekyll site. + Capybara.app = Rack::Jekyll.new(force_build: true) +end