Module: HyperSpec::WaitForAjax

Defined in:
lib/hyper-spec/wait_for_ajax.rb

Instance Method Summary collapse

Instance Method Details

#finished_all_ajax_requests?Boolean



37
38
39
40
41
42
43
# File 'lib/hyper-spec/wait_for_ajax.rb', line 37

def finished_all_ajax_requests?
  !running?
rescue Capybara::NotSupportedByDriverError
  true
rescue Exception => e
  e.message == 'either jQuery or Hyperloop::HTTP is not defined'
end

#running?Boolean



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hyper-spec/wait_for_ajax.rb', line 12

def running?
  jscode = "  (function() {\n    if (typeof Opal !== \"undefined\" && Opal.Hyperloop !== undefined) {\n      try {\n        return Opal.Hyperloop.$const_get(\"HTTP\")[\"$active?\"]();\n      } catch(err) {\n        if (typeof jQuery !== \"undefined\" && jQuery.active !== undefined) {\n          return (jQuery.active > 0);\n        } else {\n          return false;\n        }\n      }\n    } else if (typeof jQuery !== \"undefined\" && jQuery.active !== undefined) {\n      return (jQuery.active > 0);\n    } else {\n      return false;\n    }\n  })();\n  CODE\n  page.evaluate_script(jscode)\nrescue Exception => e\n  puts \"wait_for_ajax failed while testing state of ajax requests: \#{e}\"\nend\n"

#wait_for_ajaxObject



3
4
5
6
7
8
9
10
# File 'lib/hyper-spec/wait_for_ajax.rb', line 3

def wait_for_ajax
  Timeout.timeout(Capybara.default_max_wait_time) do
    loop do
      sleep 0.25
      break if finished_all_ajax_requests?
    end
  end
end