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

131 lines
4.6 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
2017-08-17 22:00:37 +05:30
timeout = (ENV['CI'] || ENV['CI_SERVER']) ? 60 : 30
2015-09-11 14:41:01 +05:30
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."',
2018-10-15 14:42:47 +05:30
"Download the Vue Devtools extension"
])
2019-07-31 22:56:46 +05:30
CAPYBARA_WINDOW_SIZE = [1366, 768].freeze
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
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")
# Run headless by default unless CHROME_HEADLESS specified
2018-10-15 14:42:47 +05:30
options.add_argument("headless") unless 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
2019-09-04 21:01:54 +05:30
Capybara.server = :webrick
2018-03-17 18:26:18 +05:30
Capybara.javascript_driver = :chrome
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
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
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
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
# 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)
rescue # ?
end
2018-03-17 18:26:18 +05:30
end
2017-09-10 17:25:29 +05:30
end
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)
2018-10-15 14:42:47 +05:30
console = page.driver.browser.manage.logs.get(:browser)&.reject { |log| log.message =~ JS_CONSOLE_FILTER }
2019-12-26 22:10:19 +05:30
2018-10-15 14:42:47 +05:30
if console.present?
message = "Unexpected browser console output:\n" + console.map(&:message).join("\n")
raise JSConsoleError, message
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