3021: 4399: Convert some tests from selenium to phantomjs. Restart Headless less.
[arvados.git] / apps / workbench / test / integration_helper.rb
1 require 'test_helper'
2 require 'capybara/rails'
3 require 'capybara/poltergeist'
4 require 'uri'
5 require 'yaml'
6
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'],
11     inspector: true,
12   }
13 end
14
15 Headless.new.start
16
17 module WaitForAjax
18   Capybara.default_wait_time = 5
19   def wait_for_ajax
20     Timeout.timeout(Capybara.default_wait_time) do
21       loop until finished_all_ajax_requests?
22     end
23   end
24
25   def finished_all_ajax_requests?
26     page.evaluate_script('jQuery.active').zero?
27   end
28 end
29
30 class ActionDispatch::IntegrationTest
31   # Make the Capybara DSL available in all integration tests
32   include Capybara::DSL
33   include ApiFixtureLoader
34   include WaitForAjax
35
36   @@API_AUTHS = self.api_fixture('api_client_authorizations')
37
38   def page_with_token(token, path='/')
39     # Generate a page path with an embedded API token.
40     # Typical usage: visit page_with_token('token_name', page)
41     # The token can be specified by the name of an api_client_authorizations
42     # fixture, or passed as a raw string.
43     api_token = ((@@API_AUTHS.include? token) ?
44                  @@API_AUTHS[token]['api_token'] : token)
45     sep = (path.include? '?') ? '&' : '?'
46     q_string = URI.encode_www_form('api_token' => api_token)
47     "#{path}#{sep}#{q_string}"
48   end
49
50   # Find a page element, but return false instead of raising an
51   # exception if not found. Use this with assertions to explain that
52   # the error signifies a failed test rather than an unexpected error
53   # during a testing procedure.
54   def find? *args
55     begin
56       find *args
57     rescue Capybara::ElementNotFound
58       false
59     end
60   end
61
62   @@screenshot_count = 1
63   def screenshot
64     image_file = "./tmp/workbench-fail-#{@@screenshot_count}.png"
65     begin
66       page.save_screenshot image_file
67     rescue Capybara::NotSupportedByDriverError
68       # C'est la vie.
69     else
70       puts "Saved #{image_file}"
71       @@screenshot_count += 1
72     end
73   end
74
75   teardown do
76     if not passed?
77       screenshot
78     end
79     if Capybara.current_driver == :selenium
80       page.execute_script("window.localStorage.clear()")
81     end
82     Capybara.reset_sessions!
83   end
84 end