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
159 class ActionDispatch::IntegrationTest
160 # Make the Capybara DSL available in all integration tests
161 include Capybara::DSL
162 include ApiFixtureLoader
164 include AssertDomEvent
165 include HeadlessHelper
167 @@API_AUTHS = self.api_fixture('api_client_authorizations')
169 def page_with_token(token, path='/')
170 # Generate a page path with an embedded API token.
171 # Typical usage: visit page_with_token('token_name', page)
172 # The token can be specified by the name of an api_client_authorizations
173 # fixture, or passed as a raw string.
174 api_token = ((@@API_AUTHS.include? token) ?
175 @@API_AUTHS[token]['api_token'] : token)
176 path_parts = path.partition("#")
177 sep = (path_parts.first.include? '?') ? '&' : '?'
178 q_string = URI.encode_www_form('api_token' => api_token)
179 path_parts.insert(1, "#{sep}#{q_string}")
183 # Find a page element, but return false instead of raising an
184 # exception if not found. Use this with assertions to explain that
185 # the error signifies a failed test rather than an unexpected error
186 # during a testing procedure.
190 rescue Capybara::ElementNotFound
195 @@screenshot_count = 1
197 image_file = "./tmp/workbench-fail-#{@@screenshot_count}.png"
199 page.save_screenshot image_file
200 rescue Capybara::NotSupportedByDriverError
203 puts "Saved #{image_file}"
204 @@screenshot_count += 1
209 if !passed? && !skipped?
212 if Capybara.current_driver == :selenium
213 # Clearing localStorage crashes on a page where JS isn't
214 # executed. We also need to make sure we're clearing
215 # localStorage for the test server's origin, even if we finished
216 # the test on a different origin.
217 host = Capybara.current_session.server.host
218 port = Capybara.current_session.server.port
219 base = "http://#{host}:#{port}"
220 if page.evaluate_script("window.document.contentType") != "text/html" ||
221 !page.evaluate_script("window.location.toString()").start_with?(base)
224 page.execute_script("window.localStorage.clear()")
226 page.driver.restart if defined?(page.driver.restart)
228 Capybara.reset_sessions!
232 if Capybara.current_driver == :selenium
235 page.driver.browser.switch_to.alert.accept
237 rescue Selenium::WebDriver::Error::NoSuchAlertError
242 # poltergeist returns true for confirm, so no need to accept
247 def upload_data_and_get_collection(data, user, filename, owner_uuid=nil)
248 token = api_token(user)
249 datablock = `echo -n #{data.shellescape} | ARVADOS_API_TOKEN=#{token.shellescape} arv-put --no-progress --raw -`.strip
250 assert $?.success?, $?
253 mtxt = ". #{datablock} 0:#{data.length}:#{filename}\n"
255 col = Collection.create(manifest_text: mtxt, owner_uuid: owner_uuid)
257 col = Collection.create(manifest_text: mtxt)