2 require 'capybara/rails'
3 require 'capybara/poltergeist'
7 Capybara.register_driver :poltergeist do |app|
8 Capybara::Poltergeist::Driver.new app, {
9 window_size: [1200, 800],
10 phantomjs_options: ['--ignore-ssl-errors=true'],
16 Capybara.default_wait_time = 5
18 Timeout.timeout(Capybara.default_wait_time) do
19 loop until finished_all_ajax_requests?
23 def finished_all_ajax_requests?
24 page.evaluate_script('jQuery.active').zero?
29 # Yield the supplied block, then wait for an event to arrive at a
31 def assert_triggers_dom_event events, target='body'
32 magic = 'received-dom-event-' + rand(2**30).to_s(36)
33 page.evaluate_script <<eos
34 $('#{target}').one('#{events}', function() {
35 $('body').addClass('#{magic}');
39 assert_selector "body.#{magic}"
40 page.evaluate_script "$('body').removeClass('#{magic}');";
45 class HeadlessSingleton
47 @headless ||= Headless.new reuse: false
51 Capybara.default_driver = :rack_test
53 def self.included base
56 Capybara.use_default_driver
69 def need_selenium reason=nil
70 Capybara.current_driver = :selenium
71 unless ENV['ARVADOS_TEST_HEADFUL'] or @headless
72 @headless = HeadlessSingleton.get
77 def need_javascript reason=nil
78 unless Capybara.current_driver == :selenium
79 Capybara.current_driver = :poltergeist
84 class ActionDispatch::IntegrationTest
85 # Make the Capybara DSL available in all integration tests
87 include ApiFixtureLoader
89 include AssertDomEvent
90 include HeadlessHelper
92 @@API_AUTHS = self.api_fixture('api_client_authorizations')
94 def page_with_token(token, path='/')
95 # Generate a page path with an embedded API token.
96 # Typical usage: visit page_with_token('token_name', page)
97 # The token can be specified by the name of an api_client_authorizations
98 # fixture, or passed as a raw string.
99 api_token = ((@@API_AUTHS.include? token) ?
100 @@API_AUTHS[token]['api_token'] : token)
101 path_parts = path.partition("#")
102 sep = (path_parts.first.include? '?') ? '&' : '?'
103 q_string = URI.encode_www_form('api_token' => api_token)
104 path_parts.insert(1, "#{sep}#{q_string}")
108 # Find a page element, but return false instead of raising an
109 # exception if not found. Use this with assertions to explain that
110 # the error signifies a failed test rather than an unexpected error
111 # during a testing procedure.
115 rescue Capybara::ElementNotFound
120 @@screenshot_count = 1
122 image_file = "./tmp/workbench-fail-#{@@screenshot_count}.png"
124 page.save_screenshot image_file
125 rescue Capybara::NotSupportedByDriverError
128 puts "Saved #{image_file}"
129 @@screenshot_count += 1
137 if Capybara.current_driver == :selenium
138 page.execute_script("window.localStorage.clear()")
140 Capybara.reset_sessions!