2017-03-31 07:55:46 +00:00
|
|
|
# rubocop:disable Style/GlobalVars
|
2015-04-25 18:33:04 +00:00
|
|
|
require 'capybara/rails'
|
|
|
|
require 'capybara/rspec'
|
2016-12-08 03:45:34 +00:00
|
|
|
require 'capybara-screenshot/rspec'
|
2017-06-17 05:07:14 +00:00
|
|
|
require 'selenium-webdriver'
|
2015-04-25 18:33:04 +00:00
|
|
|
|
|
|
|
# Give CI some extra time
|
2017-03-31 07:55:46 +00:00
|
|
|
timeout = (ENV['CI'] || ENV['CI_SERVER']) ? 60 : 30
|
2015-04-25 18:33:04 +00:00
|
|
|
|
2017-06-17 05:07:14 +00:00
|
|
|
Capybara.register_driver :chrome do |app|
|
|
|
|
capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
|
2017-11-29 05:47:38 +00: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 05:07:14 +00:00
|
|
|
}
|
2016-11-28 08:50:08 +00:00
|
|
|
)
|
2017-06-17 05:07:14 +00:00
|
|
|
|
2017-11-29 05:47:38 +00:00
|
|
|
options = Selenium::WebDriver::Chrome::Options.new
|
|
|
|
options.add_argument("window-size=1240,1400")
|
|
|
|
|
|
|
|
# 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
|
|
|
|
unless ENV['CHROME_HEADLESS'] =~ /^(false|no|0)$/i
|
|
|
|
options.add_argument("headless")
|
|
|
|
|
|
|
|
# Chrome documentation says this flag is needed for now
|
|
|
|
# https://developers.google.com/web/updates/2017/04/headless-chrome#cli
|
|
|
|
options.add_argument("disable-gpu")
|
|
|
|
end
|
|
|
|
|
2017-12-22 07:40:56 +00:00
|
|
|
# Disable /dev/shm use in CI. See https://gitlab.com/gitlab-org/gitlab-ee/issues/4252
|
|
|
|
options.add_argument("disable-dev-shm-usage") if ENV['CI'] || ENV['CI_SERVER']
|
|
|
|
|
2017-11-29 05:47:38 +00:00
|
|
|
Capybara::Selenium::Driver.new(
|
|
|
|
app,
|
|
|
|
browser: :chrome,
|
|
|
|
desired_capabilities: capabilities,
|
|
|
|
options: options
|
|
|
|
)
|
2015-04-25 18:33:04 +00:00
|
|
|
end
|
|
|
|
|
2017-11-29 05:47:38 +00:00
|
|
|
Capybara.javascript_driver = :chrome
|
2016-03-15 18:03:17 +00:00
|
|
|
Capybara.default_max_wait_time = timeout
|
2015-04-25 18:33:04 +00:00
|
|
|
Capybara.ignore_hidden_elements = true
|
|
|
|
|
2016-12-08 03:45:34 +00:00
|
|
|
# Keep only the screenshots generated from the last failing test suite
|
|
|
|
Capybara::Screenshot.prune_strategy = :keep_last_run
|
2017-06-17 05:35:46 +00: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-29 01:45:03 +00:00
|
|
|
|
|
|
|
RSpec.configure do |config|
|
2017-03-31 07:55:46 +00:00
|
|
|
config.before(:context, :js) do
|
|
|
|
next if $capybara_server_already_started
|
|
|
|
|
2017-03-30 14:30:05 +00:00
|
|
|
TestEnv.eager_load_driver_server
|
2017-03-31 07:55:46 +00:00
|
|
|
$capybara_server_already_started = true
|
2016-01-29 01:45:03 +00:00
|
|
|
end
|
2017-06-16 15:23:49 +00:00
|
|
|
|
2017-07-27 09:15:57 +00:00
|
|
|
config.before(:example, :js) do
|
2017-11-01 19:04:24 +00:00
|
|
|
session = Capybara.current_session
|
|
|
|
|
2017-07-27 09:15:57 +00:00
|
|
|
allow(Gitlab::Application.routes).to receive(:default_url_options).and_return(
|
2017-11-01 19:04:24 +00:00
|
|
|
host: session.server.host,
|
|
|
|
port: session.server.port,
|
2017-07-27 09:15:57 +00:00
|
|
|
protocol: 'http')
|
|
|
|
|
2017-11-01 16:01:33 +00:00
|
|
|
# reset window size between tests
|
2017-11-01 19:04:24 +00:00
|
|
|
unless session.current_window.size == [1240, 1400]
|
|
|
|
session.current_window.resize_to(1240, 1400) rescue nil
|
|
|
|
end
|
2017-11-01 17:17:13 +00:00
|
|
|
end
|
2017-11-01 16:01:33 +00:00
|
|
|
|
2017-11-01 17:17:13 +00:00
|
|
|
config.after(:example, :js) do |example|
|
2018-02-23 21:50:53 +00: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 06:05:18 +00:00
|
|
|
|
2017-06-16 15:23:49 +00: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
|
|
|
|
# calling it explicitely here, we prevent any new requests from being fired
|
|
|
|
# See https://github.com/teamcapybara/capybara/blob/ffb41cfad620de1961bb49b1562a9fa9b28c0903/lib/capybara/rspec.rb#L20-L25
|
2017-06-22 02:38:16 +00: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 15:23:49 +00:00
|
|
|
block_and_wait_for_requests_complete
|
|
|
|
end
|
2016-01-29 01:45:03 +00:00
|
|
|
end
|