2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-03-04 22:52:19 +01:00
|
|
|
require 'capybara/rspec'
|
|
|
|
require 'capybara-screenshot/rspec'
|
|
|
|
require 'capybara/email/rspec'
|
|
|
|
require 'selenium/webdriver'
|
|
|
|
|
2022-10-27 22:23:25 +02:00
|
|
|
def setup_driver(app, download_path, options)
|
|
|
|
Capybara::Selenium::Driver.new(app, browser: :chrome, options:).tap do |driver|
|
|
|
|
if ENV['MAKE_IT_SLOW'].present?
|
|
|
|
driver.browser.network_conditions = {
|
|
|
|
offline: false,
|
|
|
|
latency: 800,
|
|
|
|
download_throughput: 1024000,
|
|
|
|
upload_throughput: 1024000
|
|
|
|
}
|
|
|
|
end
|
2022-10-28 11:13:09 +02:00
|
|
|
|
|
|
|
if ENV['JS_LOG'].present?
|
|
|
|
driver.browser.on_log_event(:console) do |event|
|
2024-06-24 15:27:34 +02:00
|
|
|
puts event.args.join(" ") if event.type.in? ENV['JS_LOG'].downcase.split(',').map(&:to_sym)
|
2022-10-28 11:13:09 +02:00
|
|
|
end
|
|
|
|
end
|
2022-10-27 22:23:25 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-04 22:52:19 +01:00
|
|
|
Capybara.register_driver :chrome do |app|
|
2022-10-12 13:02:48 +02:00
|
|
|
options = Selenium::WebDriver::Chrome::Options.new
|
|
|
|
options.add_argument('--no-sandbox') unless ENV['SANDBOX']
|
|
|
|
options.add_argument('--mute-audio')
|
2022-10-14 13:52:05 +02:00
|
|
|
options.add_argument('--window-size=1440,900')
|
2022-10-12 13:02:48 +02:00
|
|
|
|
|
|
|
download_path = Capybara.save_path
|
|
|
|
# Chromedriver 77 requires setting this for headless mode on linux
|
|
|
|
# Different versions of Chrome/selenium-webdriver require setting differently - just set them all
|
|
|
|
options.add_preference('download.default_directory', download_path)
|
|
|
|
options.add_preference(:download, default_directory: download_path)
|
|
|
|
|
2022-10-27 22:23:25 +02:00
|
|
|
setup_driver(app, download_path, options)
|
2021-03-04 22:52:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
Capybara.register_driver :headless_chrome do |app|
|
|
|
|
options = Selenium::WebDriver::Chrome::Options.new
|
2021-03-08 10:24:53 +01:00
|
|
|
options.add_argument('--no-sandbox') unless ENV['SANDBOX']
|
2022-10-12 13:02:48 +02:00
|
|
|
options.add_argument('--headless')
|
2021-03-04 22:52:19 +01:00
|
|
|
options.add_argument('--window-size=1440,900')
|
2022-10-12 13:02:48 +02:00
|
|
|
options.add_argument('--disable-dev-shm-usage')
|
|
|
|
options.add_argument('--disable-software-rasterizer')
|
|
|
|
options.add_argument('--mute-audio')
|
2021-03-04 22:52:19 +01:00
|
|
|
|
|
|
|
download_path = Capybara.save_path
|
|
|
|
# Chromedriver 77 requires setting this for headless mode on linux
|
|
|
|
# Different versions of Chrome/selenium-webdriver require setting differently - just set them all
|
|
|
|
options.add_preference('download.default_directory', download_path)
|
|
|
|
options.add_preference(:download, default_directory: download_path)
|
|
|
|
|
2022-10-27 22:23:25 +02:00
|
|
|
setup_driver(app, download_path, options)
|
2021-03-04 22:52:19 +01:00
|
|
|
end
|
|
|
|
|
2022-11-03 15:26:48 +01:00
|
|
|
Capybara.default_max_wait_time = 4
|
2021-03-04 22:52:19 +01:00
|
|
|
|
2021-10-26 11:44:53 +02:00
|
|
|
Capybara.ignore_hidden_elements = false
|
|
|
|
|
2022-01-05 11:44:49 +01:00
|
|
|
Capybara.enable_aria_label = true
|
|
|
|
|
2022-10-14 13:52:22 +02:00
|
|
|
Capybara.disable_animation = true
|
|
|
|
|
2021-03-04 22:52:19 +01:00
|
|
|
# Save a snapshot of the HTML page when an integration test fails
|
|
|
|
Capybara::Screenshot.autosave_on_failure = true
|
|
|
|
# Keep only the screenshots generated from the last failing test suite
|
|
|
|
Capybara::Screenshot.prune_strategy = :keep_last_run
|
|
|
|
# Tell Capybara::Screenshot how to take screenshots when using the headless_chrome driver
|
|
|
|
Capybara::Screenshot.register_driver :headless_chrome do |driver, path|
|
|
|
|
driver.browser.save_screenshot(path)
|
|
|
|
end
|
2021-07-22 10:31:13 +02:00
|
|
|
|
|
|
|
RSpec.configure do |config|
|
2021-10-26 11:44:53 +02:00
|
|
|
config.before(:each, type: :system) do
|
|
|
|
driven_by :rack_test
|
|
|
|
end
|
|
|
|
|
|
|
|
config.before(:each, type: :system, js: true) do
|
2022-10-12 13:02:48 +02:00
|
|
|
driven_by ENV['NO_HEADLESS'] ? :chrome : :headless_chrome
|
2021-10-26 11:44:53 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Set the user preferred language before Javascript system specs.
|
2021-10-19 15:51:47 +02:00
|
|
|
#
|
2021-10-26 11:44:53 +02:00
|
|
|
# System specs without Javascript run in a Rack stack, and respect the Accept-Language value.
|
2021-10-19 15:51:47 +02:00
|
|
|
# However specs using Javascript are run into a Headless Chrome, which doesn't support setting
|
|
|
|
# the default Accept-Language value reliably.
|
|
|
|
# So instead we set the locale cookie explicitly before each Javascript test.
|
2021-10-26 11:44:53 +02:00
|
|
|
config.before(:each, type: :system, js: true) do
|
2021-10-19 15:51:47 +02:00
|
|
|
visit '/' # Webdriver needs visiting a page before setting the cookie
|
|
|
|
Capybara.current_session.driver.browser.manage.add_cookie(
|
|
|
|
name: :locale,
|
|
|
|
value: Rails.application.config.i18n.default_locale
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-07-22 10:31:13 +02:00
|
|
|
# Examples tagged with :capybara_ignore_server_errors will allow Capybara
|
|
|
|
# to continue when an exception in raised by Rails.
|
|
|
|
# This allows to test for error cases.
|
|
|
|
config.around(:each, :capybara_ignore_server_errors) do |example|
|
|
|
|
Capybara.raise_server_errors = false
|
|
|
|
|
|
|
|
example.run
|
|
|
|
ensure
|
|
|
|
Capybara.raise_server_errors = true
|
|
|
|
end
|
|
|
|
end
|