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

Commit e9061b3

Browse files
thorn0cnishina
authored andcommitted
chore(types): make plugins.ts more strongly-typed (#3685)
1 parent 9e07de0 commit e9061b3

File tree

4 files changed

+161
-188
lines changed

4 files changed

+161
-188
lines changed

lib/browser.ts

+26-44
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {ActionSequence, Capabilities, Command as WdCommand, FileDetector, Options, promise as wdpromise, Session, TargetLocator, TouchSequence, until, WebDriver, WebElement} from 'selenium-webdriver';
1+
import {ActionSequence, By, Capabilities, Command as WdCommand, FileDetector, ICommandName, Options, promise as wdpromise, Session, TargetLocator, TouchSequence, until, WebDriver, WebElement} from 'selenium-webdriver';
22
import * as url from 'url';
33

44
import {DebugHelper} from './debugger';
@@ -9,10 +9,11 @@ import {Locator, ProtractorBy} from './locators';
99
import {Logger} from './logger';
1010
import {Plugins} from './plugins';
1111

12-
let clientSideScripts = require('./clientsidescripts');
13-
let webdriver = require('selenium-webdriver');
14-
let Command = require('selenium-webdriver/lib/command').Command;
15-
let CommandName = require('selenium-webdriver/lib/command').Name;
12+
const clientSideScripts = require('./clientsidescripts');
13+
const webdriver = require('selenium-webdriver');
14+
// TODO: fix the typings for selenium-webdriver/lib/command
15+
const Command = require('selenium-webdriver/lib/command').Command as typeof WdCommand;
16+
const CommandName = require('selenium-webdriver/lib/command').Name as ICommandName;
1617

1718
// jshint browser: true
1819

@@ -244,7 +245,7 @@ export class ProtractorBrowser extends Webdriver {
244245
* @type {Array<{name: string, script: function|string, args:
245246
* Array.<string>}>}
246247
*/
247-
mockModules_: any[];
248+
mockModules_: {name: string, script: string|Function, args: any[]}[];
248249

249250
/**
250251
* If specified, start a debugger server at specified port instead of repl
@@ -279,8 +280,8 @@ export class ProtractorBrowser extends Webdriver {
279280
let methodsToSync = ['getCurrentUrl', 'getPageSource', 'getTitle'];
280281

281282
// Mix all other driver functionality into Protractor.
282-
Object.getOwnPropertyNames(webdriver.WebDriver.prototype).forEach((method: string) => {
283-
if (!this[method] && typeof(webdriverInstance as any)[method] == 'function') {
283+
Object.getOwnPropertyNames(WebDriver.prototype).forEach(method => {
284+
if (!this[method] && typeof(webdriverInstance as any)[method] === 'function') {
284285
if (methodsToSync.indexOf(method) !== -1) {
285286
ptorMixin(this, webdriverInstance, method, this.waitForAngular.bind(this));
286287
} else {
@@ -291,8 +292,8 @@ export class ProtractorBrowser extends Webdriver {
291292

292293
this.driver = webdriverInstance;
293294
this.element = buildElementHelper(this);
294-
this.$ = build$(this.element, webdriver.By);
295-
this.$$ = build$$(this.element, webdriver.By);
295+
this.$ = build$(this.element, By);
296+
this.$$ = build$$(this.element, By);
296297
this.baseUrl = opt_baseUrl || '';
297298
this.rootEl = opt_rootElement || 'body';
298299
this.ignoreSynchronization = false;
@@ -442,7 +443,7 @@ export class ProtractorBrowser extends Webdriver {
442443

443444
let runWaitForAngularScript: () => wdpromise.Promise<any> = () => {
444445
if (this.plugins_.skipAngularStability()) {
445-
return webdriver.promise.fulfilled();
446+
return wdpromise.fulfilled();
446447
} else if (this.rootEl) {
447448
return this.executeAsyncScript_(
448449
clientSideScripts.waitForAngular, 'Protractor.waitForAngular()' + description,
@@ -472,9 +473,7 @@ export class ProtractorBrowser extends Webdriver {
472473
.then(() => {
473474
return this.driver.wait(() => {
474475
return this.plugins_.waitForCondition().then((results: boolean[]) => {
475-
return results.reduce((x, y) => {
476-
return x && y;
477-
}, true);
476+
return results.reduce((x, y) => x && y, true);
478477
});
479478
}, this.allScriptsTimeout, 'Plugins.waitForCondition()');
480479
});
@@ -580,8 +579,7 @@ export class ProtractorBrowser extends Webdriver {
580579
* Add a module to load before Angular whenever Protractor.get is called.
581580
* Modules will be registered after existing modules already on the page,
582581
* so any module registered here will override preexisting modules with the
583-
* same
584-
* name.
582+
* same name.
585583
*
586584
* @example
587585
* browser.addMockModule('modName', function() {
@@ -629,9 +627,7 @@ export class ProtractorBrowser extends Webdriver {
629627
* @returns {Array.<!string|Function>} The list of mock modules.
630628
*/
631629
getRegisteredMockModules(): Array<string|Function> {
632-
return this.mockModules_.map((module) => {
633-
return module.script;
634-
});
630+
return this.mockModules_.map(module => module.script);
635631
};
636632

637633
/**
@@ -661,9 +657,7 @@ export class ProtractorBrowser extends Webdriver {
661657
* @param {number=} opt_timeout Number of milliseconds to wait for Angular to
662658
* start.
663659
*/
664-
get(destination: string, opt_timeout?: number) {
665-
let timeout = opt_timeout ? opt_timeout : this.getPageTimeout;
666-
660+
get(destination: string, timeout = this.getPageTimeout) {
667661
destination = this.baseUrl.indexOf('file://') === 0 ? this.baseUrl + destination :
668662
url.resolve(this.baseUrl, destination);
669663
let msg = (str: string) => {
@@ -672,17 +666,14 @@ export class ProtractorBrowser extends Webdriver {
672666

673667
if (this.ignoreSynchronization) {
674668
this.driver.get(destination);
675-
return this.driver.controlFlow().execute(() => {
676-
return this.plugins_.onPageLoad();
677-
});
669+
return this.driver.controlFlow().execute(() => this.plugins_.onPageLoad()).then(() => {});
678670
}
679671

680-
let deferred = webdriver.promise.defer();
672+
let deferred = wdpromise.defer<void>();
681673

682674
this.driver.get(this.resetUrl).then(null, deferred.reject);
683675
this.executeScriptWithDescription(
684676
'window.name = "' + DEFER_LABEL + '" + window.name;' +
685-
686677
'window.location.replace("' + destination + '");',
687678
msg('reset url'))
688679
.then(null, deferred.reject);
@@ -741,16 +732,11 @@ export class ProtractorBrowser extends Webdriver {
741732
let self = this;
742733
function loadMocks(angularVersion: number) {
743734
if (angularVersion === 1) {
744-
// At this point, Angular will pause for us until
745-
// angular.resumeBootstrap
746-
// is called.
735+
// At this point, Angular will pause for us until angular.resumeBootstrap is called.
747736
let moduleNames: string[] = [];
748-
for (let i = 0; i < self.mockModules_.length; ++i) {
749-
let mockModule = self.mockModules_[i];
750-
let name = mockModule.name;
737+
for (const {name, script, args} of self.mockModules_) {
751738
moduleNames.push(name);
752-
let executeScriptArgs =
753-
[mockModule.script, msg('add mock module ' + name)].concat(mockModule.args);
739+
let executeScriptArgs = [script, msg('add mock module ' + name), ...args];
754740
self.executeScriptWithDescription.apply(self, executeScriptArgs)
755741
.then(
756742
null,
@@ -776,13 +762,9 @@ export class ProtractorBrowser extends Webdriver {
776762
}
777763

778764
this.driver.controlFlow().execute(() => {
779-
return self.plugins_.onPageStable().then(
780-
() => {
781-
deferred.fulfill();
782-
},
783-
(error: Error) => {
784-
deferred.reject(error);
785-
});
765+
return this.plugins_.onPageStable().then(() => {
766+
deferred.fulfill();
767+
}, deferred.reject);
786768
});
787769

788770
return deferred.promise;
@@ -885,7 +867,7 @@ export class ProtractorBrowser extends Webdriver {
885867
debugger() {
886868
// jshint debug: true
887869
this.driver.executeScript(clientSideScripts.installInBrowser);
888-
webdriver.promise.controlFlow().execute(() => {
870+
wdpromise.controlFlow().execute(() => {
889871
debugger;
890872
}, 'add breakpoint to control flow');
891873
}
@@ -944,7 +926,7 @@ export class ProtractorBrowser extends Webdriver {
944926
pause(opt_debugPort?: number): webdriver.promise.Promise<any> {
945927
if (this.debugHelper.isAttached()) {
946928
logger.info('Encountered browser.pause(), but debugger already attached.');
947-
return webdriver.promise.fulfilled(true);
929+
return wdpromise.fulfilled(true);
948930
}
949931
let debuggerClientPath = __dirname + '/debugger/clients/wddebugger.js';
950932
let onStartFn = (firstTime: boolean) => {

lib/config.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {PluginConfig} from './plugins';
2+
13
export interface Config {
24
// ---------------------------------------------------------------------------
35
// ----- How to connect to Browser Drivers -----------------------------------
@@ -570,7 +572,7 @@ export interface Config {
570572
/**
571573
* See docs/plugins.md
572574
*/
573-
plugins?: Array<any>;
575+
plugins?: PluginConfig[];
574576

575577
/**
576578
* Turns off source map support. Stops protractor from registering global

lib/element.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {By, error, ILocation, ISize, promise as wdpromise, WebDriver, WebElement
33
import {ElementHelper} from './browser';
44
import {ProtractorBrowser} from './browser';
55
import {IError} from './exitCodes';
6-
import {Locator, ProtractorBy} from './locators';
6+
import {Locator} from './locators';
77
import {Logger} from './logger';
88

99
let webdriver = require('selenium-webdriver');
@@ -1061,7 +1061,7 @@ export class ElementFinder extends WebdriverWebElement {
10611061
* @returns {ElementFinder} which identifies the located
10621062
* {@link webdriver.WebElement}
10631063
*/
1064-
export let build$ = (element: ElementHelper, by: ProtractorBy) => {
1064+
export let build$ = (element: ElementHelper, by: typeof By) => {
10651065
return (selector: string) => {
10661066
return element(by.css(selector));
10671067
};
@@ -1092,7 +1092,7 @@ export let build$ = (element: ElementHelper, by: ProtractorBy) => {
10921092
* @returns {ElementArrayFinder} which identifies the
10931093
* array of the located {@link webdriver.WebElement}s.
10941094
*/
1095-
export let build$$ = (element: ElementHelper, by: ProtractorBy) => {
1095+
export let build$$ = (element: ElementHelper, by: typeof By) => {
10961096
return (selector: string) => {
10971097
return element.all(by.css(selector));
10981098
};

0 commit comments

Comments
 (0)