2754: Merge branch '2754-easy-run-pipeline' refs #2754
[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 setup
29     reset_session!
30     super
31   end
32
33   def page_with_token(token, path='/')
34     # Generate a page path with an embedded API token.
35     # Typical usage: visit page_with_token('token_name', page)
36     # The token can be specified by the name of an api_client_authorizations
37     # fixture, or passed as a raw string.
38     api_token = ((@@API_AUTHS.include? token) ?
39                  @@API_AUTHS[token]['api_token'] : token)
40     sep = (path.include? '?') ? '&' : '?'
41     q_string = URI.encode_www_form('api_token' => api_token)
42     "#{path}#{sep}#{q_string}"
43   end
44
45   # Find a page element, but return false instead of raising an
46   # exception if not found. Use this with assertions to explain that
47   # the error signifies a failed test rather than an unexpected error
48   # during a testing procedure.
49   def find? *args
50     begin
51       find *args
52     rescue Capybara::ElementNotFound
53       false
54     end
55   end
56
57   @@screenshot_count = 0
58   def screenshot
59     image_file = "./tmp/workbench-fail-#{@@screenshot_count += 1}.png"
60     page.save_screenshot image_file
61     puts "Saved #{image_file}"
62   end
63
64   teardown do
65     if not passed?
66       screenshot
67     end
68     if Capybara.current_driver == :selenium
69       page.execute_script("window.localStorage.clear()")
70     end
71   end
72 end