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}"
169 class ActionDispatch::IntegrationTest
170 # Make the Capybara DSL available in all integration tests
171 include Capybara::DSL
172 include ApiFixtureLoader
174 include AssertDomEvent
175 include HeadlessHelper
177 @@API_AUTHS = self.api_fixture('api_client_authorizations')
179 def page_with_token(token, path='/')
180 # Generate a page path with an embedded API token.
181 # Typical usage: visit page_with_token('token_name', page)
182 # The token can be specified by the name of an api_client_authorizations
183 # fixture, or passed as a raw string.
184 api_token = ((@@API_AUTHS.include? token) ?
185 @@API_AUTHS[token]['api_token'] : token)
186 path_parts = path.partition("#")
187 sep = (path_parts.first.include? '?') ? '&' : '?'
188 q_string = URI.encode_www_form('api_token' => api_token)
189 path_parts.insert(1, "#{sep}#{q_string}")
193 # Find a page element, but return false instead of raising an
194 # exception if not found. Use this with assertions to explain that
195 # the error signifies a failed test rather than an unexpected error
196 # during a testing procedure.
200 rescue Capybara::ElementNotFound
205 @@screenshot_count = 1
207 image_file = "./tmp/workbench-fail-#{@@screenshot_count}.png"
209 page.save_screenshot image_file
210 rescue Capybara::NotSupportedByDriverError
213 puts "Saved #{image_file}"
214 @@screenshot_count += 1
222 if Capybara.current_driver == :selenium
223 page.execute_script("window.localStorage.clear()")
225 Capybara.reset_sessions!
229 if Capybara.current_driver == :selenium
232 page.driver.browser.switch_to.alert.accept
234 rescue Selenium::WebDriver::Error::NoSuchAlertError
239 # poltergeist returns true for confirm, so no need to accept
244 def upload_data_and_get_collection(data, user, filename, owner_uuid=nil)
245 token = api_fixture('api_client_authorizations')[user]['api_token']
246 datablock = `echo -n #{data.shellescape} | ARVADOS_API_TOKEN=#{token.shellescape} arv-put --no-progress --raw -`.strip
247 assert $?.success?, $?
250 mtxt = ". #{datablock} 0:#{data.length}:#{filename}\n"
252 col = Collection.create(manifest_text: mtxt, owner_uuid: owner_uuid)
254 col = Collection.create(manifest_text: mtxt)