Sha256: 431b3f87e4ffa53881577bd95ab24449ac757e39a4a5217bbe11a66460124d95

Contents?: true

Size: 1.22 KB

Versions: 4

Compression:

Stored size: 1.22 KB

Contents

module HyperSpec
  module WaitForAjax
    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

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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hyper-spec-1.0.alpha1.8.1 lib/hyper-spec/wait_for_ajax.rb
hyper-spec-1.0.alpha1.8 lib/hyper-spec/wait_for_ajax.rb
hyper-spec-1.0.alpha1.7 lib/hyper-spec/wait_for_ajax.rb
hyper-spec-1.0.alpha1.6 lib/hyper-spec/wait_for_ajax.rb