wait_for_requests.rb 2.07 KB
Newer Older
1
2
3
4
module WaitForRequests
  extend self

  # This is inspired by http://www.salsify.com/blog/engineering/tearing-capybara-ajax-tests
5
  def block_and_wait_for_requests_complete
6
7
8
9
10
    block_requests { wait_for_all_requests }
  end

  # Block all requests inside block with 503 response
  def block_requests
11
    Gitlab::Testing::RequestBlockerMiddleware.block_requests!
12
    yield
13
14
15
16
  ensure
    Gitlab::Testing::RequestBlockerMiddleware.allow_requests!
  end

17
18
19
20
21
22
23
24
25
  # Slow down requests inside block by injecting `sleep 0.2` before each response
  def slow_requests
    Gitlab::Testing::RequestBlockerMiddleware.slow_requests!
    yield
  ensure
    Gitlab::Testing::RequestBlockerMiddleware.allow_requests!
  end

  # Wait for client-side AJAX requests
26
  def wait_for_requests
27
28
29
30
31
32
33
34
35
    wait_for('JS requests complete') { finished_all_js_requests? }
  end

  # Wait for active Rack requests and client-side AJAX requests
  def wait_for_all_requests
    wait_for('pending requests complete') do
      finished_all_rack_reqiests? &&
        finished_all_js_requests?
    end
36
37
38
39
  end

  private

40
41
42
43
44
  def finished_all_rack_reqiests?
    Gitlab::Testing::RequestBlockerMiddleware.num_active_requests.zero?
  end

  def finished_all_js_requests?
45
46
47
48
49
50
    return true unless javascript_test?

    finished_all_ajax_requests? &&
      finished_all_vue_resource_requests?
  end

51
52
53
54
55
56
57
58
59
60
61
62
  # Waits until the passed block returns true
  def wait_for(condition_name, max_wait_time: Capybara.default_max_wait_time, polling_interval: 0.01)
    wait_until = Time.now + max_wait_time.seconds
    loop do
      break if yield
      if Time.now > wait_until
        raise "Condition not met: #{condition_name}"
      else
        sleep(polling_interval)
      end
    end
  end
63
64

  def finished_all_vue_resource_requests?
65
    Capybara.page.evaluate_script('window.activeVueResources || 0').zero?
66
67
68
  end

  def finished_all_ajax_requests?
69
    return true if Capybara.page.evaluate_script('typeof jQuery === "undefined"')
70

71
    Capybara.page.evaluate_script('jQuery.active').zero?
72
73
74
75
76
  end

  def javascript_test?
    Capybara.current_driver == Capybara.javascript_driver
  end
77
end