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'),
32 desired_capabilities: Selenium::WebDriver::Remote::Capabilities.firefox(
33 acceptInsecureCerts: true,
40 phantomjs_options: ['--ignore-ssl-errors=true'],
41 port: available_port('poltergeist'),
42 window_size: [1200, 800],
46 Capybara.register_driver :poltergeist do |app|
47 Capybara::Poltergeist::Driver.new app, poltergeist_opts
50 Capybara.register_driver :poltergeist_debug do |app|
51 Capybara::Poltergeist::Driver.new app, poltergeist_opts.merge(inspector: true)
54 Capybara.register_driver :poltergeist_with_fake_websocket do |app|
55 js = File.expand_path '../support/fake_websocket.js', __FILE__
56 Capybara::Poltergeist::Driver.new app, poltergeist_opts.merge(extensions: [js])
59 Capybara.register_driver :poltergeist_without_file_api do |app|
60 js = File.expand_path '../support/remove_file_api.js', __FILE__
61 Capybara::Poltergeist::Driver.new app, poltergeist_opts.merge(extensions: [js])
64 Capybara.register_driver :selenium do |app|
65 Capybara::Selenium::Driver.new app, selenium_opts
68 Capybara.register_driver :selenium_with_download do |app|
69 profile = Selenium::WebDriver::Firefox::Profile.new
70 profile['browser.download.dir'] = DownloadHelper.path.to_s
71 profile['browser.download.downloadDir'] = DownloadHelper.path.to_s
72 profile['browser.download.defaultFolder'] = DownloadHelper.path.to_s
73 profile['browser.download.folderList'] = 2 # "save to user-defined location"
74 profile['browser.download.manager.showWhenStarting'] = false
75 profile['browser.helperApps.alwaysAsk.force'] = false
76 profile['browser.helperApps.neverAsk.saveToDisk'] = 'text/plain,application/octet-stream'
77 Capybara::Selenium::Driver.new app, selenium_opts.merge(profile: profile)
81 # FIXME: Huge side effect here
82 # The following line changes the global default Capybara wait time, affecting
83 # every test which follows this one. This should be removed and the failing tests
84 # should have their individual wait times increased, if appropriate, using
85 # the using_wait_time(N) construct to temporarily change the wait time.
86 # Note: the below is especially bad because there are places that increase wait
87 # times using a multiplier e.g. using_wait_time(3 * Capybara.default_max_wait_time)
88 Capybara.default_max_wait_time = 10
92 while page.evaluate_script("jQuery.active").to_i > 0
94 raise "AJAX request took more than #{timeout} seconds" if count > timeout * 10
101 module AssertDomEvent
102 # Yield the supplied block, then wait for an event to arrive at a
104 def assert_triggers_dom_event events, target='body'
105 magic = 'received-dom-event-' + rand(2**30).to_s(36)
106 page.execute_script <<eos
107 $('#{target}').one('#{events}', function() {
108 $('body').addClass('#{magic}');
112 assert_selector "body.#{magic}"
113 page.execute_script "$('body').removeClass('#{magic}');";
117 module HeadlessHelper
118 class HeadlessSingleton
119 @display = ENV['ARVADOS_TEST_HEADLESS_DISPLAY'] || rand(400)+100
120 STDERR.puts "Using display :#{@display} for headless tests"
122 @headless ||= Headless.new reuse: false, display: @display
126 Capybara.default_driver = :rack_test
128 def self.included base
131 Capybara.use_default_driver
144 def need_selenium reason=nil, driver=:selenium
145 Capybara.current_driver = driver
146 unless ENV['ARVADOS_TEST_HEADFUL'] or @headless
147 @headless = HeadlessSingleton.get
152 def need_javascript reason=nil
153 unless Capybara.current_driver == :selenium
154 Capybara.current_driver = :poltergeist
161 File.read(File.expand_path("../../../../tmp/#{service}.port", __FILE__))
164 def use_keep_web_config
165 @kwport = getport 'keep-web-ssl'
166 @kwdport = getport 'keep-web-dl-ssl'
167 Rails.configuration.keep_web_url = "https://localhost:#{@kwport}/c=%{uuid_or_pdh}"
168 Rails.configuration.keep_web_download_url = "https://localhost:#{@kwdport}/c=%{uuid_or_pdh}"
172 class ActionDispatch::IntegrationTest
173 # Make the Capybara DSL available in all integration tests
174 include Capybara::DSL
175 include ApiFixtureLoader
177 include AssertDomEvent
178 include HeadlessHelper
180 @@API_AUTHS = self.api_fixture('api_client_authorizations')
182 def page_with_token(token, path='/')
183 # Generate a page path with an embedded API token.
184 # Typical usage: visit page_with_token('token_name', page)
185 # The token can be specified by the name of an api_client_authorizations
186 # fixture, or passed as a raw string.
187 api_token = ((@@API_AUTHS.include? token) ?
188 @@API_AUTHS[token]['api_token'] : token)
189 path_parts = path.partition("#")
190 sep = (path_parts.first.include? '?') ? '&' : '?'
191 q_string = URI.encode_www_form('api_token' => api_token)
192 path_parts.insert(1, "#{sep}#{q_string}")
196 # Find a page element, but return false instead of raising an
197 # exception if not found. Use this with assertions to explain that
198 # the error signifies a failed test rather than an unexpected error
199 # during a testing procedure.
203 rescue Capybara::ElementNotFound
208 @@screenshot_count = 1
210 image_file = "./tmp/workbench-fail-#{@@screenshot_count}.png"
212 page.save_screenshot image_file
213 rescue Capybara::NotSupportedByDriverError
216 puts "Saved #{image_file}"
217 @@screenshot_count += 1
225 if Capybara.current_driver == :selenium
226 # Clearing localStorage crashes on a page where JS isn't
227 # executed. We also need to make sure we're clearing
228 # localStorage for the test server's origin, even if we finished
229 # the test on a different origin.
230 host = Capybara.current_session.server.host
231 port = Capybara.current_session.server.port
232 base = "http://#{host}:#{port}"
233 if page.evaluate_script("window.document.contentType") != "text/html" ||
234 !page.evaluate_script("window.location.toString()").start_with?(base)
237 page.execute_script("window.localStorage.clear()")
239 page.driver.restart if defined?(page.driver.restart)
241 Capybara.reset_sessions!
245 if Capybara.current_driver == :selenium
248 page.driver.browser.switch_to.alert.accept
250 rescue Selenium::WebDriver::Error::NoSuchAlertError
255 # poltergeist returns true for confirm, so no need to accept
260 def upload_data_and_get_collection(data, user, filename, owner_uuid=nil)
261 token = api_token(user)
262 datablock = `echo -n #{data.shellescape} | ARVADOS_API_TOKEN=#{token.shellescape} arv-put --no-progress --raw -`.strip
263 assert $?.success?, $?
266 mtxt = ". #{datablock} 0:#{data.length}:#{filename}\n"
268 col = Collection.create(manifest_text: mtxt, owner_uuid: owner_uuid)
270 col = Collection.create(manifest_text: mtxt)