1 # Copyright (C) The Arvados Authors. All rights reserved.
3 # SPDX-License-Identifier: AGPL-3.0
6 require 'capybara/rails'
7 require 'capybara/poltergeist'
11 def available_port for_what
13 Addrinfo.tcp("0.0.0.0", 0).listen do |srv|
14 port = srv.connect_address.ip_port
15 # Selenium needs an additional locking port, check if it's available
16 # and retry if necessary.
17 if for_what == 'selenium'
18 locking_port = port - 1
19 Addrinfo.tcp("0.0.0.0", locking_port).listen.close
21 STDERR.puts "Using port #{port} for #{for_what}"
24 rescue Errno::EADDRINUSE, Errno::EACCES
31 port: available_port('selenium'),
37 phantomjs_options: ['--ignore-ssl-errors=true'],
38 port: available_port('poltergeist'),
39 window_size: [1200, 800],
43 Capybara.register_driver :poltergeist do |app|
44 Capybara::Poltergeist::Driver.new app, poltergeist_opts
47 Capybara.register_driver :poltergeist_debug do |app|
48 Capybara::Poltergeist::Driver.new app, poltergeist_opts.merge(inspector: true)
51 Capybara.register_driver :poltergeist_with_fake_websocket do |app|
52 js = File.expand_path '../support/fake_websocket.js', __FILE__
53 Capybara::Poltergeist::Driver.new app, poltergeist_opts.merge(extensions: [js])
56 Capybara.register_driver :poltergeist_without_file_api do |app|
57 js = File.expand_path '../support/remove_file_api.js', __FILE__
58 Capybara::Poltergeist::Driver.new app, poltergeist_opts.merge(extensions: [js])
61 Capybara.register_driver :selenium do |app|
62 Capybara::Selenium::Driver.new app, selenium_opts
65 Capybara.register_driver :selenium_with_download do |app|
66 profile = Selenium::WebDriver::Firefox::Profile.new
67 profile['browser.download.dir'] = DownloadHelper.path.to_s
68 profile['browser.download.downloadDir'] = DownloadHelper.path.to_s
69 profile['browser.download.defaultFolder'] = DownloadHelper.path.to_s
70 profile['browser.download.folderList'] = 2 # "save to user-defined location"
71 profile['browser.download.manager.showWhenStarting'] = false
72 profile['browser.helperApps.alwaysAsk.force'] = false
73 profile['browser.helperApps.neverAsk.saveToDisk'] = 'text/plain,application/octet-stream'
74 Capybara::Selenium::Driver.new app, selenium_opts.merge(profile: profile)
78 # FIXME: Huge side effect here
79 # The following line changes the global default Capybara wait time, affecting
80 # every test which follows this one. This should be removed and the failing tests
81 # should have their individual wait times increased, if appropriate, using
82 # the using_wait_time(N) construct to temporarily change the wait time.
83 # Note: the below is especially bad because there are places that increase wait
84 # times using a multiplier e.g. using_wait_time(3 * Capybara.default_max_wait_time)
85 Capybara.default_max_wait_time = 10
89 while page.evaluate_script("jQuery.active").to_i > 0
91 raise "AJAX request took more than #{timeout} seconds" if count > timeout * 10
99 # Yield the supplied block, then wait for an event to arrive at a
101 def assert_triggers_dom_event events, target='body'
102 magic = 'received-dom-event-' + rand(2**30).to_s(36)
103 page.evaluate_script <<eos
104 $('#{target}').one('#{events}', function() {
105 $('body').addClass('#{magic}');
109 assert_selector "body.#{magic}"
110 page.evaluate_script "$('body').removeClass('#{magic}');";
114 module HeadlessHelper
115 class HeadlessSingleton
116 @display = ENV['ARVADOS_TEST_HEADLESS_DISPLAY'] || rand(400)+100
117 STDERR.puts "Using display :#{@display} for headless tests"
119 @headless ||= Headless.new reuse: false, display: @display
123 Capybara.default_driver = :rack_test
125 def self.included base
128 Capybara.use_default_driver
141 def need_selenium reason=nil, driver=:selenium
142 Capybara.current_driver = driver
143 unless ENV['ARVADOS_TEST_HEADFUL'] or @headless
144 @headless = HeadlessSingleton.get
149 def need_javascript reason=nil
150 unless Capybara.current_driver == :selenium
151 Capybara.current_driver = :poltergeist
158 File.read(File.expand_path("../../../../tmp/#{service}.port", __FILE__))
161 def use_keep_web_config
162 @kwport = getport 'keep-web-ssl'
163 @kwdport = getport 'keep-web-dl-ssl'
164 Rails.configuration.keep_web_url = "https://localhost:#{@kwport}/c=%{uuid_or_pdh}"
165 Rails.configuration.keep_web_download_url = "https://localhost:#{@kwdport}/c=%{uuid_or_pdh}"
166 CollectionsController.any_instance.expects(:file_enumerator).never
170 class ActionDispatch::IntegrationTest
171 # Make the Capybara DSL available in all integration tests
172 include Capybara::DSL
173 include ApiFixtureLoader
175 include AssertDomEvent
176 include HeadlessHelper
178 @@API_AUTHS = self.api_fixture('api_client_authorizations')
180 def page_with_token(token, path='/')
181 # Generate a page path with an embedded API token.
182 # Typical usage: visit page_with_token('token_name', page)
183 # The token can be specified by the name of an api_client_authorizations
184 # fixture, or passed as a raw string.
185 api_token = ((@@API_AUTHS.include? token) ?
186 @@API_AUTHS[token]['api_token'] : token)
187 path_parts = path.partition("#")
188 sep = (path_parts.first.include? '?') ? '&' : '?'
189 q_string = URI.encode_www_form('api_token' => api_token)
190 path_parts.insert(1, "#{sep}#{q_string}")
194 # Find a page element, but return false instead of raising an
195 # exception if not found. Use this with assertions to explain that
196 # the error signifies a failed test rather than an unexpected error
197 # during a testing procedure.
201 rescue Capybara::ElementNotFound
206 @@screenshot_count = 1
208 image_file = "./tmp/workbench-fail-#{@@screenshot_count}.png"
210 page.save_screenshot image_file
211 rescue Capybara::NotSupportedByDriverError
214 puts "Saved #{image_file}"
215 @@screenshot_count += 1
223 if Capybara.current_driver == :selenium
224 page.execute_script("window.localStorage.clear()")
226 Capybara.reset_sessions!
230 if Capybara.current_driver == :selenium
233 page.driver.browser.switch_to.alert.accept
235 rescue Selenium::WebDriver::Error::NoSuchAlertError
240 # poltergeist returns true for confirm, so no need to accept