From 9f1c09f46f255d217ec0a14686bd4ccc6b02b08c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20=C4=90=E1=BB=A9c=20Long?= Date: Thu, 18 Jan 2018 12:44:22 +0700 Subject: [PATCH] Fix: setup_ready with delayed angular bootstrapping --- lib/capybara/angular/waiter.rb | 27 +++++++++++++++++++-------- spec/capybara/angular_spec.rb | 9 +++++++++ spec/public/delayed-manual.html | 14 ++++++++++++++ 3 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 spec/public/delayed-manual.html diff --git a/lib/capybara/angular/waiter.rb b/lib/capybara/angular/waiter.rb index 7135e91..a06f176 100644 --- a/lib/capybara/angular/waiter.rb +++ b/lib/capybara/angular/waiter.rb @@ -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 diff --git a/spec/capybara/angular_spec.rb b/spec/capybara/angular_spec.rb index 5d9185f..decf3d4 100644 --- a/spec/capybara/angular_spec.rb +++ b/spec/capybara/angular_spec.rb @@ -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 @@ -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 diff --git a/spec/public/delayed-manual.html b/spec/public/delayed-manual.html new file mode 100644 index 0000000..cd980bf --- /dev/null +++ b/spec/public/delayed-manual.html @@ -0,0 +1,14 @@ + + + + + + +{{text}} + + +