Skip to content

Instantly share code, notes, and snippets.

@antony
Last active February 11, 2020 15:12
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antony/270006c36ac7799a5299 to your computer and use it in GitHub Desktop.
Save antony/270006c36ac7799a5299 to your computer and use it in GitHub Desktop.
Make Geb work with AngularJS reliably, by waiting for the angular views to be resolved before attempting assertions. This uses the same onReady technique as Protractor.
// Include this file in the head of your main layout.
window.MYAPP = window.MYAPP || {};
window.MYAPP.waitForAngular = function() {
window.MYAPP.APP_READY = false;
function done(err) {
if (err) {
console.error('Waiting for Angular:', err);
return;
}
window.MYAPP.APP_READY = true;
}
var el = document.querySelector('html');
try {
if (!window.angular) {
throw new Error('Angular could not be found on the window');
}
if (angular.getTestability) {
angular.getTestability(el).whenStable(done);
} else {
if (!angular.element(el).injector()) {
throw new Error('Root element (html) has no injector. This may mean it is not inside ng-app.');
}
angular.element(el).injector().get('$browser').notifyWhenNoOutstandingRequests(done);
}
} catch (err) {
done(err.message);
}
};
package extensions
trait AngularJsAware {
boolean isAngularReady() {
js.exec('window.MYAPP.waitForAngular();');
waitFor {
js.MYAPP.APP_READY == true
}
}
}
class ExampleGebSpec extends GebSpec {
def 'Check that something is as it should be' {
given:
to ExamplePage
expect:
someResolvedText == 'This is fetched from a remote source'
}
}
class ExamplePage extends Page implements AngularJSAware {
static at = {
angularReady
}
static content = {
someResolvedText { $('.some-selector').text() }
}
}
@antony
Copy link
Author

antony commented Jan 19, 2017

Hi @pbwebguy - I'm not sure what you mean by that. We use Geb as an alternative to Protractor, and this Gist is to give Geb the 'awareness' that protractor uses to ensure the page model is ready to be tested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment