Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: setup_ready with delayed angular bootstrapping #38

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions lib/capybara/angular/waiter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,29 @@ def angular_app?

def setup_ready
page.execute_script <<-JS
var el = document.querySelector('[ng-app], [data-ng-app]') || document.querySelector('body');
var el = document.querySelector('[ng-app], [data-ng-app]') || document.body;
var injector = angular.element(el).injector();

window.angularReady = false;

if (angular.getTestability) {
angular.getTestability(el).whenStable(function() { window.angularReady = true; });
} else {
var $browser = angular.element(el).injector().get('$browser');

if ($browser.outstandingRequestCount > 0) { window.angularReady = false; }
$browser.notifyWhenNoOutstandingRequests(function() { window.angularReady = true; });
function capybaraAngularSetupReady() {
try {
angular.getTestability(el).whenStable(function() { window.angularReady = true; });
} catch(error) {
var $browser = injector.get('$browser');
if ($browser.outstandingRequestCount > 0) { window.angularReady = false; }
$browser.notifyWhenNoOutstandingRequests(function() { window.angularReady = true; });
}
}

if (injector === void 0) {
var tid = setInterval(function() {
injector = angular.element(el).injector();
if (injector === void 0) return;
clearInterval(tid);
capybaraAngularSetupReady();
}, 100);
} else capybaraAngularSetupReady();
JS
end

Expand Down
9 changes: 9 additions & 0 deletions spec/capybara/angular_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
timeout_page_should_have_waited
end

scenario 'when manually bootstrapping an angular application after a delay' do
open_delayed_manual_bootstrap_page
timeout_page_should_have_waited
end

scenario 'when using ng-app to bootstrap an application' do
open_ng_app_bootstrap_page
timeout_page_should_have_waited
Expand All @@ -31,6 +36,10 @@ def open_manual_bootstrap_page
visit '/manual.html'
end

def open_delayed_manual_bootstrap_page
visit '/delayed-manual.html'
end

def open_ng_app_bootstrap_page
visit '/ng-app.html'
end
Expand Down
14 changes: 14 additions & 0 deletions spec/public/delayed-manual.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<html>
<head>
<script src="/js/angular-1.5.8.js"></script>
<script src="/js/app.js"></script>
</head>
<body ng-controller="waitingController">
{{text}}
<script>
setTimeout(function() {
angular.bootstrap(document.body, ['app']);
}, 5000);
</script>
</body>
</html>