Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
feat(lib): add useAllAngularAppRoots option
Browse files Browse the repository at this point in the history
This allows waiting for all angular applications on the page, for
angular2 apps only.
  • Loading branch information
juliemr committed Oct 7, 2015
1 parent f246880 commit c5d37c2
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
32 changes: 29 additions & 3 deletions lib/clientsidescripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ function wrapWithHelpers(fun) {

/**
* Wait until Angular has finished rendering and has
* no outstanding $http calls before continuing.
* no outstanding $http calls before continuing. The specific Angular app
* is determined by the rootSelector.
*
* Asynchronous.
*
Expand Down Expand Up @@ -72,6 +73,32 @@ functions.waitForAngular = function(rootSelector, callback) {
}
};

/**
* Wait until all Angular2 applications on the page have become stable.
*
* Asynchronous.
*
* @param {function(string)} callback callback. If a failure occurs, it will
* be passed as a parameter.
*/
functions.waitForAllAngular2 = function(callback) {
try {
var testabilities = window.getAllAngularTestabilities();
var count = testabilities.length;
var decrement = function() {
count--;
if (count === 0) {
callback();
}
};
testabilities.forEach(function(testability) {
testability.whenStable(decrement);
});
} catch (err) {
callback(err.message);
}
};

/**
* Find a list of elements in the page by their angular binding.
*
Expand Down Expand Up @@ -558,9 +585,8 @@ functions.findByCssContainingText = function(cssSelector, searchText, using) {
* Asynchronous.
*
* @param {number} attempts Number of times to retry.
* @param {function} asyncCallback callback
* @param {function({version: ?number, message: ?string})} asyncCallback callback
*
* @return {{version: ?number, message: ?string}}
*/
functions.testForAngular = function(attempts, asyncCallback) {
var callback = function(args) {
Expand Down
30 changes: 26 additions & 4 deletions lib/protractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,18 @@ Protractor.prototype.getProcessedConfig = null;
*/
Protractor.prototype.forkNewDriverInstance = null;


/**
* Instead of using a single root element, search through all angular apps
* available on the page when finding elements or waiting for stability.
* Only compatible with Angular2.
*/
Protractor.prototype.useAllAngular2AppRoots = function() {
// The empty string is an invalid css selector, so we use it to easily
// signal to scripts to not find a root element.
this.rootEl = '';
};

/**
* The same as {@code webdriver.WebDriver.prototype.executeScript},
* but with a customized description for debugging.
Expand Down Expand Up @@ -325,10 +337,20 @@ Protractor.prototype.waitForAngular = function(opt_description) {
}, 'Ignore Synchronization Protractor.waitForAngular()');
}

return this.executeAsyncScript_(
clientSideScripts.waitForAngular,
'Protractor.waitForAngular()' + description,
this.rootEl).
function runWaitForAngularScript() {
if (self.rootEl) {
return self.executeAsyncScript_(
clientSideScripts.waitForAngular,
'Protractor.waitForAngular()' + description,
self.rootEl);
} else {
return self.executeAsyncScript_(
clientSideScripts.waitForAllAngular2,
'Protractor.waitForAngular()' + description);
}
}

return runWaitForAngularScript().
then(function(browserErr) {
if (browserErr) {
throw 'Error while waiting for Protractor to ' +
Expand Down
3 changes: 3 additions & 0 deletions lib/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ Runner.prototype.createBrowser = function(plugins) {
if (config.debuggerServerPort) {
browser_.debuggerServerPort_ = config.debuggerServerPort;
}
if (config.useAllAngular2AppRoots) {
browser_.useAllAngular2AppRoots();
}
var self = this;


Expand Down
12 changes: 10 additions & 2 deletions spec/angular2Conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var env = require('./environment.js');
// As Angular2 is in rapid development, the test application that ships with
// the Protractor repository does not yet contain an Angular2 section. This
// configuration assumes that you are serving the examples from the
// angular/angular repository at localhost:8000.
// angular/angular repository at localhost:8000.
// See https://github.com/angular/angular/blob/master/DEVELOPER.md for
// setup instructions.
//