Sha256: 8100408a9a2d974e7c397e6336ce08d7613f56031e54c48ad923b0eee9af9b91

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 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)
          if page_reloaded_on_wait?
            return unless angular_app?
            setup_ready
          end
          sleep(0.01)
        end
      end

      private

      def timeout?(start)
        Time.now - start > Capybara::Angular.default_max_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?
        js = "(typeof angular !== 'undefined') && "
        js += "angular.element(document.querySelector('[ng-app], [data-ng-app]')).length > 0"
        page.evaluate_script js

      rescue Capybara::NotSupportedByDriverError
        false
      end

      def setup_ready
        page.execute_script <<-JS
          var el = document.querySelector('body')
          window.angularReady = false;

          if (angular.getTestability) {
            angular.getTestability(el).whenStable(function() { window.angularReady = true; });
          } else {
            var $browser = angular.element(el).injector().get('$browser');

            if ($browser.outstandingRequestCount > 0) { window.angularReady = false; }
            $browser.notifyWhenNoOutstandingRequests(function() { window.angularReady = true; });
          }
        JS
      end

      def page_reloaded_on_wait?
        page.evaluate_script("window.angularReady === undefined")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
capybara-angular-0.2.2 lib/capybara/angular/waiter.rb