Last active
February 11, 2020 15:12
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package extensions | |
trait AngularJsAware { | |
boolean isAngularReady() { | |
js.exec('window.MYAPP.waitForAngular();'); | |
waitFor { | |
js.MYAPP.APP_READY == true | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ExamplePage extends Page implements AngularJSAware { | |
static at = { | |
angularReady | |
} | |
static content = { | |
someResolvedText { $('.some-selector').text() } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.