From 161dd525ac3fad5bac179bbda8dd2db1f6b8c9ba Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Fri, 12 Feb 2021 14:57:45 +0100 Subject: [PATCH] Maintenance: Add support for visit-ing external URLs in Capybara system tests. --- spec/support/capybara/common_actions.rb | 29 +++++++++++++++++++++---- spec/support/capybara/config.rb | 1 + 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/spec/support/capybara/common_actions.rb b/spec/support/capybara/common_actions.rb index 8b2d0a91b..be3c40461 100644 --- a/spec/support/capybara/common_actions.rb +++ b/spec/support/capybara/common_actions.rb @@ -90,26 +90,47 @@ module CommonActions visit('logout') end - # Overwrites the Capybara::Session#visit method to allow SPA navigation. + # Overwrites the Capybara::Session#visit method to allow SPA navigation + # and visiting of external URLs. # All routes not starting with `/` will be handled as SPA routes. + # All routes containing `://` will be handled as an external URL. # # @see Capybara::Session#visit # # @example # visit('logout') - # => visited SPA route '/#logout' + # => visited SPA route 'localhost:32435/#logout' # # @example # visit('/test/ui') - # => visited regular route '/test/ui' + # => visited regular route 'localhost:32435/test/ui' + # + # @example + # visit('https://zammad.org') + # => visited external URL 'https://zammad.org' # def visit(route) - if !route.start_with?('/') + if route.include?('://') + return without_port do + super(route) + end + elsif !route.start_with?('/') route = "/##{route}" end super(route) end + # Overwrites the global Capybara.always_include_port setting (true) + # with false. This comes in handy when visiting external pages. + # + def without_port + original = Capybara.current_session.config.always_include_port + Capybara.current_session.config.always_include_port = false + yield + ensure + Capybara.current_session.config.always_include_port = original + end + # This method is equivalent to Capybara::RSpecMatchers#have_current_path # but checks the SPA route instead of the actual path. # diff --git a/spec/support/capybara/config.rb b/spec/support/capybara/config.rb index 959b9f525..34ad5c473 100644 --- a/spec/support/capybara/config.rb +++ b/spec/support/capybara/config.rb @@ -1,4 +1,5 @@ Capybara.configure do |config| + config.threadsafe = true config.always_include_port = true config.default_max_wait_time = 16 end