|
1 | 1 | /**
|
2 | 2 | * Wait until Angular has finished rendering and has
|
3 | 3 | * no outstanding $http calls before continuing. The specific Angular app
|
4 |
| - * is determined by the rootSelector. |
| 4 | + * is determined by the rootSelector. Copied from Protractor 5. |
5 | 5 | *
|
6 | 6 | * Asynchronous.
|
7 | 7 | *
|
8 | 8 | * @param {string} rootSelector The selector housing an ng-app
|
9 |
| - * @param {boolean} ng12Hybrid Flag set if app is a hybrid of angular 1 and 2 |
10 | 9 | * @param {function(string)} callback callback. If a failure occurs, it will
|
11 | 10 | * be passed as a parameter.
|
12 | 11 | */
|
13 |
| -exports.NG_WAIT_FN = function(rootSelector, ng12Hybrid, callback) { |
14 |
| - var el = document.querySelector(rootSelector); |
15 |
| - |
| 12 | +function waitForAngular(rootSelector, callback) { |
16 | 13 | try {
|
17 |
| - if (!ng12Hybrid && window.getAngularTestability) { |
| 14 | + if (window.angular && !(window.angular.version && |
| 15 | + window.angular.version.major > 1)) { |
| 16 | + /* ng1 */ |
| 17 | + var hooks = getNg1Hooks(rootSelector); |
| 18 | + if (hooks.$$testability) { |
| 19 | + hooks.$$testability.whenStable(callback); |
| 20 | + } else if (hooks.$injector) { |
| 21 | + hooks.$injector.get('$browser'). |
| 22 | + notifyWhenNoOutstandingRequests(callback); |
| 23 | + } else if (!!rootSelector) { |
| 24 | + throw new Error('Could not automatically find injector on page: "' + |
| 25 | + window.location.toString() + '". Consider using config.rootEl'); |
| 26 | + } else { |
| 27 | + throw new Error('root element (' + rootSelector + ') has no injector.' + |
| 28 | + ' this may mean it is not inside ng-app.'); |
| 29 | + } |
| 30 | + } else if (rootSelector && window.getAngularTestability) { |
| 31 | + var el = document.querySelector(rootSelector); |
18 | 32 | window.getAngularTestability(el).whenStable(callback);
|
19 |
| - return; |
20 |
| - } |
21 |
| - if (!window.angular) { |
| 33 | + } else if (window.getAllAngularTestabilities) { |
| 34 | + var testabilities = window.getAllAngularTestabilities(); |
| 35 | + var count = testabilities.length; |
| 36 | + var decrement = function() { |
| 37 | + count--; |
| 38 | + if (count === 0) { |
| 39 | + callback(); |
| 40 | + } |
| 41 | + }; |
| 42 | + testabilities.forEach(function(testability) { |
| 43 | + testability.whenStable(decrement); |
| 44 | + }); |
| 45 | + } else if (!window.angular) { |
22 | 46 | throw new Error('window.angular is undefined. This could be either ' +
|
23 | 47 | 'because this is a non-angular page or because your test involves ' +
|
24 | 48 | 'client-side navigation, which can interfere with Protractor\'s ' +
|
25 | 49 | 'bootstrapping. See http://git.io/v4gXM for details');
|
26 |
| - } |
27 |
| - if (angular.getTestability) { |
28 |
| - angular.getTestability(el).whenStable(callback); |
| 50 | + } else if (window.angular.version >= 2) { |
| 51 | + throw new Error('You appear to be using angular, but window.' + |
| 52 | + 'getAngularTestability was never set. This may be due to bad ' + |
| 53 | + 'obfuscation.'); |
29 | 54 | } else {
|
30 |
| - if (!angular.element(el).injector()) { |
31 |
| - throw new Error('root element (' + rootSelector + ') has no injector.' + |
32 |
| - ' this may mean it is not inside ng-app.'); |
33 |
| - } |
34 |
| - angular.element(el).injector().get('$browser'). |
35 |
| - notifyWhenNoOutstandingRequests(callback); |
| 55 | + throw new Error('Cannot get testability API for unknown angular ' + |
| 56 | + 'version "' + window.angular.version + '"'); |
36 | 57 | }
|
37 | 58 | } catch (err) {
|
38 | 59 | callback(err.message);
|
39 | 60 | }
|
40 | 61 | };
|
41 | 62 |
|
42 |
| -/** |
43 |
| - * Wait until all Angular2 applications on the page have become stable. |
| 63 | +/* Tries to find $$testability and possibly $injector for an ng1 app |
44 | 64 | *
|
45 |
| - * Asynchronous. |
| 65 | + * By default, doesn't care about $injector if it finds $$testability. However, |
| 66 | + * these priorities can be reversed. |
46 | 67 | *
|
47 |
| - * @param {function(string)} callback callback. If a failure occurs, it will |
48 |
| - * be passed as a parameter. |
| 68 | + * @param {string=} selector The selector for the element with the injector. If |
| 69 | + * falsy, tries a variety of methods to find an injector |
| 70 | + * @param {boolean=} injectorPlease Prioritize finding an injector |
| 71 | + * @return {$$testability?: Testability, $injector?: Injector} Returns whatever |
| 72 | + * ng1 app hooks it finds |
49 | 73 | */
|
50 |
| -exports.NG2_WAIT_FN = function(callback) { |
51 |
| - try { |
52 |
| - var testabilities = window.getAllAngularTestabilities(); |
53 |
| - var count = testabilities.length; |
54 |
| - var decrement = function() { |
55 |
| - count--; |
56 |
| - if (count === 0) { |
57 |
| - callback(); |
| 74 | +function getNg1Hooks(selector, injectorPlease) { |
| 75 | + function tryEl(el) { |
| 76 | + try { |
| 77 | + if (!injectorPlease && angular.getTestability) { |
| 78 | + var $$testability = angular.getTestability(el); |
| 79 | + if ($$testability) { |
| 80 | + return {$$testability: $$testability}; |
| 81 | + } |
| 82 | + } else { |
| 83 | + var $injector = angular.element(el).injector(); |
| 84 | + if ($injector) { |
| 85 | + return {$injector: $injector}; |
| 86 | + } |
58 | 87 | }
|
59 |
| - }; |
60 |
| - testabilities.forEach(function(testability) { |
61 |
| - testability.whenStable(decrement); |
62 |
| - }); |
63 |
| - } catch (err) { |
64 |
| - callback(err.message); |
| 88 | + } catch(err) {} |
65 | 89 | }
|
66 |
| -}; |
| 90 | + function trySelector(selector) { |
| 91 | + var els = document.querySelectorAll(selector); |
| 92 | + for (var i = 0; i < els.length; i++) { |
| 93 | + var elHooks = tryEl(els[i]); |
| 94 | + if (elHooks) { |
| 95 | + return elHooks; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + if (selector) { |
| 101 | + return trySelector(selector); |
| 102 | + } else if (window.__TESTABILITY__NG1_APP_ROOT_INJECTOR__) { |
| 103 | + var $injector = window.__TESTABILITY__NG1_APP_ROOT_INJECTOR__; |
| 104 | + var $$testability = null; |
| 105 | + try { |
| 106 | + $$testability = $injector.get('$$testability'); |
| 107 | + } catch (e) {} |
| 108 | + return {$injector: $injector, $$testability: $$testability}; |
| 109 | + } else { |
| 110 | + return tryEl(document.body) || |
| 111 | + trySelector('[ng-app]') || trySelector('[ng:app]') || |
| 112 | + trySelector('[ng-controller]') || trySelector('[ng:controller]'); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +/* Wraps a function up into a string with its helper functions so that it can |
| 117 | + * call those helper functions client side |
| 118 | + * |
| 119 | + * @param {function} fun The function to wrap up with its helpers |
| 120 | + * @param {...function} The helper functions. Each function must be named |
| 121 | + * |
| 122 | + * @return {string} The string which, when executed, will invoke fun in such a |
| 123 | + * way that it has access to its helper functions |
| 124 | + */ |
| 125 | +function wrapWithHelpers(fun) { |
| 126 | + var helpers = Array.prototype.slice.call(arguments, 1); |
| 127 | + if (!helpers.length) { |
| 128 | + return fun; |
| 129 | + } |
| 130 | + var FunClass = Function; // Get the linter to allow this eval |
| 131 | + return new FunClass( |
| 132 | + helpers.join(';') + String.fromCharCode(59) + |
| 133 | + ' return (' + fun.toString() + ').apply(this, arguments);'); |
| 134 | +} |
67 | 135 |
|
| 136 | +exports.NG_WAIT_FN = wrapWithHelpers(waitForAngular, getNg1Hooks); |
0 commit comments