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