Merge pull request #69 from Matt-Yorkley/cuprite-tidyup

Cuprite tidyup
This commit is contained in:
Filipe
2021-07-22 14:30:40 +01:00
committed by GitHub
6 changed files with 49 additions and 70 deletions

View File

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

View File

@@ -1,36 +0,0 @@
# 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
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,9 +1,7 @@
# 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
Capybara.default_max_wait_time = 10
# Normalize whitespaces when using `has_text?` and similar matchers,
# i.e., ignore newlines, trailing spaces, etc.
@@ -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

View File

@@ -0,0 +1,29 @@
# 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
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
end

View File

@@ -1,22 +1,18 @@
# spec/system/support/cuprite_setup.rb
# frozen_string_literal: true
# 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
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,
# Allow running Chrome in a headful mode by setting HEADLESS env
headless: true
}
)
@@ -25,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

View File

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