2 require 'capybara/rails'
3 require 'capybara/poltergeist'
8 window_size: [1200, 800],
9 phantomjs_options: ['--ignore-ssl-errors=true'],
13 Capybara.register_driver :poltergeist do |app|
14 Capybara::Poltergeist::Driver.new app, POLTERGEIST_OPTS
17 Capybara.register_driver :poltergeist_without_file_api do |app|
18 js = File.expand_path '../support/remove_file_api.js', __FILE__
19 Capybara::Poltergeist::Driver.new app, POLTERGEIST_OPTS.merge(extensions: [js])
22 Capybara.register_driver :selenium_with_download do |app|
23 profile = Selenium::WebDriver::Firefox::Profile.new
24 profile['browser.download.dir'] = DownloadHelper.path.to_s
25 profile['browser.download.downloadDir'] = DownloadHelper.path.to_s
26 profile['browser.download.defaultFolder'] = DownloadHelper.path.to_s
27 profile['browser.download.folderList'] = 2 # "save to user-defined location"
28 profile['browser.download.manager.showWhenStarting'] = false
29 profile['browser.helperApps.alwaysAsk.force'] = false
30 profile['browser.helperApps.neverAsk.saveToDisk'] = 'text/plain,application/octet-stream'
31 Capybara::Selenium::Driver.new app, profile: profile
35 Capybara.default_wait_time = 5
37 Timeout.timeout(Capybara.default_wait_time) do
38 loop until finished_all_ajax_requests?
42 def finished_all_ajax_requests?
43 page.evaluate_script('jQuery.active').zero?
48 # Yield the supplied block, then wait for an event to arrive at a
50 def assert_triggers_dom_event events, target='body'
51 magic = 'received-dom-event-' + rand(2**30).to_s(36)
52 page.evaluate_script <<eos
53 $('#{target}').one('#{events}', function() {
54 $('body').addClass('#{magic}');
58 assert_selector "body.#{magic}"
59 page.evaluate_script "$('body').removeClass('#{magic}');";
64 class HeadlessSingleton
66 @headless ||= Headless.new reuse: false
70 Capybara.default_driver = :rack_test
72 def self.included base
75 Capybara.use_default_driver
88 def need_selenium reason=nil, driver=:selenium
89 Capybara.current_driver = driver
90 unless ENV['ARVADOS_TEST_HEADFUL'] or @headless
91 @headless = HeadlessSingleton.get
96 def need_javascript reason=nil
97 unless Capybara.current_driver == :selenium
98 Capybara.current_driver = :poltergeist
103 class ActionDispatch::IntegrationTest
104 # Make the Capybara DSL available in all integration tests
105 include Capybara::DSL
106 include ApiFixtureLoader
108 include AssertDomEvent
109 include HeadlessHelper
111 @@API_AUTHS = self.api_fixture('api_client_authorizations')
113 def page_with_token(token, path='/')
114 # Generate a page path with an embedded API token.
115 # Typical usage: visit page_with_token('token_name', page)
116 # The token can be specified by the name of an api_client_authorizations
117 # fixture, or passed as a raw string.
118 api_token = ((@@API_AUTHS.include? token) ?
119 @@API_AUTHS[token]['api_token'] : token)
120 path_parts = path.partition("#")
121 sep = (path_parts.first.include? '?') ? '&' : '?'
122 q_string = URI.encode_www_form('api_token' => api_token)
123 path_parts.insert(1, "#{sep}#{q_string}")
127 # Find a page element, but return false instead of raising an
128 # exception if not found. Use this with assertions to explain that
129 # the error signifies a failed test rather than an unexpected error
130 # during a testing procedure.
134 rescue Capybara::ElementNotFound
139 @@screenshot_count = 1
141 image_file = "./tmp/workbench-fail-#{@@screenshot_count}.png"
143 page.save_screenshot image_file
144 rescue Capybara::NotSupportedByDriverError
147 puts "Saved #{image_file}"
148 @@screenshot_count += 1
156 if Capybara.current_driver == :selenium
157 page.execute_script("window.localStorage.clear()")
159 Capybara.reset_sessions!