From 83694f5e66592c5e229db98af3733ff73dac8392 Mon Sep 17 00:00:00 2001 From: Craig Date: Tue, 20 Dec 2016 11:03:34 -0800 Subject: [PATCH] fix(types): update for selenium-webdriver types creating transpile errors (#3848) - pin package.json to a specific version of @types/selenium-webdriver --- lib/browser.ts | 11 ++++----- lib/debugger.ts | 25 ++++++++++---------- lib/driverProviders/attachSession.ts | 13 +++++------ lib/driverProviders/browserStack.ts | 5 ++-- lib/driverProviders/direct.ts | 21 ++++++++--------- lib/driverProviders/driverProvider.ts | 17 +++++++------- lib/driverProviders/local.ts | 9 ++++---- lib/driverProviders/mock.ts | 13 +++++++---- lib/driverProviders/sauce.ts | 7 +++--- lib/element.ts | 33 +++++++++++++-------------- lib/expectedConditions.ts | 2 +- lib/launcher.ts | 2 +- lib/locators.ts | 2 +- lib/ptor.ts | 2 +- lib/runner.ts | 3 ++- package.json | 2 +- 16 files changed, 85 insertions(+), 82 deletions(-) diff --git a/lib/browser.ts b/lib/browser.ts index 2aa658f1e..25e1da744 100644 --- a/lib/browser.ts +++ b/lib/browser.ts @@ -562,7 +562,7 @@ export class ProtractorBrowser extends Webdriver { * @returns {!webdriver.promise.Promise} A promise that will be resolved to an * array of the located {@link webdriver.WebElement}s. */ - findElements(locator: Locator): webdriver.promise.Promise { + findElements(locator: Locator): wdpromise.Promise { return this.element.all(locator).getWebElements(); } @@ -572,8 +572,7 @@ export class ProtractorBrowser extends Webdriver { * @returns {!webdriver.promise.Promise} A promise that will resolve to whether * the element is present on the page. */ - isElementPresent(locatorOrElement: ProtractorBy| - webdriver.WebElement): webdriver.promise.Promise { + isElementPresent(locatorOrElement: ProtractorBy|WebElement): wdpromise.Promise { let element = ((locatorOrElement as any).isPresent) ? locatorOrElement : this.element(locatorOrElement); return (element as any).isPresent(); @@ -823,7 +822,7 @@ export class ProtractorBrowser extends Webdriver { * @returns {!webdriver.promise.Promise} A promise that will resolve once * page has been changed. */ - setLocation(url: string): webdriver.promise.Promise { + setLocation(url: string): wdpromise.Promise { this.waitForAngular(); return this .executeScriptWithDescription( @@ -845,7 +844,7 @@ export class ProtractorBrowser extends Webdriver { * @returns {webdriver.promise.Promise} The current absolute url from * AngularJS. */ - getLocationAbsUrl(): webdriver.promise.Promise { + getLocationAbsUrl(): wdpromise.Promise { this.waitForAngular(); return this.executeScriptWithDescription( clientSideScripts.getLocationAbsUrl, 'Protractor.getLocationAbsUrl()', this.rootEl); @@ -930,7 +929,7 @@ export class ProtractorBrowser extends Webdriver { * @param {number=} opt_debugPort Optional port to use for the debugging * process */ - pause(opt_debugPort?: number): webdriver.promise.Promise { + pause(opt_debugPort?: number): wdpromise.Promise { if (this.debugHelper.isAttached()) { logger.info('Encountered browser.pause(), but debugger already attached.'); return wdpromise.fulfilled(true); diff --git a/lib/debugger.ts b/lib/debugger.ts index 3d8af4336..f2d152d69 100644 --- a/lib/debugger.ts +++ b/lib/debugger.ts @@ -1,4 +1,5 @@ import * as net from 'net'; +import {promise as wdpromise, WebElement} from 'selenium-webdriver'; import * as util from 'util' import {ProtractorBrowser} from './browser'; @@ -11,7 +12,7 @@ declare var global: any; declare var process: any; let logger = new Logger('protractor'); -let webdriver = require('selenium-webdriver'); +const webdriver = require('selenium-webdriver'); export class DebugHelper { /** @@ -60,17 +61,15 @@ export class DebugHelper { } let context: Context = {require: require}; global.list = (locator: Locator) => { - return (global.protractor) - .browser.findElements(locator) - .then((arr: webdriver.WebElement[]) => { - let found: string[] = []; - for (let i = 0; i < arr.length; ++i) { - arr[i].getText().then((text: string) => { - found.push(text); - }); - } - return found; + return (global.protractor).browser.findElements(locator).then((arr: WebElement[]) => { + let found: string[] = []; + for (let i = 0; i < arr.length; ++i) { + arr[i].getText().then((text: string) => { + found.push(text); }); + } + return found; + }); }; for (let key in global) { context[key] = global[key]; @@ -230,9 +229,9 @@ export class DebugHelper { * is done. The promise will resolve to a boolean which represents whether * this is the first time that the debugger is called. */ - private validatePortAvailability_(port: number): webdriver.promise.Promise { + private validatePortAvailability_(port: number): wdpromise.Promise { if (this.debuggerValidated_) { - return webdriver.promise.fulfilled(false); + return wdpromise.fulfilled(false); } let doneDeferred = webdriver.promise.defer(); diff --git a/lib/driverProviders/attachSession.ts b/lib/driverProviders/attachSession.ts index da933f3b5..29af052ae 100644 --- a/lib/driverProviders/attachSession.ts +++ b/lib/driverProviders/attachSession.ts @@ -4,15 +4,14 @@ * it down, and setting up the driver correctly. */ import * as q from 'q'; +import {WebDriver} from 'selenium-webdriver'; +import * as executors from 'selenium-webdriver/executors'; import {Config} from '../config'; import {Logger} from '../logger'; import {DriverProvider} from './driverProvider'; -let webdriver = require('selenium-webdriver'); -let executors = require('selenium-webdriver/executors'); - let logger = new Logger('attachSession'); export class AttachSession extends DriverProvider { @@ -38,9 +37,9 @@ export class AttachSession extends DriverProvider { * @public * @return {WebDriver} webdriver instance */ - getNewDriver(): webdriver.WebDriver { + getNewDriver(): WebDriver { var executor = executors.createExecutor(this.config_.seleniumAddress); - var newDriver = webdriver.WebDriver.attachToSession(executor, this.config_.seleniumSessionId); + var newDriver = WebDriver.attachToSession(executor, this.config_.seleniumSessionId); this.drivers_.push(newDriver); return newDriver; } @@ -50,8 +49,8 @@ export class AttachSession extends DriverProvider { * * @public */ - quitDriver(): q.Promise { - let defer = q.defer(); + quitDriver(): q.Promise { + let defer = q.defer(); defer.resolve(null); return defer.promise; } diff --git a/lib/driverProviders/browserStack.ts b/lib/driverProviders/browserStack.ts index bffd3396a..8e7d7edde 100644 --- a/lib/driverProviders/browserStack.ts +++ b/lib/driverProviders/browserStack.ts @@ -5,6 +5,7 @@ */ import * as https from 'https'; import * as q from 'q'; +import {Session, WebDriver} from 'selenium-webdriver'; import * as util from 'util'; import {Config} from '../config'; @@ -27,9 +28,9 @@ export class BrowserStack extends DriverProvider { * @return {q.promise} A promise that will resolve when the update is complete. */ updateJob(update: any): q.Promise { - let deferredArray = this.drivers_.map((driver: webdriver.WebDriver) => { + let deferredArray = this.drivers_.map((driver: WebDriver) => { let deferred = q.defer(); - driver.getSession().then((session: webdriver.Session) => { + driver.getSession().then((session: Session) => { let headers: Object = { 'Content-Type': 'application/json', 'Authorization': 'Basic ' + diff --git a/lib/driverProviders/direct.ts b/lib/driverProviders/direct.ts index a5ad011ed..20af0cf38 100644 --- a/lib/driverProviders/direct.ts +++ b/lib/driverProviders/direct.ts @@ -6,6 +6,9 @@ import * as fs from 'fs'; import * as path from 'path'; import * as q from 'q'; +import {Capabilities, WebDriver} from 'selenium-webdriver'; +import {Driver as ChromeDriver, ServiceBuilder as ChromeServiceBuilder} from 'selenium-webdriver/chrome'; +import {Driver as FirefoxDriver} from 'selenium-webdriver/firefox'; import {Config} from '../config'; import {BrowserError} from '../exitCodes'; @@ -13,12 +16,8 @@ import {Logger} from '../logger'; import {DriverProvider} from './driverProvider'; -let webdriver = require('selenium-webdriver'), chrome = require('selenium-webdriver/chrome'), - firefox = require('selenium-webdriver/firefox'); -let SeleniumConfig = require('webdriver-manager/built/lib/config').Config; -let SeleniumChrome = require('webdriver-manager/built/lib/binaries/chrome_driver').ChromeDriver; -let SeleniumStandAlone = require('webdriver-manager/built/lib/binaries/stand_alone').StandAlone; - +const SeleniumConfig = require('webdriver-manager/built/lib/config').Config; +const SeleniumChrome = require('webdriver-manager/built/lib/binaries/chrome_driver').ChromeDriver; let logger = new Logger('direct'); export class Direct extends DriverProvider { @@ -56,8 +55,8 @@ export class Direct extends DriverProvider { * @override * @return webdriver instance */ - getNewDriver(): webdriver.WebDriver { - let driver: webdriver.WebDriver; + getNewDriver(): WebDriver { + let driver: WebDriver; switch (this.config_.capabilities.browserName) { case 'chrome': let defaultChromeDriverPath = path.resolve( @@ -73,14 +72,14 @@ export class Direct extends DriverProvider { throw new BrowserError(logger, 'Could not find chromedriver at ' + chromeDriverFile); } - let service = new chrome.ServiceBuilder(chromeDriverFile).build(); - driver = new chrome.Driver(new webdriver.Capabilities(this.config_.capabilities), service); + let service = new ChromeServiceBuilder(chromeDriverFile).build(); + driver = new ChromeDriver(new Capabilities(this.config_.capabilities), service); break; case 'firefox': if (this.config_.firefoxPath) { this.config_.capabilities['firefox_binary'] = this.config_.firefoxPath; } - driver = new firefox.Driver(this.config_.capabilities); + driver = new FirefoxDriver(this.config_.capabilities); break; default: throw new BrowserError( diff --git a/lib/driverProviders/driverProvider.ts b/lib/driverProviders/driverProvider.ts index 20ce966ec..035293be5 100644 --- a/lib/driverProviders/driverProvider.ts +++ b/lib/driverProviders/driverProvider.ts @@ -4,13 +4,12 @@ * it down, and setting up the driver correctly. */ import * as q from 'q'; +import {Builder, Session, WebDriver} from 'selenium-webdriver'; import {Config} from '../config'; -let webdriver = require('selenium-webdriver'); - export class DriverProvider { - drivers_: webdriver.WebDriver[]; + drivers_: WebDriver[]; config_: Config; constructor(config: Config) { @@ -35,7 +34,7 @@ export class DriverProvider { * @return webdriver instance */ getNewDriver() { - let builder = new webdriver.Builder() + let builder = new Builder() .usingServer(this.config_.seleniumAddress) .usingWebDriverProxy(this.config_.webDriverProxy) .withCapabilities(this.config_.capabilities); @@ -53,18 +52,18 @@ export class DriverProvider { * @public * @param webdriver instance */ - quitDriver(driver: webdriver.WebDriver): q.Promise { + quitDriver(driver: WebDriver): q.Promise { let driverIndex = this.drivers_.indexOf(driver); if (driverIndex >= 0) { this.drivers_.splice(driverIndex, 1); } - let deferred = q.defer(); + let deferred = q.defer(); if (driver.getSession() === undefined) { deferred.resolve(); } else { driver.getSession() - .then((session_) => { + .then((session_: Session) => { if (session_) { driver.quit().then(function() { deferred.resolve(); @@ -104,8 +103,8 @@ export class DriverProvider { * @return {q.promise} A promise which will resolve when the environment * is down. */ - teardownEnv(): q.Promise[]> { - return q.all(this.drivers_.map((driver: webdriver.WebDriver) => { + teardownEnv(): q.Promise[]> { + return q.all(this.drivers_.map((driver: WebDriver) => { return this.quitDriver(driver); })); } diff --git a/lib/driverProviders/local.ts b/lib/driverProviders/local.ts index 45ad97318..1991ad75b 100644 --- a/lib/driverProviders/local.ts +++ b/lib/driverProviders/local.ts @@ -16,12 +16,13 @@ import {Logger} from '../logger'; import {DriverProvider} from './driverProvider'; -let SeleniumConfig = require('webdriver-manager/built/lib/config').Config; -let SeleniumChrome = require('webdriver-manager/built/lib/binaries/chrome_driver').ChromeDriver; -let SeleniumStandAlone = require('webdriver-manager/built/lib/binaries/stand_alone').StandAlone; -let remote = require('selenium-webdriver/remote'); +const SeleniumConfig = require('webdriver-manager/built/lib/config').Config; +const SeleniumChrome = require('webdriver-manager/built/lib/binaries/chrome_driver').ChromeDriver; +const SeleniumStandAlone = require('webdriver-manager/built/lib/binaries/stand_alone').StandAlone; +const remote = require('selenium-webdriver/remote'); let logger = new Logger('local'); + export class Local extends DriverProvider { server_: any; constructor(config: Config) { diff --git a/lib/driverProviders/mock.ts b/lib/driverProviders/mock.ts index f3a3b7b73..f4d02ebfd 100644 --- a/lib/driverProviders/mock.ts +++ b/lib/driverProviders/mock.ts @@ -4,10 +4,15 @@ * server. */ import * as q from 'q'; +import {Session, WebDriver} from 'selenium-webdriver'; +import * as executors from 'selenium-webdriver/executors'; + import {Config} from '../config'; import {DriverProvider} from './driverProvider'; -let webdriver = require('selenium-webdriver'); +export class MockExecutor { + execute(command: any): any {} +} export class Mock extends DriverProvider { constructor(config?: Config) { @@ -39,9 +44,9 @@ export class Mock extends DriverProvider { * @override * @return webdriver instance */ - getNewDriver(): webdriver.WebDriver { - let mockSession = new webdriver.Session('test_session_id', {}); - let newDriver = new webdriver.WebDriver(mockSession, new Mock()); + getNewDriver(): WebDriver { + let mockSession = new Session('test_session_id', {}); + let newDriver = new WebDriver(mockSession, new MockExecutor()); this.drivers_.push(newDriver); return newDriver; } diff --git a/lib/driverProviders/sauce.ts b/lib/driverProviders/sauce.ts index af3e3d5bf..a3db08a90 100644 --- a/lib/driverProviders/sauce.ts +++ b/lib/driverProviders/sauce.ts @@ -5,6 +5,7 @@ */ import * as q from 'q'; +import {Session, WebDriver} from 'selenium-webdriver'; import * as util from 'util'; import {Config} from '../config'; @@ -12,7 +13,7 @@ import {Logger} from '../logger'; import {DriverProvider} from './driverProvider'; -let SauceLabs = require('saucelabs'); +const SauceLabs = require('saucelabs'); let logger = new Logger('sauce'); export class Sauce extends DriverProvider { @@ -29,9 +30,9 @@ export class Sauce extends DriverProvider { * @return {q.promise} A promise that will resolve when the update is complete. */ updateJob(update: any): q.Promise { - var deferredArray = this.drivers_.map((driver: webdriver.WebDriver) => { + var deferredArray = this.drivers_.map((driver: WebDriver) => { var deferred = q.defer(); - driver.getSession().then((session: webdriver.Session) => { + driver.getSession().then((session: Session) => { logger.info('SauceLabs results available at http://saucelabs.com/jobs/' + session.getId()); this.sauceServer_.updateJob(session.getId(), update, (err: Error) => { if (err) { diff --git a/lib/element.ts b/lib/element.ts index 3dca28f81..06b658fcf 100644 --- a/lib/element.ts +++ b/lib/element.ts @@ -161,7 +161,7 @@ export class ElementArrayFinder extends WebdriverWebElement { if (this.getWebElements === null) { // This is the first time we are looking for an element return ptor.waitForAngular('Locator: ' + locator) - .then((): wdpromise.Promise => { + .then((): wdpromise.Promise => { if (locator.findElementsOverride) { return locator.findElementsOverride(ptor.driver, null, ptor.rootEl); } else { @@ -172,20 +172,20 @@ export class ElementArrayFinder extends WebdriverWebElement { return this.getWebElements().then((parentWebElements: WebElement[]) => { // For each parent web element, find their children and construct // a list of Promise> - let childrenPromiseList = - parentWebElements.map((parentWebElement: webdriver.WebElement) => { - return locator.findElementsOverride ? - locator.findElementsOverride(ptor.driver, parentWebElement, ptor.rootEl) : - parentWebElement.findElements(locator); - }); + let childrenPromiseList = parentWebElements.map((parentWebElement: WebElement) => { + return locator.findElementsOverride ? + locator.findElementsOverride(ptor.driver, parentWebElement, ptor.rootEl) : + parentWebElement.findElements(locator); + }); // Resolve the list of Promise> and merge // into a single list - return wdpromise.all(childrenPromiseList).then((resolved) => { - return resolved.reduce((childrenList, resolvedE) => { - return childrenList.concat(resolvedE); - }, []); - }); + return wdpromise.all(childrenPromiseList) + .then((resolved: WebElement[][]) => { + return resolved.reduce((childrenList, resolvedE) => { + return childrenList.concat(resolvedE); + }, []); + }); }); } }; @@ -470,7 +470,7 @@ export class ElementArrayFinder extends WebdriverWebElement { ElementArrayFinder { let callerError = new Error(); let actionResults = this.getWebElements() - .then(arr => webdriver.promise.all(arr.map(actionFn))) + .then((arr: any) => wdpromise.all(arr.map(actionFn))) .then(null, (e: IError | string) => { let noSuchErr: any; if (e instanceof Error) { @@ -899,10 +899,9 @@ export class ElementFinder extends WebdriverWebElement { * @returns {webdriver.WebElement} */ getWebElement(): WebElementPromise { - let id = this.elementArrayFinder_.getWebElements().then( - (parentWebElements: webdriver.WebElement[]) => { - return parentWebElements[0]; - }); + let id = this.elementArrayFinder_.getWebElements().then((parentWebElements: WebElement[]) => { + return parentWebElements[0]; + }); return new WebElementPromise(this.browser_.driver, id); } diff --git a/lib/expectedConditions.ts b/lib/expectedConditions.ts index cee398ad5..3cfef1a8b 100644 --- a/lib/expectedConditions.ts +++ b/lib/expectedConditions.ts @@ -1,7 +1,7 @@ import {ProtractorBrowser} from './browser'; import {ElementFinder} from './element'; -let webdriver = require('selenium-webdriver'); +const webdriver = require('selenium-webdriver'); declare var global: any; diff --git a/lib/launcher.ts b/lib/launcher.ts index dea64b38b..4bcbb8891 100644 --- a/lib/launcher.ts +++ b/lib/launcher.ts @@ -2,6 +2,7 @@ * The launcher is responsible for parsing the capabilities from the * input configuration and launching test runners. */ +import * as fs from 'fs'; import * as q from 'q'; import {Config} from './config'; @@ -47,7 +48,6 @@ class TaskResults { }, []); let json = JSON.stringify(jsonOutput, null, ' '); - let fs = require('fs'); fs.writeFileSync(filepath, json); } diff --git a/lib/locators.ts b/lib/locators.ts index 46b6c0db8..1b8e25017 100644 --- a/lib/locators.ts +++ b/lib/locators.ts @@ -1,6 +1,6 @@ import {By, promise as wdpromise, WebDriver, WebElement} from 'selenium-webdriver'; -let webdriver = require('selenium-webdriver'); +const webdriver = require('selenium-webdriver'); let clientSideScripts = require('./clientsidescripts'); diff --git a/lib/ptor.ts b/lib/ptor.ts index b350b4f51..d0ab492d4 100644 --- a/lib/ptor.ts +++ b/lib/ptor.ts @@ -23,7 +23,7 @@ export class Ptor { By: ProtractorBy; by: ProtractorBy; wrapDriver: - (webdriver: webdriver.WebDriver, baseUrl?: string, rootElement?: string, + (webdriver: WebDriver, baseUrl?: string, rootElement?: string, untrackOutstandingTimeouts?: boolean) => ProtractorBrowser; ExpectedConditions: ProtractorExpectedConditions; diff --git a/lib/runner.ts b/lib/runner.ts index 7ad8d2eac..d5ebcfc28 100644 --- a/lib/runner.ts +++ b/lib/runner.ts @@ -1,5 +1,6 @@ import {EventEmitter} from 'events'; import * as q from 'q'; +import {Session} from 'selenium-webdriver'; import * as util from 'util'; import {ProtractorBrowser} from './browser'; @@ -298,7 +299,7 @@ export class Runner extends EventEmitter { this.setupGlobals_(browser_); return browser_.ready.then(browser_.getSession) .then( - (session: webdriver.Session) => { + (session: Session) => { logger.debug( 'WebDriver session successfully started with capabilities ' + util.inspect(session.getCapabilities())); diff --git a/package.json b/package.json index 957df5442..f578da8b0 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "@types/jasmine": "^2.5.36", "@types/node": "^6.0.46", "@types/q": "^0.0.32", - "@types/selenium-webdriver": "~2.53.31", + "@types/selenium-webdriver": "2.53.31", "adm-zip": "0.4.7", "chalk": "^1.1.3", "glob": "^7.0.3",