2 require 'capybara/rails'
3 require 'capybara/poltergeist'
7 def available_port for_what
8 Addrinfo.tcp("0.0.0.0", 0).listen do |srv|
9 port = srv.connect_address.ip_port
10 STDERR.puts "Using port #{port} for #{for_what}"
17 port: available_port('selenium'),
23 phantomjs_options: ['--ignore-ssl-errors=true'],
24 port: available_port('poltergeist'),
25 window_size: [1200, 800],
29 Capybara.register_driver :poltergeist do |app|
30 Capybara::Poltergeist::Driver.new app, poltergeist_opts
33 Capybara.register_driver :poltergeist_debug do |app|
34 Capybara::Poltergeist::Driver.new app, poltergeist_opts.merge(inspector: true)
37 Capybara.register_driver :poltergeist_without_file_api do |app|
38 js = File.expand_path '../support/remove_file_api.js', __FILE__
39 Capybara::Poltergeist::Driver.new app, poltergeist_opts.merge(extensions: [js])
42 Capybara.register_driver :selenium do |app|
43 Capybara::Selenium::Driver.new app, selenium_opts
46 Capybara.register_driver :selenium_with_download do |app|
47 profile = Selenium::WebDriver::Firefox::Profile.new
48 profile['browser.download.dir'] = DownloadHelper.path.to_s
49 profile['browser.download.downloadDir'] = DownloadHelper.path.to_s
50 profile['browser.download.defaultFolder'] = DownloadHelper.path.to_s
51 profile['browser.download.folderList'] = 2 # "save to user-defined location"
52 profile['browser.download.manager.showWhenStarting'] = false
53 profile['browser.helperApps.alwaysAsk.force'] = false
54 profile['browser.helperApps.neverAsk.saveToDisk'] = 'text/plain,application/octet-stream'
55 Capybara::Selenium::Driver.new app, selenium_opts.merge(profile: profile)
59 Capybara.default_max_wait_time = 10
61 Timeout.timeout(Capybara.default_max_wait_time) do
62 loop until finished_all_ajax_requests?
66 def finished_all_ajax_requests?
67 page.evaluate_script('jQuery.active').zero?
72 # Yield the supplied block, then wait for an event to arrive at a
74 def assert_triggers_dom_event events, target='body'
75 magic = 'received-dom-event-' + rand(2**30).to_s(36)
76 page.evaluate_script <<eos
77 $('#{target}').one('#{events}', function() {
78 $('body').addClass('#{magic}');
82 assert_selector "body.#{magic}"
83 page.evaluate_script "$('body').removeClass('#{magic}');";
88 class HeadlessSingleton
89 @display = ENV['ARVADOS_TEST_HEADLESS_DISPLAY'] || rand(400)+100
90 STDERR.puts "Using display :#{@display} for headless tests"
92 @headless ||= Headless.new reuse: false, display: @display
96 Capybara.default_driver = :rack_test
98 def self.included base
101 Capybara.use_default_driver
114 def need_selenium reason=nil, driver=:selenium
115 Capybara.current_driver = driver
116 unless ENV['ARVADOS_TEST_HEADFUL'] or @headless
117 @headless = HeadlessSingleton.get
122 def need_javascript reason=nil
123 unless Capybara.current_driver == :selenium
124 Capybara.current_driver = :poltergeist
131 File.read(File.expand_path("../../../../tmp/#{service}.port", __FILE__))
134 def use_keep_web_config
135 @kwport = getport 'keep-web-ssl'
136 @kwdport = getport 'keep-web-dl-ssl'
137 Rails.configuration.keep_web_url = "https://localhost:#{@kwport}/c=%{uuid_or_pdh}"
138 Rails.configuration.keep_web_download_url = "https://localhost:#{@kwdport}/c=%{uuid_or_pdh}"
139 CollectionsController.any_instance.expects(:file_enumerator).never
143 class ActionDispatch::IntegrationTest
144 # Make the Capybara DSL available in all integration tests
145 include Capybara::DSL
146 include ApiFixtureLoader
148 include AssertDomEvent
149 include HeadlessHelper
151 @@API_AUTHS = self.api_fixture('api_client_authorizations')
153 def page_with_token(token, path='/')
154 # Generate a page path with an embedded API token.
155 # Typical usage: visit page_with_token('token_name', page)
156 # The token can be specified by the name of an api_client_authorizations
157 # fixture, or passed as a raw string.
158 api_token = ((@@API_AUTHS.include? token) ?
159 @@API_AUTHS[token]['api_token'] : token)
160 path_parts = path.partition("#")
161 sep = (path_parts.first.include? '?') ? '&' : '?'
162 q_string = URI.encode_www_form('api_token' => api_token)
163 path_parts.insert(1, "#{sep}#{q_string}")
167 # Find a page element, but return false instead of raising an
168 # exception if not found. Use this with assertions to explain that
169 # the error signifies a failed test rather than an unexpected error
170 # during a testing procedure.
174 rescue Capybara::ElementNotFound
179 @@screenshot_count = 1
181 image_file = "./tmp/workbench-fail-#{@@screenshot_count}.png"
183 page.save_screenshot image_file
184 rescue Capybara::NotSupportedByDriverError
187 puts "Saved #{image_file}"
188 @@screenshot_count += 1
196 if Capybara.current_driver == :selenium
197 page.execute_script("window.localStorage.clear()")
199 Capybara.reset_sessions!