Reorganise cuprite setup code

This commit is contained in:
Matt-Yorkley
2021-07-22 11:01:51 +01:00
parent 1c493c7238
commit 2b4d8fbd63
2 changed files with 27 additions and 34 deletions

View File

@@ -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

View File

@@ -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