I am attempting to create some basic tests to try out the new Cypress library. In my test I have cy.visit('http://mywebsite.com');
which is loading an AngularJS app that uses SystemJS.
If I understand Cypress correctly, I shouldn't have to do anything else and it will make sure the page is loaded before running anything else. However this doesn't seem to be working because the page is loaded, but SystemJS is still loading the modules.
How can I get Cypress to wait for all the SystemJS modules to load before running any more tests without using cy.wait(5000)
?
EDIT
Thanks to Dwelle this is the solution that works for me. I wrap the initial System.import in a promise that gets resolved once the AngularJS app has been bootstrapped.
window.APP_READY = new Promise(function(resolve, reject) {
System.import('app').then(function(app) {
angular.element(document).ready(function() {
angular.bootstrap(document, ['app']);
resolve();
});
});
});
And then in the test
cy.visit('http://mywebsite.com').its('APP_READY');
In theory, you can also solve this in a pure Cypress way.
If your page makes any request which indicates that your app is ready, you can make Cypress wait for it to complete before it proceeds. You might find this cleaner than the APP_READY work-around. Its just an opinion though, as this approach will fail if there are not requests you can trust to signal that your app is ready.
Cypress best practices: Unnecessary-Waiting.
Cypress docs on wait Alias.
Not familiar with SystemJS or your app, but if we assume you're doing some asynchronous work on load, you can set up some global property which indicates whether the app is ready or not.
Then, in your tests:
That being said --- if you're not doing any async setup in your app, but the
main.js
is simply being loaded asynchronously and it can take longer than 4sec, then I'd do this:You'll want to strip the
APP_READY
logic from production build.