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

Commit 665a05e

Browse files
authored
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 d48392c commit 665a05e

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

lib/browser.ts

Lines changed: 40 additions & 29 deletions
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

@@ -252,13 +258,7 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver {
252258
* @type {boolean}
253259
*/
254260
set ignoreSynchronization(value) {
255-
this.driver.controlFlow().execute(() => {
256-
if (this.bpClient) {
257-
logger.debug('Setting waitForAngular' + value);
258-
this.bpClient.setSynchronization(!value);
259-
}
260-
}, `Set proxy synchronization to ${value}`);
261-
this.internalIgnoreSynchronization = value;
261+
this.waitForAngularEnabled(!value);
262262
}
263263

264264
get ignoreSynchronization() {
@@ -449,11 +449,20 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver {
449449
* Call waitForAngularEnabled() without passing a value to read the current
450450
* state without changing it.
451451
*/
452-
waitForAngularEnabled(enabled: boolean = null): boolean {
452+
waitForAngularEnabled(enabled: boolean = null): wdpromise.Promise<boolean> {
453453
if (enabled != null) {
454-
this.ignoreSynchronization = !enabled;
454+
const ret = this.driver.controlFlow().execute(() => {
455+
if (this.bpClient) {
456+
logger.debug('Setting waitForAngular' + !enabled);
457+
return this.bpClient.setSynchronization(enabled).then(() => {
458+
return enabled;
459+
});
460+
}
461+
}, `Set proxy synchronization enabled to ${enabled}`);
462+
this.internalIgnoreSynchronization = !enabled;
463+
return ret;
455464
}
456-
return !this.ignoreSynchronization;
465+
return wdpromise.when(!this.ignoreSynchronization);
457466
}
458467

459468
/**
@@ -659,7 +668,9 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver {
659668

660669
let runWaitForAngularScript: () => wdpromise.Promise<any> = () => {
661670
if (this.plugins_.skipAngularStability() || this.bpClient) {
662-
return wdpromise.when(null);
671+
return this.driver.controlFlow().execute(() => {
672+
return wdpromise.when(null);
673+
}, 'bpClient or plugin stability override');
663674
} else {
664675
return this.executeAsyncScript_(
665676
clientSideScripts.waitForAngular, 'Protractor.waitForAngular()' + description,
@@ -1053,15 +1064,15 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver {
10531064
* page has been changed.
10541065
*/
10551066
setLocation(url: string): wdpromise.Promise<any> {
1056-
this.waitForAngular();
1057-
return this
1058-
.executeScriptWithDescription(
1059-
clientSideScripts.setLocation, 'Protractor.setLocation()', this.rootEl, url)
1060-
.then((browserErr: Error) => {
1061-
if (browserErr) {
1062-
throw 'Error while navigating to \'' + url + '\' : ' + JSON.stringify(browserErr);
1063-
}
1064-
});
1067+
return this.waitForAngular().then(
1068+
() => this.executeScriptWithDescription(
1069+
clientSideScripts.setLocation, 'Protractor.setLocation()', this.rootEl, url)
1070+
.then((browserErr: Error) => {
1071+
if (browserErr) {
1072+
throw 'Error while navigating to \'' + url + '\' : ' +
1073+
JSON.stringify(browserErr);
1074+
}
1075+
}));
10651076
}
10661077

10671078
/**
@@ -1075,9 +1086,9 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver {
10751086
* AngularJS.
10761087
*/
10771088
getLocationAbsUrl(): wdpromise.Promise<any> {
1078-
this.waitForAngular();
1079-
return this.executeScriptWithDescription(
1080-
clientSideScripts.getLocationAbsUrl, 'Protractor.getLocationAbsUrl()', this.rootEl);
1089+
return this.waitForAngular().then(
1090+
() => this.executeScriptWithDescription(
1091+
clientSideScripts.getLocationAbsUrl, 'Protractor.getLocationAbsUrl()', this.rootEl));
10811092
}
10821093

10831094
/**
@@ -1102,10 +1113,10 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver {
11021113
*/
11031114
debugger() {
11041115
// jshint debug: true
1105-
this.driver.executeScript(clientSideScripts.installInBrowser);
1106-
wdpromise.controlFlow().execute(() => {
1107-
debugger;
1108-
}, 'add breakpoint to control flow');
1116+
return this.driver.executeScript(clientSideScripts.installInBrowser)
1117+
.then(() => wdpromise.controlFlow().execute(() => {
1118+
debugger;
1119+
}, 'add breakpoint to control flow'));
11091120
}
11101121

11111122
/**

lib/clientsidescripts.js

Lines changed: 2 additions & 2 deletions
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)