debian-mirror-gitlab/spec/support/capybara.rb

223 lines
8.1 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
# rubocop:disable Style/GlobalVars
2015-09-11 14:41:01 +05:30
require 'capybara/rails'
require 'capybara/rspec'
2017-08-17 22:00:37 +05:30
require 'capybara-screenshot/rspec'
2018-03-17 18:26:18 +05:30
require 'selenium-webdriver'
2015-09-11 14:41:01 +05:30
# Give CI some extra time
2020-05-24 23:13:21 +05:30
timeout = ENV['CI'] || ENV['CI_SERVER'] ? 60 : 30
2015-09-11 14:41:01 +05:30
2020-07-28 23:09:34 +05:30
# Support running Capybara on a specific port to allow saving commonly used pages
Capybara.server_port = ENV['CAPYBARA_PORT'] if ENV['CAPYBARA_PORT']
2018-10-15 14:42:47 +05:30
# Define an error class for JS console messages
JSConsoleError = Class.new(StandardError)
# Filter out innocuous JS console messages
JS_CONSOLE_FILTER = Regexp.union([
'"[HMR] Waiting for update signal from WDS..."',
'"[WDS] Hot Module Replacement enabled."',
2020-01-01 13:55:28 +05:30
'"[WDS] Live Reloading enabled."',
2020-04-08 14:13:33 +05:30
'Download the Vue Devtools extension',
2021-09-04 01:27:46 +05:30
'Download the Apollo DevTools',
"Unrecognized feature: 'interest-cohort'"
2018-10-15 14:42:47 +05:30
])
2019-07-31 22:56:46 +05:30
CAPYBARA_WINDOW_SIZE = [1366, 768].freeze
2020-04-08 14:13:33 +05:30
# Run Workhorse on the given host and port, proxying to Puma on a UNIX socket,
# for a closer-to-production experience
Capybara.register_server :puma_via_workhorse do |app, port, host, **options|
file = Tempfile.new
socket_path = file.path
file.close! # We just want the filename
2021-03-08 18:12:59 +05:30
TestEnv.with_workhorse(host, port, socket_path) do
2020-04-08 14:13:33 +05:30
Capybara.servers[:puma].call(app, nil, socket_path, **options)
end
end
2018-03-17 18:26:18 +05:30
Capybara.register_driver :chrome do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
# This enables access to logs with `page.driver.manage.get_log(:browser)`
loggingPrefs: {
browser: "ALL",
client: "ALL",
driver: "ALL",
server: "ALL"
}
)
options = Selenium::WebDriver::Chrome::Options.new
2021-01-03 14:25:43 +05:30
# Force the browser's scale factor to prevent inconsistencies on high-res devices
options.add_argument('--force-device-scale-factor=1')
2019-07-31 22:56:46 +05:30
options.add_argument("window-size=#{CAPYBARA_WINDOW_SIZE.join(',')}")
2018-03-17 18:26:18 +05:30
# Chrome won't work properly in a Docker container in sandbox mode
options.add_argument("no-sandbox")
2021-09-30 23:02:18 +05:30
# Run headless by default unless WEBDRIVER_HEADLESS specified
options.add_argument("headless") unless ENV['WEBDRIVER_HEADLESS'] =~ /^(false|no|0)$/i || ENV['CHROME_HEADLESS'] =~ /^(false|no|0)$/i
2018-03-17 18:26:18 +05:30
2019-12-04 20:38:33 +05:30
# Disable /dev/shm use in CI. See https://gitlab.com/gitlab-org/gitlab/issues/4252
2018-03-17 18:26:18 +05:30
options.add_argument("disable-dev-shm-usage") if ENV['CI'] || ENV['CI_SERVER']
2019-12-04 20:38:33 +05:30
# Explicitly set user-data-dir to prevent crashes. See https://gitlab.com/gitlab-org/gitlab-foss/issues/58882#note_179811508
2019-09-04 21:01:54 +05:30
options.add_argument("user-data-dir=/tmp/chrome") if ENV['CI'] || ENV['CI_SERVER']
2019-12-04 20:38:33 +05:30
# Chrome 75 defaults to W3C mode which doesn't allow console log access
options.add_option(:w3c, false)
2018-03-17 18:26:18 +05:30
Capybara::Selenium::Driver.new(
2017-08-17 22:00:37 +05:30
app,
2018-03-17 18:26:18 +05:30
browser: :chrome,
desired_capabilities: capabilities,
options: options
2017-08-17 22:00:37 +05:30
)
2015-09-11 14:41:01 +05:30
end
2021-04-17 20:07:23 +05:30
Capybara.register_driver :firefox do |app|
capabilities = Selenium::WebDriver::Remote::Capabilities.firefox(
log: {
level: :trace
}
)
options = Selenium::WebDriver::Firefox::Options.new(log_level: :trace)
options.add_argument("--window-size=#{CAPYBARA_WINDOW_SIZE.join(',')}")
# Run headless by default unless WEBDRIVER_HEADLESS specified
options.add_argument("--headless") unless ENV['WEBDRIVER_HEADLESS'] =~ /^(false|no|0)$/i
Capybara::Selenium::Driver.new(
app,
browser: :firefox,
desired_capabilities: capabilities,
options: options
)
end
2020-04-08 14:13:33 +05:30
Capybara.server = :puma_via_workhorse
2021-04-17 20:07:23 +05:30
Capybara.javascript_driver = ENV.fetch('WEBDRIVER', :chrome).to_sym
2016-06-02 11:05:42 +05:30
Capybara.default_max_wait_time = timeout
2015-09-11 14:41:01 +05:30
Capybara.ignore_hidden_elements = true
2019-09-04 21:01:54 +05:30
Capybara.default_normalize_ws = true
2019-10-12 21:52:04 +05:30
Capybara.enable_aria_label = true
2015-09-11 14:41:01 +05:30
2020-04-08 14:13:33 +05:30
Capybara::Screenshot.append_timestamp = false
Capybara::Screenshot.register_filename_prefix_formatter(:rspec) do |example|
example.full_description.downcase.parameterize(separator: "_")[0..99]
end
2017-08-17 22:00:37 +05:30
# Keep only the screenshots generated from the last failing test suite
Capybara::Screenshot.prune_strategy = :keep_last_run
2018-03-17 18:26:18 +05:30
# From https://github.com/mattheworiordan/capybara-screenshot/issues/84#issuecomment-41219326
Capybara::Screenshot.register_driver(:chrome) do |driver, path|
driver.browser.save_screenshot(path)
end
2016-04-02 18:10:28 +05:30
RSpec.configure do |config|
2018-10-15 14:42:47 +05:30
config.include CapybaraHelpers, type: :feature
2017-08-17 22:00:37 +05:30
config.before(:context, :js) do
2020-05-24 23:13:21 +05:30
# This prevents Selenium from creating thousands of connections while waiting for
# an element to appear
webmock_enable_with_http_connect_on_start!
2017-08-17 22:00:37 +05:30
next if $capybara_server_already_started
TestEnv.eager_load_driver_server
$capybara_server_already_started = true
2016-04-02 18:10:28 +05:30
end
2017-09-10 17:25:29 +05:30
2020-05-24 23:13:21 +05:30
config.after(:context, :js) do
# WebMock doesn't stub connections, so we need to restore the original behavior
# to prevent many specs from failing:
# https://github.com/bblimke/webmock/blob/master/README.md#connecting-on-nethttpstart
webmock_enable!
end
2017-09-10 17:25:29 +05:30
config.before(:example, :js) do
2018-03-17 18:26:18 +05:30
session = Capybara.current_session
2017-09-10 17:25:29 +05:30
allow(Gitlab::Application.routes).to receive(:default_url_options).and_return(
2018-03-17 18:26:18 +05:30
host: session.server.host,
port: session.server.port,
2017-09-10 17:25:29 +05:30
protocol: 'http')
2018-03-17 18:26:18 +05:30
2021-01-03 14:25:43 +05:30
# CSRF protection is disabled by default. We only enable this for JS specs because some forms
# require Javascript to set the CSRF token.
allow_any_instance_of(ActionController::Base).to receive(:protect_against_forgery?).and_return(true)
2018-03-17 18:26:18 +05:30
# reset window size between tests
2019-07-31 22:56:46 +05:30
unless session.current_window.size == CAPYBARA_WINDOW_SIZE
begin
session.current_window.resize_to(*CAPYBARA_WINDOW_SIZE)
2021-06-08 01:23:25 +05:30
rescue StandardError # ?
2019-07-31 22:56:46 +05:30
end
2018-03-17 18:26:18 +05:30
end
2017-09-10 17:25:29 +05:30
end
2020-04-08 14:13:33 +05:30
# The :capybara_ignore_server_errors metadata means unhandled exceptions raised
# by the application under test will not necessarily fail the server. This is
# useful when testing conditions that are expected to raise a 500 error in
# production; it should not be used on the happy path.
config.around(:each, :capybara_ignore_server_errors) do |example|
Capybara.raise_server_errors = false
example.run
2021-06-08 01:23:25 +05:30
ensure
Capybara.raise_server_errors = true
end
2020-04-08 14:13:33 +05:30
2021-06-08 01:23:25 +05:30
config.append_after do |example|
2020-04-08 14:13:33 +05:30
if example.metadata[:screenshot]
screenshot = example.metadata[:screenshot][:image] || example.metadata[:screenshot][:html]
2021-06-08 01:23:25 +05:30
screenshot&.delete_prefix!(ENV.fetch('CI_PROJECT_DIR', ''))
2020-04-08 14:13:33 +05:30
example.metadata[:stdout] = %{[[ATTACHMENT|#{screenshot}]]}
end
end
2017-09-10 17:25:29 +05:30
config.after(:example, :js) do |example|
2018-10-15 14:42:47 +05:30
# when a test fails, display any messages in the browser's console
2019-12-26 22:10:19 +05:30
# but fail don't add the message if the failure is a pending test that got
# fixed. If we raised the `JSException` the fixed test would be marked as
# failed again.
if example.exception && !example.exception.is_a?(RSpec::Core::Pending::PendingExampleFixedError)
2020-07-28 23:09:34 +05:30
begin
console = page.driver.browser.manage.logs.get(:browser)&.reject { |log| log.message =~ JS_CONSOLE_FILTER }
if console.present?
message = "Unexpected browser console output:\n" + console.map(&:message).join("\n")
raise JSConsoleError, message
end
rescue Selenium::WebDriver::Error::WebDriverError => error
2021-09-30 23:02:18 +05:30
if error.message =~ %r{unknown command: session/[0-9a-zA-Z]+(?:/se)?/log}
2020-07-28 23:09:34 +05:30
message = "Unable to access Chrome javascript console logs. You may be using an outdated version of ChromeDriver."
raise JSConsoleError, message
else
raise error
end
2018-10-15 14:42:47 +05:30
end
end
2018-03-27 19:54:05 +05:30
# prevent localStorage from introducing side effects based on test order
unless ['', 'about:blank', 'data:,'].include? Capybara.current_session.driver.browser.current_url
execute_script("localStorage.clear();")
end
2018-03-17 18:26:18 +05:30
2017-09-10 17:25:29 +05:30
# capybara/rspec already calls Capybara.reset_sessions! in an `after` hook,
# but `block_and_wait_for_requests_complete` is called before it so by
2018-12-13 13:39:08 +05:30
# calling it explicitly here, we prevent any new requests from being fired
2017-09-10 17:25:29 +05:30
# See https://github.com/teamcapybara/capybara/blob/ffb41cfad620de1961bb49b1562a9fa9b28c0903/lib/capybara/rspec.rb#L20-L25
# We don't reset the session when the example failed, because we need capybara-screenshot to have access to it.
Capybara.reset_sessions! unless example.exception
block_and_wait_for_requests_complete
end
2016-04-02 18:10:28 +05:30
end