From aa6cd07ee48aab695d41038c9462a39fbe9e509b Mon Sep 17 00:00:00 2001 From: Trey Pendragon Date: Fri, 15 Feb 2019 15:55:24 -0800 Subject: [PATCH] Javascript specs. Co-authored-by: GriffinJ --- Gemfile | 8 +++- Gemfile.lock | 30 +++++++------ spec/features/catalog_show_spec.rb | 54 ++++++++++++++++++++++++ spec/support/capybara_selenium.rb | 23 ++++++++++ spec/support/devise_helpers.rb | 7 ++- spec/support/features/session_helpers.rb | 8 +++- spec/support/request_spec_helper.rb | 24 +++++++++++ 7 files changed, 136 insertions(+), 18 deletions(-) create mode 100644 spec/features/catalog_show_spec.rb create mode 100644 spec/support/capybara_selenium.rb create mode 100644 spec/support/request_spec_helper.rb diff --git a/Gemfile b/Gemfile index 6ecc0b12..5b292f53 100644 --- a/Gemfile +++ b/Gemfile @@ -16,9 +16,8 @@ group :development, :test do gem 'capistrano-passenger' gem 'capistrano-rails' gem 'capistrano-rails-console' - gem 'capybara' + gem 'capybara', '~> 2.0' gem 'capybara-screenshot' - gem 'capybara-selenium' gem 'coveralls', require: false gem 'database_cleaner' gem 'factory_bot_rails', require: false @@ -26,6 +25,7 @@ group :development, :test do gem 'pry-rails' gem 'rails-controller-testing' gem 'rspec-rails' + gem "selenium-webdriver" gem 'simplecov', require: false gem 'sqlite3' gem 'sshkit', '~> 1.18' @@ -33,6 +33,10 @@ group :development, :test do gem 'webmock', require: false end +group :test do + gem "chromedriver-helper" +end + group :development do gem 'foreman' gem 'puma' diff --git a/Gemfile.lock b/Gemfile.lock index ed4465c8..b3022e68 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -114,6 +114,8 @@ GEM almond-rails (0.1.0) rails (>= 4.2, < 6) amq-protocol (2.3.0) + archive-zip (0.11.0) + io-like (~> 0.3.0) arel (7.1.4) ast (2.4.0) autoprefixer-rails (6.7.6) @@ -179,25 +181,25 @@ GEM capistrano-rails-console (2.1.1) capistrano (>= 3.5.0, < 4.0.0) sshkit-interactive (~> 0.2.0) - capybara (2.10.1) + capybara (2.18.0) addressable - mime-types (>= 1.16) + mini_mime (>= 0.1.3) nokogiri (>= 1.3.3) rack (>= 1.0.0) rack-test (>= 0.5.4) - xpath (~> 2.0) + xpath (>= 2.0, < 4.0) capybara-screenshot (1.0.22) capybara (>= 1.0, < 4) launchy - capybara-selenium (0.0.6) - capybara - selenium-webdriver carrierwave (1.2.2) activemodel (>= 4.0.0) activesupport (>= 4.0.0) mime-types (>= 1.16) childprocess (0.9.0) ffi (~> 1.0, >= 1.0.11) + chromedriver-helper (2.1.0) + archive-zip (~> 0.10) + nokogiri (~> 1.8) clipboard-rails (1.7.1) coderay (1.1.1) coffee-rails (4.1.1) @@ -252,7 +254,7 @@ GEM multipart-post (>= 1.2, < 3) faraday_middleware (0.12.2) faraday (>= 0.7.4, < 1.0) - ffi (1.9.25) + ffi (1.10.0) foreman (0.85.0) thor (~> 0.19.1) github-markup (2.0.0) @@ -275,6 +277,7 @@ GEM iiif_manifest (0.1.2) activesupport (>= 4) iiif-presentation (~> 0.1.0) + io-like (0.3.0) iso-639 (0.2.8) jbuilder (2.6.0) activesupport (>= 3.0.0, < 5.1) @@ -475,9 +478,9 @@ GEM sdoc (0.4.2) json (~> 1.7, >= 1.7.7) rdoc (~> 4.0) - selenium-webdriver (3.12.0) + selenium-webdriver (3.141.0) childprocess (~> 0.5) - rubyzip (~> 1.2) + rubyzip (~> 1.2, >= 1.2.2) serverengine (2.0.6) sigdump (~> 0.2.2) sidekiq (4.2.2) @@ -584,8 +587,8 @@ GEM websocket-driver (0.6.5) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.3) - xpath (2.0.0) - nokogiri (~> 1.3) + xpath (3.2.0) + nokogiri (~> 1.8) PLATFORMS ruby @@ -601,9 +604,9 @@ DEPENDENCIES capistrano-passenger capistrano-rails capistrano-rails-console - capybara + capybara (~> 2.0) capybara-screenshot - capybara-selenium + chromedriver-helper coffee-rails coveralls dalli @@ -644,6 +647,7 @@ DEPENDENCIES rubyzip (>= 1.2.2) sass-rails (~> 5.0) sdoc (~> 0.4.0) + selenium-webdriver sidekiq simplecov sitemap_generator diff --git a/spec/features/catalog_show_spec.rb b/spec/features/catalog_show_spec.rb new file mode 100644 index 00000000..1caa1557 --- /dev/null +++ b/spec/features/catalog_show_spec.rb @@ -0,0 +1,54 @@ +require 'rails_helper' + +RSpec.describe 'Catalog', type: :feature, js: true do + let(:exhibit) { FactoryBot.create(:exhibit, title: 'Exhibit Title 1') } + let(:curator) { FactoryBot.create(:exhibit_curator, exhibit: exhibit) } + let(:admin) { FactoryBot.create(:exhibit_admin, exhibit: exhibit) } + let(:document_id) { '1r66j4408' } + let(:id) { Digest::MD5.hexdigest("#{exhibit.id}-#{document_id}") } + let(:document) do + SolrDocument.new( + id: id + ) + end + + before do + sign_in admin + Spotlight::SolrDocumentSidecar.create!( + document: document, exhibit: exhibit, + data: { + full_title_tesim: [ + 'test item' + ], + access_identifier_ssim: [ + "1r66j4408" + ], + 'readonly_description_ssim': [ + "panoramic" * 50 + ], + 'exhibit_exhibit-title-1_readonly_description_ssim': [ + "panoramic" * 50 + ], + 'content_metadata_iiif_manifest_field_ssi': [ + 'http://images.institution.edu' + ] + } + ) + + document.make_public! exhibit + document.reindex + Blacklight.default_index.connection.commit + + Spotlight::CustomField.create!(exhibit: exhibit, slug: 'description', field: 'readonly_description_ssim', configuration: { "label" => "Description" }, field_type: 'vocab', readonly_field: true) + end + + it 'will add a ... More link in the field' do + visit spotlight.exhibit_solr_document_path(exhibit, document_id) + expect(page).to have_content 'panoramic' + expect(page).to have_selector "a.morelink" + end + + def index + Blacklight.default_index.connection + end +end diff --git a/spec/support/capybara_selenium.rb b/spec/support/capybara_selenium.rb new file mode 100644 index 00000000..9aae886d --- /dev/null +++ b/spec/support/capybara_selenium.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require 'capybara/rspec' +require 'selenium-webdriver' + +# there's a bug in capybara-screenshot that requires us to name +# the driver ":selenium" so we changed it from :headless_chrome" +Capybara.register_driver(:selenium) do |app| + capabilities = Selenium::WebDriver::Remote::Capabilities.chrome( + chromeOptions: { args: %w[headless disable-gpu disable-setuid-sandbox window-size=7680,4320] } + ) + + http_client = Selenium::WebDriver::Remote::Http::Default.new + http_client.read_timeout = 120 + http_client.open_timeout = 120 + Capybara::Selenium::Driver.new(app, + browser: :chrome, + desired_capabilities: capabilities, + http_client: http_client) +end + +Capybara.javascript_driver = :selenium +Capybara.default_max_wait_time = 15 diff --git a/spec/support/devise_helpers.rb b/spec/support/devise_helpers.rb index 0cf56ea8..3377e81e 100644 --- a/spec/support/devise_helpers.rb +++ b/spec/support/devise_helpers.rb @@ -1,4 +1,9 @@ +# frozen_string_literal: true + +require_relative 'request_spec_helper' RSpec.configure do |config| - config.include Devise::Test::ControllerHelpers, type: :controller config.include Devise::Test::ControllerHelpers, type: :view + config.include Devise::Test::ControllerHelpers, type: :controller + config.include RequestSpecHelper, type: :request + config.include RequestSpecHelper, type: :feature, js: true end diff --git a/spec/support/features/session_helpers.rb b/spec/support/features/session_helpers.rb index ec0ccdf5..4933475d 100644 --- a/spec/support/features/session_helpers.rb +++ b/spec/support/features/session_helpers.rb @@ -4,12 +4,16 @@ module SessionHelpers # Use this in feature tests def sign_in(who = :user) user = if who.instance_of?(User) - who.username + who.uid else - FactoryBot.create(:user).username + FactoryBot.create(:user).uid end + OmniAuth.config.test_mode = true OmniAuth.config.add_mock(:cas, uid: user) visit user_cas_omniauth_authorize_path end end end +RSpec.configure do |config| + config.include Features::SessionHelpers, type: :feature +end diff --git a/spec/support/request_spec_helper.rb b/spec/support/request_spec_helper.rb new file mode 100644 index 00000000..fe3f5868 --- /dev/null +++ b/spec/support/request_spec_helper.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +module RequestSpecHelper + include Warden::Test::Helpers + + def self.included(base) + base.before { Warden.test_mode! } + base.after { Warden.test_reset! } + end + + def sign_in(resource) + login_as(resource, scope: warden_scope(resource)) + end + + def sign_out(resource) + logout(warden_scope(resource)) + end + + private + + def warden_scope(resource) + resource.class.name.underscore.to_sym + end +end