From ec07a3cd3dea5dd229eab7ceb668d5579d054ec0 Mon Sep 17 00:00:00 2001 From: filipefurtad0 Date: Tue, 20 Jul 2021 15:52:21 +0100 Subject: [PATCH 01/12] Add the Ferrum-Cuprite driver to the stack --- Gemfile | 1 + spec/system/first_system_spec.rb | 23 +++++++ .../support/better_rails_system_tests.rb | 45 +++++++++++++ spec/system/support/capybara_setup.rb | 28 ++++++++ spec/system/support/cuprite_setup.rb | 44 +++++++++++++ spec/system/support/precompile_assets.rb | 65 +++++++++++++++++++ spec/system_helper.rb | 6 ++ 7 files changed, 212 insertions(+) create mode 100644 spec/system/first_system_spec.rb create mode 100644 spec/system/support/better_rails_system_tests.rb create mode 100644 spec/system/support/capybara_setup.rb create mode 100644 spec/system/support/cuprite_setup.rb create mode 100644 spec/system/support/precompile_assets.rb create mode 100644 spec/system_helper.rb diff --git a/Gemfile b/Gemfile index 3df788509a..a85c2e0ffe 100644 --- a/Gemfile +++ b/Gemfile @@ -150,6 +150,7 @@ group :test, :development do gem 'timecop' gem 'unicorn-rails' gem 'webdrivers' + gem 'cuprite' end group :test do diff --git a/spec/system/first_system_spec.rb b/spec/system/first_system_spec.rb new file mode 100644 index 0000000000..7ed33b80f3 --- /dev/null +++ b/spec/system/first_system_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require "system_helper" + +feature "visit admin", js: true do + include UIComponentHelper + include AuthenticationHelper + include WebHelper + let(:user) { create(:user, password: "password", password_confirmation: "password") } + let!(:enterprise) { create(:enterprise, owner: user) } # Required for access to admin + + scenario "logging into admin redirects home, then back to admin" do + visit spree.admin_dashboard_path + + fill_in "Email", with: user.email + fill_in "Password", with: user.password + + click_login_button + expect(page).to have_content "DASHBOARD" + expect(page).to have_current_path spree.admin_dashboard_path + expect(page).to have_no_content "CONFIGURATION" + end +end \ No newline at end of file diff --git a/spec/system/support/better_rails_system_tests.rb b/spec/system/support/better_rails_system_tests.rb new file mode 100644 index 0000000000..2b5828f778 --- /dev/null +++ b/spec/system/support/better_rails_system_tests.rb @@ -0,0 +1,45 @@ +# spec/system/support/better_rails_system_tests.rb + +module BetterRailsSystemTests + # Use our `Capybara.save_path` to store screenshots with other capybara artifacts + # (Rails screenshots path is not configurable https://github.com/rails/rails/blob/49baf092439fc74fc3377b12e3334c3dd9d0752f/actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb#L79) + def absolute_image_path + Rails.root.join("#{Capybara.save_path}/screenshots/#{image_name}.png") + end + + # Make failure screenshots compatible with multi-session setup. + # That's where we use Capybara.last_used_session introduced before. + def take_screenshot + return super unless Capybara.last_used_session + + Capybara.using_session(Capybara.last_used_session) { super } + end + + ## Use relative path in screenshot message + ##def image_path + ## absolute_image_path.relative_path_from(Rails.root).to_s + ##end + +end + +RSpec.configure do |config| + config.include BetterRailsSystemTests, type: :system + + # Make urls in mailers contain the correct server host. + # It's required for testing links in emails (e.g., via capybara-email). + config.around(:each, type: :system) do |ex| + was_host = Rails.application.default_url_options[:host] + Rails.application.default_url_options[:host] = Capybara.server_host + ex.run + Rails.application.default_url_options[:host] = was_host + end + + # Make sure this hook runs before others + config.prepend_before(:each, type: :system) do + # Rails sets host to `127.0.0.1` for every test by default. + # That won't work with a remote browser. + host! CAPYBARA_APP_HOST + # Use JS driver always + driven_by Capybara.javascript_driver + end +end diff --git a/spec/system/support/capybara_setup.rb b/spec/system/support/capybara_setup.rb new file mode 100644 index 0000000000..c631bcb2eb --- /dev/null +++ b/spec/system/support/capybara_setup.rb @@ -0,0 +1,28 @@ +# spec/system/support/capybara_setup.rb + +# Usually, especially when using Selenium, developers tend to increase the max wait time. +# With Cuprite, there is no need for that. +# We use a Capybara default value here explicitly. +Capybara.default_max_wait_time = 2 + +# Normalize whitespaces when using `has_text?` and similar matchers, +# i.e., ignore newlines, trailing spaces, etc. +# That makes tests less dependent on slightly UI changes. +Capybara.default_normalize_ws = true + +# Where to store system tests artifacts (e.g. screenshots, downloaded files, etc.). +# It could be useful to be able to configure this path from the outside (e.g., on CI). +Capybara.save_path = ENV.fetch("CAPYBARA_ARTIFACTS", "./tmp/capybara") + +# spec/system/support/capybara_setup.rb + +Capybara.singleton_class.prepend(Module.new do + attr_accessor :last_used_session + + def using_session(name, &block) + self.last_used_session = name + super + ensure + self.last_used_session = nil + end +end) diff --git a/spec/system/support/cuprite_setup.rb b/spec/system/support/cuprite_setup.rb new file mode 100644 index 0000000000..3b3ea6f6d8 --- /dev/null +++ b/spec/system/support/cuprite_setup.rb @@ -0,0 +1,44 @@ +# spec/system/support/cuprite_setup.rb + +# First, load Cuprite Capybara integration +require "capybara/cuprite" + +# Then, we need to register our driver to be able to use it later +# with #driven_by method. +Capybara.register_driver(:cuprite) do |app| + Capybara::Cuprite::Driver.new( + app, + **{ + window_size: [1200, 800], + # See additional options for Dockerized environment in the respective section of this article + browser_options: {}, + # Increase Chrome startup wait time (required for stable CI builds) + process_timeout: 10, + # Enable debugging capabilities + inspector: true, + # Allow running Chrome in a headful mode by setting HEADLESS env + headless: false + } + ) +end + +# Configure Capybara to use :cuprite driver by default +Capybara.default_driver = Capybara.javascript_driver = :cuprite + +module CupriteHelpers + # Drop #pause anywhere in a test to stop the execution. + # Useful when you want to checkout the contents of a web page in the middle of a test + # running in a headful mode. + def pause + page.driver.pause + end + + # Drop #debug anywhere in a test to open a Chrome inspector and pause the execution + def debug(*args) + page.driver.debug(*args) + end +end + +RSpec.configure do |config| + config.include CupriteHelpers, type: :system +end diff --git a/spec/system/support/precompile_assets.rb b/spec/system/support/precompile_assets.rb new file mode 100644 index 0000000000..7d6d7ccdeb --- /dev/null +++ b/spec/system/support/precompile_assets.rb @@ -0,0 +1,65 @@ +RSpec.configure do |config| + # Skip assets precompilcation if we exclude system specs. + # For example, you can run all non-system tests via the following command: + # + # rspec --tag ~type:system + # + # In this case, we don't need to precompile assets. + next if config.filter.opposite.rules[:type] == "system" || config.exclude_pattern.match?(%r{spec/system}) + + config.before(:suite) do + # We can use webpack-dev-server for tests, too! + # Useful if you working on a frontend code fixes and want to verify them via system tests. + if Webpacker.dev_server.running? + $stdout.puts "\n⚙️ Webpack dev server is running! Skip assets compilation.\n" + next + else + $stdout.puts "\n🐢 Precompiling assets.\n" + + # The code to run webpacker:compile Rake task + # ... + end + end +end + +=begin + +# frozen_string_literal: true + +# Precompile assets before running tests to avoid timeouts. +# Do not precompile if webpack-dev-server is running (NOTE: MUST be launched with RAILS_ENV=test) +RSpec.configure do |config| + config.before(:suite) do + examples = RSpec.world.filtered_examples.values.flatten + has_no_system_tests = examples.none? { |example| example.metadata[:type] == :system } + + if has_no_system_tests + $stdout.puts "\n🚀️️ No system test selected. Skip assets compilation.\n" + next + end + + if Webpacker.dev_server.running? + $stdout.puts "\n⚙️ Webpack dev server is running! Skip assets compilation.\n" + next + else + $stdout.puts "\n🐢 Precompiling assets.\n" + original_stdout = $stdout.clone + # Use test-prof now 'cause it couldn't be monkey-patched (e.g., by Timecop or similar) + start = Time.current + begin + # Silence Webpacker output + $stdout.reopen(File.new("/dev/null", "w")) + # next 3 lines to compile webpacker before running our test suite + require "rake" + Rails.application.load_tasks + Rake::Task["webpacker:compile"].execute + ensure + $stdout.reopen(original_stdout) + $stdout.puts "Finished in #{(Time.current - start).round(2)} seconds" + end + end + end +end + +=end + diff --git a/spec/system_helper.rb b/spec/system_helper.rb new file mode 100644 index 0000000000..354b413b49 --- /dev/null +++ b/spec/system_helper.rb @@ -0,0 +1,6 @@ +# frozen_string_literal: true + +require "rails_helper" + +# system/support/ files contain system tests configurations and helpers +Dir[File.join(__dir__, "system/support/**/*.rb")].sort.each { |file| require file } From 386480d493360cd455aa5340716de019214f9579 Mon Sep 17 00:00:00 2001 From: filipefurtad0 Date: Wed, 21 Jul 2021 15:57:42 +0100 Subject: [PATCH 02/12] Adds changes on Gemfile.lock file --- Gemfile.lock | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Gemfile.lock b/Gemfile.lock index 1609fdd768..2621bb1616 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -185,6 +185,7 @@ GEM chronic (0.10.2) chunky_png (1.4.0) climate_control (0.2.0) + cliver (0.3.2) cocaine (0.5.8) climate_control (>= 0.0.3, < 1.0) codecov (0.5.2) @@ -222,6 +223,9 @@ GEM crass (1.0.6) css_parser (1.9.0) addressable + cuprite (0.13) + capybara (>= 2.1, < 4) + ferrum (~> 0.11.0) dalli (2.7.11) database_cleaner (2.0.1) database_cleaner-active_record (~> 2.0.0) @@ -269,6 +273,11 @@ GEM faraday-excon (1.1.0) faraday-net_http (1.0.1) faraday-net_http_persistent (1.1.0) + ferrum (0.11) + addressable (~> 2.5) + cliver (~> 0.3) + concurrent-ruby (~> 1.1) + websocket-driver (>= 0.6, < 0.8) ffaker (2.18.0) ffi (1.15.3) flipper (0.20.4) @@ -706,6 +715,7 @@ DEPENDENCIES coffee-rails (~> 5.0.0) combine_pdf compass-rails + cuprite custom_error_message! dalli database_cleaner From ecbab52f066407eabe74af534fd1c78b37eb2425 Mon Sep 17 00:00:00 2001 From: filipefurtad0 Date: Wed, 21 Jul 2021 15:59:08 +0100 Subject: [PATCH 03/12] Removes unecessary CAPYBARA_APP_HOST specification --- spec/system/support/better_rails_system_tests.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/spec/system/support/better_rails_system_tests.rb b/spec/system/support/better_rails_system_tests.rb index 2b5828f778..7b4ed4be25 100644 --- a/spec/system/support/better_rails_system_tests.rb +++ b/spec/system/support/better_rails_system_tests.rb @@ -36,10 +36,7 @@ RSpec.configure do |config| # Make sure this hook runs before others config.prepend_before(:each, type: :system) do - # Rails sets host to `127.0.0.1` for every test by default. - # That won't work with a remote browser. - host! CAPYBARA_APP_HOST - # Use JS driver always - driven_by Capybara.javascript_driver + # Use JS driver always + driven_by Capybara.javascript_driver end end From df7f4d0a31addd018c512f85d383598ec8551a01 Mon Sep 17 00:00:00 2001 From: filipefurtad0 Date: Wed, 21 Jul 2021 16:25:20 +0100 Subject: [PATCH 04/12] Removed some unecessary comments --- .../support/better_rails_system_tests.rb | 6 --- spec/system/support/precompile_assets.rb | 40 ------------------- 2 files changed, 46 deletions(-) diff --git a/spec/system/support/better_rails_system_tests.rb b/spec/system/support/better_rails_system_tests.rb index 7b4ed4be25..1d859e2b3a 100644 --- a/spec/system/support/better_rails_system_tests.rb +++ b/spec/system/support/better_rails_system_tests.rb @@ -14,12 +14,6 @@ module BetterRailsSystemTests Capybara.using_session(Capybara.last_used_session) { super } end - - ## Use relative path in screenshot message - ##def image_path - ## absolute_image_path.relative_path_from(Rails.root).to_s - ##end - end RSpec.configure do |config| diff --git a/spec/system/support/precompile_assets.rb b/spec/system/support/precompile_assets.rb index 7d6d7ccdeb..413969e0b6 100644 --- a/spec/system/support/precompile_assets.rb +++ b/spec/system/support/precompile_assets.rb @@ -22,44 +22,4 @@ RSpec.configure do |config| end end -=begin - -# frozen_string_literal: true - -# Precompile assets before running tests to avoid timeouts. -# Do not precompile if webpack-dev-server is running (NOTE: MUST be launched with RAILS_ENV=test) -RSpec.configure do |config| - config.before(:suite) do - examples = RSpec.world.filtered_examples.values.flatten - has_no_system_tests = examples.none? { |example| example.metadata[:type] == :system } - - if has_no_system_tests - $stdout.puts "\n🚀️️ No system test selected. Skip assets compilation.\n" - next - end - - if Webpacker.dev_server.running? - $stdout.puts "\n⚙️ Webpack dev server is running! Skip assets compilation.\n" - next - else - $stdout.puts "\n🐢 Precompiling assets.\n" - original_stdout = $stdout.clone - # Use test-prof now 'cause it couldn't be monkey-patched (e.g., by Timecop or similar) - start = Time.current - begin - # Silence Webpacker output - $stdout.reopen(File.new("/dev/null", "w")) - # next 3 lines to compile webpacker before running our test suite - require "rake" - Rails.application.load_tasks - Rake::Task["webpacker:compile"].execute - ensure - $stdout.reopen(original_stdout) - $stdout.puts "Finished in #{(Time.current - start).round(2)} seconds" - end - end - end -end - -=end From 85d2db52aee0a1e64ee0b944f5a407570ffecac6 Mon Sep 17 00:00:00 2001 From: filipefurtad0 Date: Wed, 21 Jul 2021 16:42:46 +0100 Subject: [PATCH 05/12] Sets headless: true --- spec/system/support/cuprite_setup.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/system/support/cuprite_setup.rb b/spec/system/support/cuprite_setup.rb index 3b3ea6f6d8..f164f56e7d 100644 --- a/spec/system/support/cuprite_setup.rb +++ b/spec/system/support/cuprite_setup.rb @@ -17,7 +17,7 @@ Capybara.register_driver(:cuprite) do |app| # Enable debugging capabilities inspector: true, # Allow running Chrome in a headful mode by setting HEADLESS env - headless: false + headless: true } ) end From 94e476d21f65c74e77ee865242c1cacb757dc436 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Wed, 21 Jul 2021 18:58:28 +0100 Subject: [PATCH 06/12] Run system tests in their own action --- .github/workflows/build.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 671c5e3f43..ff47fad6ba 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -278,8 +278,11 @@ jobs: - name: Run migration tests run: bundle exec rspec --pattern "spec/{migrations}/**/*_spec.rb" + - name: Run system tests + run: bundle exec rspec --profile -- spec/system + - name: Run all other tests - run: bundle exec rake ofn:specs:run:excluding_folders["models,controllers,serializers,features,lib,migrations"] + run: bundle exec rake ofn:specs:run:excluding_folders["models,controllers,serializers,features,lib,migrations,system"] test-the-rest: runs-on: ubuntu-18.04 From ac64908c2bd87f2e663e8e22c3ff48f8c5a1c7f3 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 22 Jul 2021 10:18:18 +0100 Subject: [PATCH 07/12] Tidy up compiling helper --- spec/system/support/precompile_assets.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/spec/system/support/precompile_assets.rb b/spec/system/support/precompile_assets.rb index 413969e0b6..519ecb0fca 100644 --- a/spec/system/support/precompile_assets.rb +++ b/spec/system/support/precompile_assets.rb @@ -11,13 +11,11 @@ RSpec.configure do |config| # We can use webpack-dev-server for tests, too! # Useful if you working on a frontend code fixes and want to verify them via system tests. if Webpacker.dev_server.running? - $stdout.puts "\n⚙️ Webpack dev server is running! Skip assets compilation.\n" next else - $stdout.puts "\n🐢 Precompiling assets.\n" + $stdout.puts "\n Precompiling assets.\n" - # The code to run webpacker:compile Rake task - # ... + Webpacker.compile end end end From a14059a77d21d9eae8308afc815201f4df913943 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 22 Jul 2021 10:18:50 +0100 Subject: [PATCH 08/12] Adjust timeout settings for more flexibility --- spec/system/support/capybara_setup.rb | 2 +- spec/system/support/cuprite_setup.rb | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/spec/system/support/capybara_setup.rb b/spec/system/support/capybara_setup.rb index c631bcb2eb..c1a743e0cb 100644 --- a/spec/system/support/capybara_setup.rb +++ b/spec/system/support/capybara_setup.rb @@ -3,7 +3,7 @@ # Usually, especially when using Selenium, developers tend to increase the max wait time. # With Cuprite, there is no need for that. # We use a Capybara default value here explicitly. -Capybara.default_max_wait_time = 2 +Capybara.default_max_wait_time = 10 # Normalize whitespaces when using `has_text?` and similar matchers, # i.e., ignore newlines, trailing spaces, etc. diff --git a/spec/system/support/cuprite_setup.rb b/spec/system/support/cuprite_setup.rb index f164f56e7d..9dde447d9e 100644 --- a/spec/system/support/cuprite_setup.rb +++ b/spec/system/support/cuprite_setup.rb @@ -13,7 +13,8 @@ Capybara.register_driver(:cuprite) do |app| # See additional options for Dockerized environment in the respective section of this article browser_options: {}, # Increase Chrome startup wait time (required for stable CI builds) - process_timeout: 10, + process_timeout: 20, + timeout: 20, # Enable debugging capabilities inspector: true, # Allow running Chrome in a headful mode by setting HEADLESS env From 0fbba0fa9dd253869b2663fcd6f517c594da2405 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 22 Jul 2021 10:20:57 +0100 Subject: [PATCH 09/12] Clean up some comments --- spec/system/support/better_rails_system_tests.rb | 2 -- spec/system/support/capybara_setup.rb | 4 ---- spec/system/support/cuprite_setup.rb | 9 --------- 3 files changed, 15 deletions(-) diff --git a/spec/system/support/better_rails_system_tests.rb b/spec/system/support/better_rails_system_tests.rb index 1d859e2b3a..e717fd1a23 100644 --- a/spec/system/support/better_rails_system_tests.rb +++ b/spec/system/support/better_rails_system_tests.rb @@ -1,5 +1,3 @@ -# spec/system/support/better_rails_system_tests.rb - module BetterRailsSystemTests # Use our `Capybara.save_path` to store screenshots with other capybara artifacts # (Rails screenshots path is not configurable https://github.com/rails/rails/blob/49baf092439fc74fc3377b12e3334c3dd9d0752f/actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb#L79) diff --git a/spec/system/support/capybara_setup.rb b/spec/system/support/capybara_setup.rb index c1a743e0cb..dea6cfa57c 100644 --- a/spec/system/support/capybara_setup.rb +++ b/spec/system/support/capybara_setup.rb @@ -1,5 +1,3 @@ -# spec/system/support/capybara_setup.rb - # Usually, especially when using Selenium, developers tend to increase the max wait time. # With Cuprite, there is no need for that. # We use a Capybara default value here explicitly. @@ -14,8 +12,6 @@ Capybara.default_normalize_ws = true # It could be useful to be able to configure this path from the outside (e.g., on CI). Capybara.save_path = ENV.fetch("CAPYBARA_ARTIFACTS", "./tmp/capybara") -# spec/system/support/capybara_setup.rb - Capybara.singleton_class.prepend(Module.new do attr_accessor :last_used_session diff --git a/spec/system/support/cuprite_setup.rb b/spec/system/support/cuprite_setup.rb index 9dde447d9e..d9102b5729 100644 --- a/spec/system/support/cuprite_setup.rb +++ b/spec/system/support/cuprite_setup.rb @@ -1,23 +1,14 @@ -# spec/system/support/cuprite_setup.rb - -# First, load Cuprite Capybara integration require "capybara/cuprite" -# Then, we need to register our driver to be able to use it later -# with #driven_by method. Capybara.register_driver(:cuprite) do |app| Capybara::Cuprite::Driver.new( app, **{ window_size: [1200, 800], - # See additional options for Dockerized environment in the respective section of this article browser_options: {}, - # Increase Chrome startup wait time (required for stable CI builds) process_timeout: 20, timeout: 20, - # Enable debugging capabilities inspector: true, - # Allow running Chrome in a headful mode by setting HEADLESS env headless: true } ) From 7ee6e48f17988494923bc1ec52d2aafcff494081 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 22 Jul 2021 10:48:16 +0100 Subject: [PATCH 10/12] Ignore external scripts In some cases the browser will try to load external files like fonts, Google maps JS, Stripe JS. This can cause network issues on slow connections and add a lot of unnecessary time to each test. --- spec/system/support/cuprite_setup.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/system/support/cuprite_setup.rb b/spec/system/support/cuprite_setup.rb index d9102b5729..315d3ca32c 100644 --- a/spec/system/support/cuprite_setup.rb +++ b/spec/system/support/cuprite_setup.rb @@ -8,6 +8,8 @@ Capybara.register_driver(:cuprite) do |app| browser_options: {}, process_timeout: 20, timeout: 20, + # Don't load scripts from external sources, like google maps or stripe + url_whitelist: ["http://localhost", "http://0.0.0.0", "http://127.0.0.1"], inspector: true, headless: true } From 1c493c72389767c472900e49a4d785cb3e03efd0 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 22 Jul 2021 10:49:14 +0100 Subject: [PATCH 11/12] Don't use `feature` keyword in system test --- spec/system/first_system_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/system/first_system_spec.rb b/spec/system/first_system_spec.rb index 7ed33b80f3..dd148a253b 100644 --- a/spec/system/first_system_spec.rb +++ b/spec/system/first_system_spec.rb @@ -2,7 +2,7 @@ require "system_helper" -feature "visit admin", js: true do +describe "Visit Admin", js: true do include UIComponentHelper include AuthenticationHelper include WebHelper @@ -20,4 +20,4 @@ feature "visit admin", js: true do expect(page).to have_current_path spree.admin_dashboard_path expect(page).to have_no_content "CONFIGURATION" end -end \ No newline at end of file +end From 2b4d8fbd63ebc40111e5717b8f800a9a566d72b2 Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Thu, 22 Jul 2021 11:01:51 +0100 Subject: [PATCH 12/12] Reorganise cuprite setup code --- ...ils_system_tests.rb => cuprite_helpers.rb} | 35 ++++++++----------- spec/system/support/cuprite_setup.rb | 26 +++++++------- 2 files changed, 27 insertions(+), 34 deletions(-) rename spec/system/support/{better_rails_system_tests.rb => cuprite_helpers.rb} (51%) diff --git a/spec/system/support/better_rails_system_tests.rb b/spec/system/support/cuprite_helpers.rb similarity index 51% rename from spec/system/support/better_rails_system_tests.rb rename to spec/system/support/cuprite_helpers.rb index e717fd1a23..0ec2d66335 100644 --- a/spec/system/support/better_rails_system_tests.rb +++ b/spec/system/support/cuprite_helpers.rb @@ -1,4 +1,18 @@ -module BetterRailsSystemTests +# frozen_string_literal: true + +module CupriteHelpers + # Drop #pause anywhere in a test to stop the execution. + # Useful when you want to checkout the contents of a web page in the middle of a test + # running in a headful mode. + def pause + page.driver.pause + end + + # Drop #debug anywhere in a test to open a Chrome inspector and pause the execution + def debug(*args) + page.driver.debug(*args) + end + # Use our `Capybara.save_path` to store screenshots with other capybara artifacts # (Rails screenshots path is not configurable https://github.com/rails/rails/blob/49baf092439fc74fc3377b12e3334c3dd9d0752f/actionpack/lib/action_dispatch/system_testing/test_helpers/screenshot_helper.rb#L79) def absolute_image_path @@ -13,22 +27,3 @@ module BetterRailsSystemTests Capybara.using_session(Capybara.last_used_session) { super } end end - -RSpec.configure do |config| - config.include BetterRailsSystemTests, type: :system - - # Make urls in mailers contain the correct server host. - # It's required for testing links in emails (e.g., via capybara-email). - config.around(:each, type: :system) do |ex| - was_host = Rails.application.default_url_options[:host] - Rails.application.default_url_options[:host] = Capybara.server_host - ex.run - Rails.application.default_url_options[:host] = was_host - end - - # Make sure this hook runs before others - config.prepend_before(:each, type: :system) do - # Use JS driver always - driven_by Capybara.javascript_driver - end -end diff --git a/spec/system/support/cuprite_setup.rb b/spec/system/support/cuprite_setup.rb index 315d3ca32c..8bc258f0b2 100644 --- a/spec/system/support/cuprite_setup.rb +++ b/spec/system/support/cuprite_setup.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "capybara/cuprite" Capybara.register_driver(:cuprite) do |app| @@ -19,20 +21,16 @@ end # Configure Capybara to use :cuprite driver by default Capybara.default_driver = Capybara.javascript_driver = :cuprite -module CupriteHelpers - # Drop #pause anywhere in a test to stop the execution. - # Useful when you want to checkout the contents of a web page in the middle of a test - # running in a headful mode. - def pause - page.driver.pause - end - - # Drop #debug anywhere in a test to open a Chrome inspector and pause the execution - def debug(*args) - page.driver.debug(*args) - end -end - RSpec.configure do |config| config.include CupriteHelpers, type: :system + + config.prepend_before(:each, type: :system) { driven_by :cuprite } + + # Make sure url helpers in mailers use the Capybara server host. + config.around(:each, type: :system) do |example| + original_host = Rails.application.default_url_options[:host] + Rails.application.default_url_options[:host] = Capybara.server_host + example.run + Rails.application.default_url_options[:host] = original_host + end end