Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit 33393ca

Browse files
committed
feat(browser): chain some promises in lib/browser.ts + return promise from waitForAngularEnabled (#4021)
Minor breaking change since `waitForAngularEnabled` no longer returns a boolean Part of #3904 Chaining `browser.get` has proved surprisingly complex, so I'll do that in a different PR Also fixed a minor bug in `lib/clientsidescripts.js` while debuging
1 parent af635fa commit 33393ca

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

lib/browser.ts

+40-29
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,16 @@ function ptorMixin(to: any, from: any, fnName: string, setupFn?: Function) {
134134
arguments[i] = arguments[i].getWebElement();
135135
}
136136
}
137+
const run = () => {
138+
return from[fnName].apply(from, arguments);
139+
};
137140
if (setupFn) {
138-
setupFn();
141+
const setupResult = setupFn();
142+
if (setupResult && (typeof setupResult.then === 'function')) {
143+
return setupResult.then(run);
144+
}
139145
}
140-
return from[fnName].apply(from, arguments);
146+
return run();
141147
};
142148
};
143149

@@ -287,13 +293,7 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver {
287293
* @type {boolean}
288294
*/
289295
set ignoreSynchronization(value) {
290-
this.driver.controlFlow().execute(() => {
291-
if (this.bpClient) {
292-
logger.debug('Setting waitForAngular' + value);
293-
return this.bpClient.setWaitEnabled(!value);
294-
}
295-
}, `Set proxy synchronization to ${value}`);
296-
this.internalIgnoreSynchronization = value;
296+
this.waitForAngularEnabled(!value);
297297
}
298298

299299
get ignoreSynchronization() {
@@ -484,11 +484,20 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver {
484484
* Call waitForAngularEnabled() without passing a value to read the current
485485
* state without changing it.
486486
*/
487-
waitForAngularEnabled(enabled: boolean = null): boolean {
487+
waitForAngularEnabled(enabled: boolean = null): wdpromise.Promise<boolean> {
488488
if (enabled != null) {
489-
this.ignoreSynchronization = !enabled;
489+
const ret = this.driver.controlFlow().execute(() => {
490+
if (this.bpClient) {
491+
logger.debug('Setting waitForAngular' + !enabled);
492+
return this.bpClient.setWaitEnabled(enabled).then(() => {
493+
return enabled;
494+
});
495+
}
496+
}, `Set proxy synchronization enabled to ${enabled}`);
497+
this.internalIgnoreSynchronization = !enabled;
498+
return ret;
490499
}
491-
return !this.ignoreSynchronization;
500+
return wdpromise.when(!this.ignoreSynchronization);
492501
}
493502

494503
/**
@@ -694,7 +703,9 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver {
694703

695704
let runWaitForAngularScript: () => wdpromise.Promise<any> = () => {
696705
if (this.plugins_.skipAngularStability() || this.bpClient) {
697-
return wdpromise.when(null);
706+
return this.driver.controlFlow().execute(() => {
707+
return wdpromise.when(null);
708+
}, 'bpClient or plugin stability override');
698709
} else {
699710
// Need to wrap this so that we read rootEl in the control flow, not synchronously.
700711
return this.angularAppRoot().then((rootEl: string) => {
@@ -1091,15 +1102,15 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver {
10911102
* page has been changed.
10921103
*/
10931104
setLocation(url: string): wdpromise.Promise<any> {
1094-
this.waitForAngular();
1095-
return this
1096-
.executeScriptWithDescription(
1097-
clientSideScripts.setLocation, 'Protractor.setLocation()', this.rootEl, url)
1098-
.then((browserErr: Error) => {
1099-
if (browserErr) {
1100-
throw 'Error while navigating to \'' + url + '\' : ' + JSON.stringify(browserErr);
1101-
}
1102-
});
1105+
return this.waitForAngular().then(
1106+
() => this.executeScriptWithDescription(
1107+
clientSideScripts.setLocation, 'Protractor.setLocation()', this.rootEl, url)
1108+
.then((browserErr: Error) => {
1109+
if (browserErr) {
1110+
throw 'Error while navigating to \'' + url + '\' : ' +
1111+
JSON.stringify(browserErr);
1112+
}
1113+
}));
11031114
}
11041115

11051116
/**
@@ -1113,9 +1124,9 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver {
11131124
* AngularJS.
11141125
*/
11151126
getLocationAbsUrl(): wdpromise.Promise<any> {
1116-
this.waitForAngular();
1117-
return this.executeScriptWithDescription(
1118-
clientSideScripts.getLocationAbsUrl, 'Protractor.getLocationAbsUrl()', this.rootEl);
1127+
return this.waitForAngular().then(
1128+
() => this.executeScriptWithDescription(
1129+
clientSideScripts.getLocationAbsUrl, 'Protractor.getLocationAbsUrl()', this.rootEl));
11191130
}
11201131

11211132
/**
@@ -1140,10 +1151,10 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver {
11401151
*/
11411152
debugger() {
11421153
// jshint debug: true
1143-
this.driver.executeScript(clientSideScripts.installInBrowser);
1144-
wdpromise.controlFlow().execute(() => {
1145-
debugger;
1146-
}, 'add breakpoint to control flow');
1154+
return this.driver.executeScript(clientSideScripts.installInBrowser)
1155+
.then(() => wdpromise.controlFlow().execute(() => {
1156+
debugger;
1157+
}, 'add breakpoint to control flow'));
11471158
}
11481159

11491160
/**

lib/clientsidescripts.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ function getNg1Hooks(selector, injectorPlease) {
109109
return {$injector: $injector, $$testability: $$testability};
110110
} else {
111111
return tryEl(document.body) ||
112-
trySelector('[ng-app]') || trySelector('[ng:app]') ||
113-
trySelector('[ng-controller]') || trySelector('[ng:controller]');
112+
trySelector('[ng-app]') || trySelector('[ng\\:app]') ||
113+
trySelector('[ng-controller]') || trySelector('[ng\\:controller]');
114114
}
115115
}
116116

0 commit comments

Comments
 (0)