2019-07-25 01:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-31 03:55:46 -04:00
|
|
|
# rubocop:disable Style/GlobalVars
|
2015-04-25 14:33:04 -04:00
|
|
|
require 'capybara/rails'
|
|
|
|
require 'capybara/rspec'
|
2016-12-07 22:45:34 -05:00
|
|
|
require 'capybara-screenshot/rspec'
|
2017-06-17 01:07:14 -04:00
|
|
|
require 'selenium-webdriver'
|
2015-04-25 14:33:04 -04:00
|
|
|
|
|
|
|
# Give CI some extra time
|
2020-04-22 17:10:00 -04:00
|
|
|
timeout = ENV['CI'] || ENV['CI_SERVER'] ? 60 : 30
|
2015-04-25 14:33:04 -04:00
|
|
|
|
2018-04-12 18:32:20 -04:00
|
|
|
# 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."',
|
2019-11-26 22:06:19 -05:00
|
|
|
'"[WDS] Live Reloading enabled."',
|
2020-02-20 13:08:51 -05:00
|
|
|
'Download the Vue Devtools extension',
|
|
|
|
'Download the Apollo DevTools'
|
2018-04-12 18:32:20 -04:00
|
|
|
])
|
|
|
|
|
2019-04-15 12:19:10 -04:00
|
|
|
CAPYBARA_WINDOW_SIZE = [1366, 768].freeze
|
|
|
|
|
2020-03-10 14:08:17 -04:00
|
|
|
# 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
|
|
|
|
|
|
|
|
TestEnv.with_workhorse(TestEnv.workhorse_dir, host, port, socket_path) do
|
|
|
|
Capybara.servers[:puma].call(app, nil, socket_path, **options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-17 01:07:14 -04:00
|
|
|
Capybara.register_driver :chrome do |app|
|
|
|
|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
|
2017-11-29 00:47:38 -05:00
|
|
|
# This enables access to logs with `page.driver.manage.get_log(:browser)`
|
|
|
|
loggingPrefs: {
|
|
|
|
browser: "ALL",
|
|
|
|
client: "ALL",
|
|
|
|
driver: "ALL",
|
|
|
|
server: "ALL"
|
2017-06-17 01:07:14 -04:00
|
|
|
}
|
2016-11-28 03:50:08 -05:00
|
|
|
)
|
2017-06-17 01:07:14 -04:00
|
|
|
|
2017-11-29 00:47:38 -05:00
|
|
|
options = Selenium::WebDriver::Chrome::Options.new
|
2019-04-15 12:19:10 -04:00
|
|
|
options.add_argument("window-size=#{CAPYBARA_WINDOW_SIZE.join(',')}")
|
2017-11-29 00:47:38 -05:00
|
|
|
|
|
|
|
# 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-04-13 02:30:08 -04:00
|
|
|
options.add_argument("headless") unless ENV['CHROME_HEADLESS'] =~ /^(false|no|0)$/i
|
2017-11-29 00:47:38 -05:00
|
|
|
|
2019-09-18 10:02:45 -04:00
|
|
|
# Disable /dev/shm use in CI. See https://gitlab.com/gitlab-org/gitlab/issues/4252
|
2017-12-22 02:40:56 -05:00
|
|
|
options.add_argument("disable-dev-shm-usage") if ENV['CI'] || ENV['CI_SERVER']
|
|
|
|
|
2019-09-18 10:02:45 -04:00
|
|
|
# Explicitly set user-data-dir to prevent crashes. See https://gitlab.com/gitlab-org/gitlab-foss/issues/58882#note_179811508
|
2019-06-11 00:17:50 -04:00
|
|
|
options.add_argument("user-data-dir=/tmp/chrome") if ENV['CI'] || ENV['CI_SERVER']
|
|
|
|
|
2019-08-14 04:12:05 -04:00
|
|
|
# Chrome 75 defaults to W3C mode which doesn't allow console log access
|
|
|
|
options.add_option(:w3c, false)
|
|
|
|
|
2017-11-29 00:47:38 -05:00
|
|
|
Capybara::Selenium::Driver.new(
|
|
|
|
app,
|
|
|
|
browser: :chrome,
|
|
|
|
desired_capabilities: capabilities,
|
|
|
|
options: options
|
|
|
|
)
|
2015-04-25 14:33:04 -04:00
|
|
|
end
|
|
|
|
|
2020-03-10 14:08:17 -04:00
|
|
|
Capybara.server = :puma_via_workhorse
|
2017-11-29 00:47:38 -05:00
|
|
|
Capybara.javascript_driver = :chrome
|
2016-03-15 14:03:17 -04:00
|
|
|
Capybara.default_max_wait_time = timeout
|
2015-04-25 14:33:04 -04:00
|
|
|
Capybara.ignore_hidden_elements = true
|
2019-04-26 11:51:05 -04:00
|
|
|
Capybara.default_normalize_ws = true
|
2019-07-15 09:16:55 -04:00
|
|
|
Capybara.enable_aria_label = true
|
2015-04-25 14:33:04 -04:00
|
|
|
|
2020-03-10 23:09:31 -04:00
|
|
|
Capybara::Screenshot.append_timestamp = false
|
|
|
|
|
|
|
|
Capybara::Screenshot.register_filename_prefix_formatter(:rspec) do |example|
|
2020-03-11 11:09:37 -04:00
|
|
|
example.full_description.downcase.parameterize(separator: "_")[0..99]
|
2020-03-10 23:09:31 -04:00
|
|
|
end
|
2016-12-07 22:45:34 -05:00
|
|
|
# Keep only the screenshots generated from the last failing test suite
|
|
|
|
Capybara::Screenshot.prune_strategy = :keep_last_run
|
2017-06-17 01:35:46 -04:00
|
|
|
# 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-01-28 20:45:03 -05:00
|
|
|
|
|
|
|
RSpec.configure do |config|
|
2018-03-29 05:47:54 -04:00
|
|
|
config.include CapybaraHelpers, type: :feature
|
|
|
|
|
2017-03-31 03:55:46 -04:00
|
|
|
config.before(:context, :js) do
|
2020-04-28 08:09:44 -04:00
|
|
|
# This prevents Selenium from creating thousands of connections while waiting for
|
|
|
|
# an element to appear
|
|
|
|
webmock_enable_with_http_connect_on_start!
|
|
|
|
|
2017-03-31 03:55:46 -04:00
|
|
|
next if $capybara_server_already_started
|
|
|
|
|
2017-03-30 10:30:05 -04:00
|
|
|
TestEnv.eager_load_driver_server
|
2017-03-31 03:55:46 -04:00
|
|
|
$capybara_server_already_started = true
|
2016-01-28 20:45:03 -05:00
|
|
|
end
|
2017-06-16 11:23:49 -04:00
|
|
|
|
2020-04-28 08:09:44 -04:00
|
|
|
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-07-27 05:15:57 -04:00
|
|
|
config.before(:example, :js) do
|
2017-11-01 15:04:24 -04:00
|
|
|
session = Capybara.current_session
|
|
|
|
|
2017-07-27 05:15:57 -04:00
|
|
|
allow(Gitlab::Application.routes).to receive(:default_url_options).and_return(
|
2017-11-01 15:04:24 -04:00
|
|
|
host: session.server.host,
|
|
|
|
port: session.server.port,
|
2017-07-27 05:15:57 -04:00
|
|
|
protocol: 'http')
|
|
|
|
|
2017-11-01 12:01:33 -04:00
|
|
|
# reset window size between tests
|
2019-04-15 12:19:10 -04:00
|
|
|
unless session.current_window.size == CAPYBARA_WINDOW_SIZE
|
|
|
|
begin
|
|
|
|
session.current_window.resize_to(*CAPYBARA_WINDOW_SIZE)
|
|
|
|
rescue # ?
|
|
|
|
end
|
2017-11-01 15:04:24 -04:00
|
|
|
end
|
2017-11-01 13:17:13 -04:00
|
|
|
end
|
2017-11-01 12:01:33 -04:00
|
|
|
|
2020-03-10 14:08:17 -04:00
|
|
|
# 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
|
2020-03-17 02:09:21 -04:00
|
|
|
|
|
|
|
if example.metadata[:screenshot]
|
|
|
|
screenshot = example.metadata[:screenshot][:image] || example.metadata[:screenshot][:html]
|
|
|
|
example.metadata[:stdout] = %{[[ATTACHMENT|#{screenshot}]]}
|
|
|
|
end
|
|
|
|
|
2020-03-10 14:08:17 -04:00
|
|
|
ensure
|
|
|
|
Capybara.raise_server_errors = true
|
|
|
|
end
|
|
|
|
|
2017-11-01 13:17:13 -04:00
|
|
|
config.after(:example, :js) do |example|
|
2018-04-12 18:32:20 -04:00
|
|
|
# when a test fails, display any messages in the browser's console
|
2019-11-10 19:06:26 -05:00
|
|
|
# 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-04-13 11:50:01 -04:00
|
|
|
console = page.driver.browser.manage.logs.get(:browser)&.reject { |log| log.message =~ JS_CONSOLE_FILTER }
|
2019-11-10 19:06:26 -05:00
|
|
|
|
2018-04-12 18:32:20 -04:00
|
|
|
if console.present?
|
|
|
|
message = "Unexpected browser console output:\n" + console.map(&:message).join("\n")
|
|
|
|
raise JSConsoleError, message
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-23 16:50:53 -05:00
|
|
|
# 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
|
2017-10-20 02:05:18 -04:00
|
|
|
|
2017-06-16 11:23:49 -04:00
|
|
|
# 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-10-30 06:53:01 -04:00
|
|
|
# calling it explicitly here, we prevent any new requests from being fired
|
2017-06-16 11:23:49 -04:00
|
|
|
# See https://github.com/teamcapybara/capybara/blob/ffb41cfad620de1961bb49b1562a9fa9b28c0903/lib/capybara/rspec.rb#L20-L25
|
2017-06-21 22:38:16 -04:00
|
|
|
# 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
|
2017-06-16 11:23:49 -04:00
|
|
|
block_and_wait_for_requests_complete
|
|
|
|
end
|
2016-01-28 20:45:03 -05:00
|
|
|
end
|