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 1/6] 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 2/6] 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 3/6] 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 4/6] 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 5/6] 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 6/6] 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