2 require 'capybara/rails'
3 require 'capybara/poltergeist'
7 def available_port for_what
9 Addrinfo.tcp("0.0.0.0", 0).listen do |srv|
10 port = srv.connect_address.ip_port
11 # Selenium needs an additional locking port, check if it's available
12 # and retry if necessary.
13 if for_what == 'selenium'
14 locking_port = port - 1
15 Addrinfo.tcp("0.0.0.0", locking_port).listen.close
17 STDERR.puts "Using port #{port} for #{for_what}"
20 rescue Errno::EADDRINUSE, Errno::EACCES
27 port: available_port('selenium'),
33 phantomjs_options: ['--ignore-ssl-errors=true'],
34 port: available_port('poltergeist'),
35 window_size: [1200, 800],
39 Capybara.register_driver :poltergeist do |app|
40 Capybara::Poltergeist::Driver.new app, poltergeist_opts
43 Capybara.register_driver :poltergeist_debug do |app|
44 Capybara::Poltergeist::Driver.new app, poltergeist_opts.merge(inspector: true)
47 Capybara.register_driver :poltergeist_with_fake_websocket do |app|
48 js = File.expand_path '../support/fake_websocket.js', __FILE__
49 Capybara::Poltergeist::Driver.new app, poltergeist_opts.merge(extensions: [js])
52 Capybara.register_driver :poltergeist_without_file_api do |app|
53 js = File.expand_path '../support/remove_file_api.js', __FILE__
54 Capybara::Poltergeist::Driver.new app, poltergeist_opts.merge(extensions: [js])
57 Capybara.register_driver :selenium do |app|
58 Capybara::Selenium::Driver.new app, selenium_opts
61 Capybara.register_driver :selenium_with_download do |app|
62 profile = Selenium::WebDriver::Firefox::Profile.new
63 profile['browser.download.dir'] = DownloadHelper.path.to_s
64 profile['browser.download.downloadDir'] = DownloadHelper.path.to_s
65 profile['browser.download.defaultFolder'] = DownloadHelper.path.to_s
66 profile['browser.download.folderList'] = 2 # "save to user-defined location"
67 profile['browser.download.manager.showWhenStarting'] = false
68 profile['browser.helperApps.alwaysAsk.force'] = false
69 profile['browser.helperApps.neverAsk.saveToDisk'] = 'text/plain,application/octet-stream'
70 Capybara::Selenium::Driver.new app, selenium_opts.merge(profile: profile)
74 # FIXME: Huge side effect here
75 # The following line changes the global default Capybara wait time, affecting
76 # every test which follows this one. This should be removed and the failing tests
77 # should have their individual wait times increased, if appropriate, using
78 # the using_wait_time(N) construct to temporarily change the wait time.
79 # Note: the below is especially bad because there are places that increase wait
80 # times using a multiplier e.g. using_wait_time(3 * Capybara.default_max_wait_time)
81 Capybara.default_max_wait_time = 10
85 while page.evaluate_script("jQuery.active").to_i > 0
87 raise "AJAX request took more than #{timeout} seconds" if count > timeout * 10
95 # Yield the supplied block, then wait for an event to arrive at a
97 def assert_triggers_dom_event events, target='body'
98 magic = 'received-dom-event-' + rand(2**30).to_s(36)
99 page.evaluate_script <<eos
100 $('#{target}').one('#{events}', function() {
101 $('body').addClass('#{magic}');
105 assert_selector "body.#{magic}"
106 page.evaluate_script "$('body').removeClass('#{magic}');";
110 module HeadlessHelper
111 class HeadlessSingleton
112 @display = ENV['ARVADOS_TEST_HEADLESS_DISPLAY'] || rand(400)+100
113 STDERR.puts "Using display :#{@display} for headless tests"
115 @headless ||= Headless.new reuse: false, display: @display
119 Capybara.default_driver = :rack_test
121 def self.included base
124 Capybara.use_default_driver
137 def need_selenium reason=nil, driver=:selenium
138 Capybara.current_driver = driver
139 unless ENV['ARVADOS_TEST_HEADFUL'] or @headless
140 @headless = HeadlessSingleton.get
145 def need_javascript reason=nil
146 unless Capybara.current_driver == :selenium
147 Capybara.current_driver = :poltergeist
154 File.read(File.expand_path("../../../../tmp/#{service}.port", __FILE__))
157 def use_keep_web_config
158 @kwport = getport 'keep-web-ssl'
159 @kwdport = getport 'keep-web-dl-ssl'
160 Rails.configuration.keep_web_url = "https://localhost:#{@kwport}/c=%{uuid_or_pdh}"
161 Rails.configuration.keep_web_download_url = "https://localhost:#{@kwdport}/c=%{uuid_or_pdh}"
162 CollectionsController.any_instance.expects(:file_enumerator).never
166 class ActionDispatch::IntegrationTest
167 # Make the Capybara DSL available in all integration tests
168 include Capybara::DSL
169 include ApiFixtureLoader
171 include AssertDomEvent
172 include HeadlessHelper
174 @@API_AUTHS = self.api_fixture('api_client_authorizations')
176 def page_with_token(token, path='/')
177 # Generate a page path with an embedded API token.
178 # Typical usage: visit page_with_token('token_name', page)
179 # The token can be specified by the name of an api_client_authorizations
180 # fixture, or passed as a raw string.
181 api_token = ((@@API_AUTHS.include? token) ?
182 @@API_AUTHS[token]['api_token'] : token)
183 path_parts = path.partition("#")
184 sep = (path_parts.first.include? '?') ? '&' : '?'
185 q_string = URI.encode_www_form('api_token' => api_token)
186 path_parts.insert(1, "#{sep}#{q_string}")
190 # Find a page element, but return false instead of raising an
191 # exception if not found. Use this with assertions to explain that
192 # the error signifies a failed test rather than an unexpected error
193 # during a testing procedure.
197 rescue Capybara::ElementNotFound
202 @@screenshot_count = 1
204 image_file = "./tmp/workbench-fail-#{@@screenshot_count}.png"
206 page.save_screenshot image_file
207 rescue Capybara::NotSupportedByDriverError
210 puts "Saved #{image_file}"
211 @@screenshot_count += 1
219 if Capybara.current_driver == :selenium
220 page.execute_script("window.localStorage.clear()")
222 Capybara.reset_sessions!