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])
23 Capybara.default_wait_time = 5
25 Timeout.timeout(Capybara.default_wait_time) do
26 loop until finished_all_ajax_requests?
30 def finished_all_ajax_requests?
31 page.evaluate_script('jQuery.active').zero?
36 # Yield the supplied block, then wait for an event to arrive at a
38 def assert_triggers_dom_event events, target='body'
39 magic = 'received-dom-event-' + rand(2**30).to_s(36)
40 page.evaluate_script <<eos
41 $('#{target}').one('#{events}', function() {
42 $('body').addClass('#{magic}');
46 assert_selector "body.#{magic}"
47 page.evaluate_script "$('body').removeClass('#{magic}');";
52 class HeadlessSingleton
54 @headless ||= Headless.new reuse: false
58 Capybara.default_driver = :rack_test
60 def self.included base
63 Capybara.use_default_driver
76 def need_selenium reason=nil
77 Capybara.current_driver = :selenium
78 unless ENV['ARVADOS_TEST_HEADFUL'] or @headless
79 @headless = HeadlessSingleton.get
84 def need_javascript reason=nil
85 unless Capybara.current_driver == :selenium
86 Capybara.current_driver = :poltergeist
91 class ActionDispatch::IntegrationTest
92 # Make the Capybara DSL available in all integration tests
94 include ApiFixtureLoader
96 include AssertDomEvent
97 include HeadlessHelper
99 @@API_AUTHS = self.api_fixture('api_client_authorizations')
101 def page_with_token(token, path='/')
102 # Generate a page path with an embedded API token.
103 # Typical usage: visit page_with_token('token_name', page)
104 # The token can be specified by the name of an api_client_authorizations
105 # fixture, or passed as a raw string.
106 api_token = ((@@API_AUTHS.include? token) ?
107 @@API_AUTHS[token]['api_token'] : token)
108 path_parts = path.partition("#")
109 sep = (path_parts.first.include? '?') ? '&' : '?'
110 q_string = URI.encode_www_form('api_token' => api_token)
111 path_parts.insert(1, "#{sep}#{q_string}")
115 # Find a page element, but return false instead of raising an
116 # exception if not found. Use this with assertions to explain that
117 # the error signifies a failed test rather than an unexpected error
118 # during a testing procedure.
122 rescue Capybara::ElementNotFound
127 @@screenshot_count = 1
129 image_file = "./tmp/workbench-fail-#{@@screenshot_count}.png"
131 page.save_screenshot image_file
132 rescue Capybara::NotSupportedByDriverError
135 puts "Saved #{image_file}"
136 @@screenshot_count += 1
144 if Capybara.current_driver == :selenium
145 page.execute_script("window.localStorage.clear()")
147 Capybara.reset_sessions!