Skip to content

Instantly share code, notes, and snippets.

@cawabunga
Created October 7, 2014 16:03
Show Gist options
  • Save cawabunga/bfb97a46ca969da35f2a to your computer and use it in GitHub Desktop.
Save cawabunga/bfb97a46ca969da35f2a to your computer and use it in GitHub Desktop.
Protractor Long polling fix (author fedenunez)
clientSideScripts.waitForAngular = function() {
var selector = arguments[0];
var callback = arguments[1];
var TIMEOUT_MS = 18000; // ms
var INTERVAL_MS = 5; //ms
var elapsed = 0;
var el = document.querySelector(selector);
// "inject" $rootScope service
var $rootScope = angular.element(el).injector().get('$rootScope');
// "inject" our own longpolling service
var modelService = angular.element(el).injector().get('model');
var checkLoop = function() {
if (elapsed <= TIMEOUT_MS ) {
// check angular digest state and check our longpoll service state (modelService state)
if (!$rootScope.$$phase && ( !modelService.isProcessing() )) {
// everything is synchronized!
callback();
} else {
// somebody is still processing, wait a little bit more.
elapsed = elapsed + INTERVAL_MS;
setTimeout( checkLoop, INTERVAL_MS );
}
} else {
callback( "Timedout while waiting for angular!" );
}
}
//initialize checking loop
setTimeout( checkLoop, 0 );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment