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 Capybara::Selenium::Driver.new app, profile: profile
34 Capybara.default_wait_time = 5
36 Timeout.timeout(Capybara.default_wait_time) do
37 loop until finished_all_ajax_requests?
41 def finished_all_ajax_requests?
42 page.evaluate_script('jQuery.active').zero?
47 # Yield the supplied block, then wait for an event to arrive at a
49 def assert_triggers_dom_event events, target='body'
50 magic = 'received-dom-event-' + rand(2**30).to_s(36)
51 page.evaluate_script <<eos
52 $('#{target}').one('#{events}', function() {
53 $('body').addClass('#{magic}');
57 assert_selector "body.#{magic}"
58 page.evaluate_script "$('body').removeClass('#{magic}');";
63 class HeadlessSingleton
65 @headless ||= Headless.new reuse: false
69 Capybara.default_driver = :rack_test
71 def self.included base
74 Capybara.use_default_driver
87 def need_selenium reason=nil, driver=:selenium
88 Capybara.current_driver = driver
89 unless ENV['ARVADOS_TEST_HEADFUL'] or @headless
90 @headless = HeadlessSingleton.get
95 def need_javascript reason=nil
96 unless Capybara.current_driver == :selenium
97 Capybara.current_driver = :poltergeist
102 class ActionDispatch::IntegrationTest
103 # Make the Capybara DSL available in all integration tests
104 include Capybara::DSL
105 include ApiFixtureLoader
107 include AssertDomEvent
108 include HeadlessHelper
110 @@API_AUTHS = self.api_fixture('api_client_authorizations')
112 def page_with_token(token, path='/')
113 # Generate a page path with an embedded API token.
114 # Typical usage: visit page_with_token('token_name', page)
115 # The token can be specified by the name of an api_client_authorizations
116 # fixture, or passed as a raw string.
117 api_token = ((@@API_AUTHS.include? token) ?
118 @@API_AUTHS[token]['api_token'] : token)
119 path_parts = path.partition("#")
120 sep = (path_parts.first.include? '?') ? '&' : '?'
121 q_string = URI.encode_www_form('api_token' => api_token)
122 path_parts.insert(1, "#{sep}#{q_string}")
126 # Find a page element, but return false instead of raising an
127 # exception if not found. Use this with assertions to explain that
128 # the error signifies a failed test rather than an unexpected error
129 # during a testing procedure.
133 rescue Capybara::ElementNotFound
138 @@screenshot_count = 1
140 image_file = "./tmp/workbench-fail-#{@@screenshot_count}.png"
142 page.save_screenshot image_file
143 rescue Capybara::NotSupportedByDriverError
146 puts "Saved #{image_file}"
147 @@screenshot_count += 1
155 if Capybara.current_driver == :selenium
156 page.execute_script("window.localStorage.clear()")
158 Capybara.reset_sessions!