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

Commit 1586fe9

Browse files
committed
feat(browser): chain promises in browser.get
Part of #3904
1 parent d48392c commit 1586fe9

File tree

1 file changed

+125
-117
lines changed

1 file changed

+125
-117
lines changed

lib/browser.ts

Lines changed: 125 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -879,131 +879,139 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver {
879879
get(destination: string, timeout = this.getPageTimeout) {
880880
destination = this.baseUrl.indexOf('file://') === 0 ? this.baseUrl + destination :
881881
url.resolve(this.baseUrl, destination);
882-
let msg = (str: string) => {
883-
return 'Protractor.get(' + destination + ') - ' + str;
884-
};
885-
886-
if (this.bpClient) {
887-
this.driver.controlFlow().execute(() => {
888-
return this.bpClient.setSynchronization(false);
889-
});
890-
}
891882

892883
if (this.ignoreSynchronization) {
893-
this.driver.get(destination);
894-
return this.driver.controlFlow().execute(() => this.plugins_.onPageLoad()).then(() => {});
884+
return this.driver.get(destination)
885+
.then(() => this.driver.controlFlow().execute(() => this.plugins_.onPageLoad()))
886+
.then(() => null);
895887
}
896888

897-
let deferred = wdpromise.defer<void>();
898-
899-
this.driver.get(this.resetUrl).then(null, deferred.reject);
900-
this.executeScriptWithDescription(
901-
'window.name = "' + DEFER_LABEL + '" + window.name;' +
902-
'window.location.replace("' + destination + '");',
903-
msg('reset url'))
904-
.then(null, deferred.reject);
905-
906-
// We need to make sure the new url has loaded before
907-
// we try to execute any asynchronous scripts.
908-
this.driver
909-
.wait(
910-
() => {
911-
return this
912-
.executeScriptWithDescription('return window.location.href;', msg('get url'))
913-
.then(
914-
(url: any) => {
915-
return url !== this.resetUrl;
916-
},
917-
(err: IError) => {
918-
if (err.code == 13) {
919-
// Ignore the error, and continue trying. This is
920-
// because IE driver sometimes (~1%) will throw an
921-
// unknown error from this execution. See
922-
// https://github.com/angular/protractor/issues/841
923-
// This shouldn't mask errors because it will fail
924-
// with the timeout anyway.
925-
return false;
926-
} else {
927-
throw err;
928-
}
929-
});
930-
},
931-
timeout, 'waiting for page to load for ' + timeout + 'ms')
932-
.then(null, deferred.reject);
933-
934-
this.driver.controlFlow().execute(() => {
935-
return this.plugins_.onPageLoad();
936-
});
889+
let msg = (str: string) => {
890+
return 'Protractor.get(' + destination + ') - ' + str;
891+
};
937892

938-
// Make sure the page is an Angular page.
939-
this.executeAsyncScript_(
940-
clientSideScripts.testForAngular, msg('test for angular'), Math.floor(timeout / 1000),
941-
this.ng12Hybrid)
942-
.then(
943-
(angularTestResult: {ver: number, message: string}) => {
944-
let angularVersion = angularTestResult.ver;
945-
if (!angularVersion) {
946-
let message = angularTestResult.message;
947-
logger.error(`Could not find Angular on page ${destination} : ${message}`);
948-
throw new Error(
949-
`Angular could not be found on the page ${destination}. If this is not an ` +
950-
`Angular application, you may need to turn off waiting for Angular. Please ` +
951-
`see https://github.com/angular/protractor/blob/master/docs/timeouts.md#waiting-for-angular-on-page-load`);
952-
}
953-
return angularVersion;
954-
},
955-
(err: Error) => {
956-
throw new Error('Error while running testForAngular: ' + err.message);
957-
})
958-
.then(loadMocks, deferred.reject);
959-
960-
let self = this;
961-
function loadMocks(angularVersion: number) {
962-
if (angularVersion === 1) {
963-
// At this point, Angular will pause for us until angular.resumeBootstrap is called.
964-
let moduleNames: string[] = [];
965-
for (const {name, script, args} of self.mockModules_) {
966-
moduleNames.push(name);
967-
let executeScriptArgs = [script, msg('add mock module ' + name), ...args];
968-
self.executeScriptWithDescription.apply(self, executeScriptArgs)
893+
return this.driver.controlFlow()
894+
.execute(() => {
895+
return wdpromise.when(null);
896+
})
897+
.then(() => {
898+
if (this.bpClient) {
899+
return this.driver.controlFlow().execute(() => {
900+
return this.bpClient.setSynchronization(false);
901+
});
902+
}
903+
})
904+
.then(() => {
905+
// Go to reset url
906+
return this.driver.get(this.resetUrl);
907+
})
908+
.then(() => {
909+
// Set defer label and navigate
910+
return this.executeScriptWithDescription(
911+
'window.name = "' + DEFER_LABEL + '" + window.name;' +
912+
'window.location.replace("' + destination + '");',
913+
msg('reset url'));
914+
})
915+
.then(() => {
916+
// We need to make sure the new url has loaded before
917+
// we try to execute any asynchronous scripts.
918+
return this.driver.wait(() => {
919+
return this.executeScriptWithDescription('return window.location.href;', msg('get url'))
920+
.then(
921+
(url: any) => {
922+
return url !== this.resetUrl;
923+
},
924+
(err: IError) => {
925+
if (err.code == 13) {
926+
// Ignore the error, and continue trying. This is
927+
// because IE driver sometimes (~1%) will throw an
928+
// unknown error from this execution. See
929+
// https://github.com/angular/protractor/issues/841
930+
// This shouldn't mask errors because it will fail
931+
// with the timeout anyway.
932+
return false;
933+
} else {
934+
throw err;
935+
}
936+
});
937+
}, timeout, 'waiting for page to load for ' + timeout + 'ms');
938+
})
939+
.then(() => {
940+
// Run Plugins
941+
return this.driver.controlFlow().execute(() => {
942+
return this.plugins_.onPageLoad();
943+
});
944+
})
945+
.then(() => {
946+
// Make sure the page is an Angular page.
947+
return this
948+
.executeAsyncScript_(
949+
clientSideScripts.testForAngular, msg('test for angular'),
950+
Math.floor(timeout / 1000), this.ng12Hybrid)
969951
.then(
970-
null,
952+
(angularTestResult: {ver: number, message: string}) => {
953+
let angularVersion = angularTestResult.ver;
954+
if (!angularVersion) {
955+
let message = angularTestResult.message;
956+
logger.error(`Could not find Angular on page ${destination} : ${message}`);
957+
throw new Error(
958+
'Angular could not be found on the page ${destination}.' +
959+
`If this is not an Angular application, you may need to turn off waiting for Angular.
960+
Please see
961+
https://github.com/angular/protractor/blob/master/docs/timeouts.md#waiting-for-angular-on-page-load`);
962+
}
963+
return angularVersion;
964+
},
971965
(err: Error) => {
972-
throw new Error(
973-
'Error while running module script ' + name + ': ' + err.message);
974-
})
975-
.then(null, deferred.reject);
976-
}
977-
978-
self.executeScriptWithDescription(
979-
'window.__TESTABILITY__NG1_APP_ROOT_INJECTOR__ = ' +
980-
'angular.resumeBootstrap(arguments[0]);',
981-
msg('resume bootstrap'), moduleNames)
982-
.then(null, deferred.reject);
983-
} else {
984-
// TODO: support mock modules in Angular2. For now, error if someone
985-
// has tried to use one.
986-
if (self.mockModules_.length > 1) {
987-
deferred.reject(
988-
'Trying to load mock modules on an Angular2 app ' +
989-
'is not yet supported.');
990-
}
991-
}
992-
}
993-
994-
if (this.bpClient) {
995-
this.driver.controlFlow().execute(() => {
996-
return this.bpClient.setSynchronization(!this.internalIgnoreSynchronization);
997-
});
998-
}
999-
1000-
this.driver.controlFlow().execute(() => {
1001-
return this.plugins_.onPageStable().then(() => {
1002-
deferred.fulfill();
1003-
}, deferred.reject);
1004-
});
966+
throw new Error('Error while running testForAngular: ' + err.message);
967+
});
1005968

1006-
return deferred.promise;
969+
})
970+
.then((angularVersion) => {
971+
// Load Angular Mocks
972+
if (angularVersion === 1) {
973+
// At this point, Angular will pause for us until angular.resumeBootstrap is called.
974+
let moduleNames: string[] = [];
975+
let modulePromise: wdpromise.Promise<void> = wdpromise.when(null);
976+
for (const {name, script, args} of this.mockModules_) {
977+
moduleNames.push(name);
978+
let executeScriptArgs = [script, msg('add mock module ' + name), ...args];
979+
modulePromise = modulePromise.then(
980+
() => this.executeScriptWithDescription.apply(this, executeScriptArgs)
981+
.then(null, (err: Error) => {
982+
throw new Error(
983+
'Error while running module script ' + name + ': ' + err.message);
984+
}));
985+
}
986+
987+
return modulePromise.then(
988+
() => this.executeScriptWithDescription(
989+
'window.__TESTABILITY__NG1_APP_ROOT_INJECTOR__ = ' +
990+
'angular.resumeBootstrap(arguments[0]);',
991+
msg('resume bootstrap'), moduleNames));
992+
} else {
993+
// TODO: support mock modules in Angular2. For now, error if someone
994+
// has tried to use one.
995+
if (this.mockModules_.length > 1) {
996+
throw 'Trying to load mock modules on an Angular2 app is not yet supported.';
997+
}
998+
}
999+
})
1000+
.then(() => {
1001+
// Reset bpClient sync
1002+
if (this.bpClient) {
1003+
return this.driver.controlFlow().execute(() => {
1004+
return this.bpClient.setSynchronization(!this.internalIgnoreSynchronization);
1005+
});
1006+
}
1007+
})
1008+
.then(() => {
1009+
// Run Plugins
1010+
return this.driver.controlFlow().execute(() => {
1011+
return this.plugins_.onPageStable();
1012+
});
1013+
})
1014+
.then(() => null);
10071015
}
10081016

10091017
/**

0 commit comments

Comments
 (0)