Sha256: 0a819654d612f7d2a072308753f55778cc1a55075270680fc0734e821dc58b0b

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

module Capybara
  module Angular
    class Waiter
      attr_accessor :page

      def initialize(page)
        @page = page
      end

      def wait_until_ready
        return unless angular_app?

        setup_ready

        start = Time.now
        until ready?
          timeout! if timeout?(start)
          sleep(0.01)
        end
      end

      private

      def timeout?(start)
        Time.now - start > Capybara.default_wait_time
      end

      def timeout!
        raise TimeoutError.new("timeout while waiting for angular")
      end

      def ready?
        page.evaluate_script("window.angularReady")
      end

      def angular_app?
        page.evaluate_script "(typeof $ != 'undefined') && $('[ng-app]')"
      end

      def setup_ready
        page.execute_script <<-JS
          window.angularReady = false;
          var app = $('*[ng-app]');
          var injector = app.injector();

          injector.invoke(function($browser) {
            $browser.notifyWhenNoOutstandingRequests(function() {
              window.angularReady = true;
            });
          });
        JS
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
capybara-angular-0.0.2 lib/capybara/angular/waiter.rb
capybara-angular-0.0.1 lib/capybara/angular/waiter.rb