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

Commit

Permalink
chore(types): fix types to use not @types/selenium-webdriver (#5127)
Browse files Browse the repository at this point in the history
- Remove the USE_PROMISE_MANAGER test in spec/ts/basic
- Remove the check if we are using the control flow or not
  • Loading branch information
cnishina authored Jan 25, 2019
1 parent 84cdc50 commit 0a2809e
Show file tree
Hide file tree
Showing 42 changed files with 2,844 additions and 3,841 deletions.
7 changes: 6 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ gulp.task('built:copy', () => {
.pipe(gulp.dest('built/'));
});

gulp.task('built:copy:typings', () => {
return gulp.src(['lib/selenium-webdriver/**/*.d.ts'])
.pipe(gulp.dest('built/selenium-webdriver/'));
});

gulp.task('webdriver:update', (done) => {
runSpawn(done, 'node', ['bin/webdriver-manager', 'update']);
});
Expand All @@ -88,7 +93,7 @@ gulp.task('prepublish', gulp.series('checkVersion', 'tsc', 'built:copy'));
gulp.task('pretest', gulp.series(
'checkVersion',
gulp.parallel('webdriver:update', 'tslint', 'format'),
'tsc', 'built:copy', 'tsc:spec'));
'tsc', 'built:copy', 'built:copy:typings', 'tsc:spec'));

gulp.task('default', gulp.series('prepublish'));

72 changes: 18 additions & 54 deletions lib/browser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {BPClient} from 'blocking-proxy';
import {By, Command as WdCommand, ICommandName, Navigation, promise as wdpromise, Session, WebDriver, WebElement, WebElementPromise} from 'selenium-webdriver';
import {By, Navigation, WebDriver, WebElement, WebElementPromise} from 'selenium-webdriver';
import {Command, ICommandName} from 'selenium-webdriver/lib/command';
import * as url from 'url';
import {extend as extendWD, ExtendedWebDriver} from 'webdriver-js-extender';

const CommandName = require('selenium-webdriver/lib/command').Name as ICommandName;

import {build$, build$$, ElementArrayFinder, ElementFinder} from './element';
import {IError} from './exitCodes';
Expand All @@ -11,9 +13,6 @@ import {Logger} from './logger';
import {Plugins} from './plugins';

const clientSideScripts = require('./clientsidescripts');
// TODO: fix the typings for selenium-webdriver/lib/command
const Command = require('selenium-webdriver/lib/command').Command as typeof WdCommand;
const CommandName = require('selenium-webdriver/lib/command').Name as ICommandName;

// jshint browser: true

Expand All @@ -33,15 +32,6 @@ for (let foo in require('selenium-webdriver')) {
exports[foo] = require('selenium-webdriver')[foo];
}


// Explicitly define types for webdriver.WebDriver and ExtendedWebDriver.
// We do this because we use composition over inheritance to implement polymorphism, and therefore
// we don't want to inherit WebDriver's constructor.
export class AbstractWebDriver {}
export interface AbstractWebDriver extends WebDriver {}
export class AbstractExtendedWebDriver extends AbstractWebDriver {}
export interface AbstractExtendedWebDriver extends ExtendedWebDriver {}

/**
* Mix a function from one object onto another. The function will still be
* called in the context of the original object. Any arguments of type
Expand Down Expand Up @@ -109,7 +99,7 @@ function buildElementHelper(browser: ProtractorBrowser): ElementHelper {
* @param {boolean=} opt_untrackOutstandingTimeouts Whether Protractor should
* stop tracking outstanding $timeouts.
*/
export class ProtractorBrowser extends AbstractExtendedWebDriver {
export class ProtractorBrowser {
/**
* @type {ProtractorBy}
*/
Expand All @@ -121,12 +111,11 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver {
ExpectedConditions: ProtractorExpectedConditions;

/**
* The wrapped webdriver instance. Use this to interact with pages that do
* not contain Angular (such as a log-in screen).
* The browser's WebDriver instance
*
* @type {webdriver_extensions.ExtendedWebDriver}
* @type {webdriver.WebDriver}
*/
driver: ExtendedWebDriver;
driver: WebDriver;

/**
* The client used to control the BlockingProxy. If unset, BlockingProxy is
Expand Down Expand Up @@ -278,8 +267,7 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver {
* Information about mock modules that will be installed during every
* get().
*
* @type {Array<{name: string, script: function|string, args:
* Array.<string>}>}
* @type {Array<{name: string, script: function|string, args: Array.<string>}>}
*/
mockModules_: {name: string, script: string|Function, args: any[]}[];

Expand All @@ -304,32 +292,23 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver {
constructor(
webdriverInstance: WebDriver, opt_baseUrl?: string, opt_rootElement?: string|Promise<string>,
opt_untrackOutstandingTimeouts?: boolean, opt_blockingProxyUrl?: string) {
super();
// These functions should delegate to the webdriver instance, but should
// wait for Angular to sync up before performing the action. This does not
// include functions which are overridden by protractor below.
let methodsToSync = ['getCurrentUrl', 'getPageSource', 'getTitle'];
let extendWDInstance: ExtendedWebDriver;
try {
extendWDInstance = extendWD(webdriverInstance);
} catch (e) {
// Probably not a driver that can be extended (e.g. gotten using
// `directConnect: true` in the config)
extendWDInstance = webdriverInstance as ExtendedWebDriver;
}

// Mix all other driver functionality into Protractor.
Object.getOwnPropertyNames(WebDriver.prototype).forEach(method => {
if (!this[method] && typeof(extendWDInstance as any)[method] === 'function') {
if (!this[method] && typeof(webdriverInstance as any)[method] === 'function') {
if (methodsToSync.indexOf(method) !== -1) {
ptorMixin(this, extendWDInstance, method, this.waitForAngular.bind(this));
ptorMixin(this, webdriverInstance, method, this.waitForAngular.bind(this));
} else {
ptorMixin(this, extendWDInstance, method);
ptorMixin(this, webdriverInstance, method);
}
}
});

this.driver = extendWDInstance;
this.driver = webdriverInstance;
if (opt_blockingProxyUrl) {
logger.info('Starting BP client for ' + opt_blockingProxyUrl);
this.bpClient = new BPClient(opt_blockingProxyUrl);
Expand Down Expand Up @@ -490,10 +469,9 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver {
}

// TODO(selenium4): schedule does not exist on driver. Should use execute instead.
return (this.driver as any)
.execute(new Command(CommandName.EXECUTE_SCRIPT)
.setParameter('script', script)
.setParameter('args', scriptArgs));
return this.driver.execute((new Command(CommandName.EXECUTE_SCRIPT) as Command)
.setParameter('script', script)
.setParameter('args', scriptArgs));
}

/**
Expand Down Expand Up @@ -620,7 +598,7 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver {
}

/**
* Waits for Angular to finish rendering before searching for elements.
* Waits for Angular to finish renderActionSequenceing before searching for elements.
* @see webdriver.WebDriver.findElement
* @returns {!webdriver.WebElementPromise} A promise that will be resolved to
* the located {@link webdriver.WebElement}.
Expand Down Expand Up @@ -882,7 +860,7 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver {
* await browser.get('http://angular.github.io/protractor/#/tutorial');
* await browser.setLocation('api');
* expect(await browser.getCurrentUrl())
* .toBe('http://angular.github.io/protractor/#/api');
* .toBe('http://angular.g../../ithub.io/protractor/#/api');

This comment has been minimized.

Copy link
@jan-molak

jan-molak Mar 28, 2019

Contributor

This looks like a typo?

*
* @param {string} url In page URL using the same syntax as $location.url()
* @returns {!Promise} A promise that will resolve once
Expand Down Expand Up @@ -921,18 +899,4 @@ export class ProtractorBrowser extends AbstractExtendedWebDriver {
return await this.executeScriptWithDescription(
clientSideScripts.getLocationAbsUrl, 'Protractor.getLocationAbsUrl()', rootEl);
}

/**
* Determine if the control flow is enabled.
*
* @returns true if the control flow is enabled, false otherwise.
*/
controlFlowIsEnabled() {
if ((wdpromise as any).USE_PROMISE_MANAGER !== undefined) {
return (wdpromise as any).USE_PROMISE_MANAGER;
} else {
// True for old versions of `selenium-webdriver`, probably false in >=5.0.0
return !!wdpromise.ControlFlow;
}
}
}
10 changes: 4 additions & 6 deletions lib/driverProviders/attachSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
* it down, and setting up the driver correctly.
*/
import {Session, WebDriver} from 'selenium-webdriver';
import {Executor, HttpClient} from 'selenium-webdriver/http';

import {Config} from '../config';
import {Logger} from '../logger';

import {DriverProvider} from './driverProvider';

const http = require('selenium-webdriver/http');

let logger = new Logger('attachSession');

export class AttachSession extends DriverProvider {
Expand All @@ -36,9 +34,9 @@ export class AttachSession extends DriverProvider {
* @return {WebDriver} webdriver instance
*/
async getNewDriver(): Promise<WebDriver> {
const httpClient = new http.HttpClient(this.config_.seleniumAddress);
const executor = new http.Executor(httpClient);
const session = new Session(this.config_.seleniumSessionId, null);
const httpClient: HttpClient = new HttpClient(this.config_.seleniumAddress);
const executor: Executor = new Executor(httpClient);
const session: Session = new Session(this.config_.seleniumSessionId, null);

const newDriver = new WebDriver(session, executor);
this.drivers_.push(newDriver);
Expand Down
10 changes: 6 additions & 4 deletions lib/driverProviders/direct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
*/
import * as fs from 'fs';
import {Capabilities, WebDriver} from 'selenium-webdriver';
import {Driver as DriverForChrome, ServiceBuilder as ChromeServiceBuilder} from 'selenium-webdriver/chrome';
import {Driver as DriverForFirefox, ServiceBuilder as FirefoxServiceBuilder} from 'selenium-webdriver/firefox';
import {Driver as DriverForChrome, ServiceBuilder as ServiceBuilderForChrome} from 'selenium-webdriver/chrome';
import {Driver as DriverForFirefox, ServiceBuilder as SerivceBuilderForFirefox} from 'selenium-webdriver/firefox';

import {ChromeDriver, GeckoDriver} from 'webdriver-manager';

import {Config} from '../config';
Expand Down Expand Up @@ -73,7 +74,8 @@ export class Direct extends DriverProvider {
'. Run \'webdriver-manager update\' to download binaries.');
}

let chromeService = new ChromeServiceBuilder(chromeDriverFile).build();
const chromeService =
(new ServiceBuilderForChrome(chromeDriverFile) as ServiceBuilderForChrome).build();
driver = await DriverForChrome.createSession(
new Capabilities(this.config_.capabilities), chromeService);
break;
Expand All @@ -97,7 +99,7 @@ export class Direct extends DriverProvider {
'. Run \'webdriver-manager update\' to download binaries.');
}

let firefoxService = new FirefoxServiceBuilder(geckoDriverFile).build();
let firefoxService = new SerivceBuilderForFirefox(geckoDriverFile).build();
driver = await DriverForFirefox.createSession(
new Capabilities(this.config_.capabilities), firefoxService);
break;
Expand Down
6 changes: 3 additions & 3 deletions lib/driverProviders/driverProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* it down, and setting up the driver correctly.
*/
import {Builder, WebDriver} from 'selenium-webdriver';

import {BlockingProxyRunner} from '../bpRunner';
import {Config} from '../config';
import {BrowserError} from '../exitCodes';
import {Logger} from '../logger';

let logger = new Logger('driverProvider');

export abstract class DriverProvider {
drivers_: WebDriver[];
config_: Config;
Expand Down Expand Up @@ -43,9 +43,9 @@ export abstract class DriverProvider {
* Create a new driver.
*
* @public
* @return webdriver instance
* @return a promise to a webdriver instance
*/
async getNewDriver() {
async getNewDriver(): Promise<WebDriver> {
let builder: Builder;
if (this.config_.useBlockingProxy) {
builder =
Expand Down
2 changes: 1 addition & 1 deletion lib/driverProviders/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {DriverProvider} from './driverProvider';
let logger = new Logger('local');

export class Local extends DriverProvider {
server_: any;
server_: SeleniumServer;
constructor(config: Config) {
super(config);
this.server_ = null;
Expand Down
6 changes: 3 additions & 3 deletions lib/driverProviders/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {Config} from '../config';
import {DriverProvider} from './driverProvider';

export class MockExecutor {
execute(command: any): any {}
execute(_: any): any {}
}

export class Mock extends DriverProvider {
Expand Down Expand Up @@ -39,8 +39,8 @@ export class Mock extends DriverProvider {
* @return webdriver instance
*/
async getNewDriver(): Promise<WebDriver> {
let mockSession = new Session('test_session_id', {});
let newDriver = new WebDriver(mockSession, new MockExecutor());
const mockSession: Session = new Session('test_session_id', {});
const newDriver: WebDriver = new WebDriver(mockSession, new MockExecutor());
this.drivers_.push(newDriver);
return newDriver;
}
Expand Down
17 changes: 8 additions & 9 deletions lib/driverProviders/sauce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* it down, and setting up the driver correctly.
*/

import {Session, WebDriver} from 'selenium-webdriver';
import {WebDriver} from 'selenium-webdriver';
import * as util from 'util';

import {Config} from '../config';
Expand Down Expand Up @@ -33,14 +33,13 @@ export class Sauce extends DriverProvider {
* @return {Promise} A promise that will resolve when the update is complete.
*/
updateJob(update: any): Promise<any> {
let mappedDrivers = this.drivers_.map((driver: WebDriver) => {
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) {
throw new Error('Error updating Sauce pass/fail status: ' + util.inspect(err));
}
});
let mappedDrivers = this.drivers_.map(async (driver: WebDriver) => {
const session = await driver.getSession();
logger.info('SauceLabs results available at http://saucelabs.com/jobs/' + session.getId());
this.sauceServer_.updateJob(session.getId(), update, (err: Error) => {
if (err) {
throw new Error('Error updating Sauce pass/fail status: ' + util.inspect(err));
}
});
});
return Promise.all(mappedDrivers);
Expand Down
2 changes: 1 addition & 1 deletion lib/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ export class ElementFinder extends WebdriverWebElement {
const id = this.elementArrayFinder_.getWebElements().then((parentWebElements: WebElement[]) => {
return parentWebElements[0];
});
return new WebElementPromise(this.browser_.driver, id);
return new WebElementPromise(this.browser_.driver, id) as WebElementPromise;
}

/**
Expand Down
12 changes: 5 additions & 7 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import {ElementHelper, ProtractorBrowser} from './browser';
import {ElementArrayFinder, ElementFinder} from './element';
import {ProtractorExpectedConditions} from './expectedConditions';
import {Locator, ProtractorBy} from './locators';
import {ProtractorBy} from './locators';
import {PluginConfig, ProtractorPlugin} from './plugins';
import {Ptor} from './ptor';

// Re-export selenium-webdriver types.
// TODO(selenium4): Actions class typings missing. ActionSequence is deprecated.
export {/*Actions,*/ Browser, Builder, Button, Capabilities, Capability, error, EventEmitter, FileDetector, Key, logging, promise, Session, until, WebDriver, WebElement, WebElementPromise} from 'selenium-webdriver';
// Re-export selenium-webdriver types from typings directory.
export {Actions, Browser, Builder, Button, Capabilities, Capability, error, EventEmitter, FileDetector, Key, logging, promise, Session, until, WebDriver, WebElement, WebElementPromise} from 'selenium-webdriver';
// Re-export public types.
export {ElementHelper, ProtractorBrowser} from './browser';
export {Config} from './config';
Expand All @@ -20,14 +19,13 @@ export {Runner} from './runner';
export type PluginConfig = PluginConfig;
export type ProtractorPlugin = ProtractorPlugin;

export let utils = {
export const utils = {
firefox: require('selenium-webdriver/firefox'),
http: require('selenium-webdriver/http'),
remote: require('selenium-webdriver/remote')
};

export let Command = require('selenium-webdriver/lib/command').Command;
export let CommandName = require('selenium-webdriver/lib/command').Name;
export {Command, Name as CommandName} from 'selenium-webdriver/lib/command';

// Export API instances based on the global Protractor object.
// We base this on NodeJS `global` because we do not want to mask
Expand Down
2 changes: 1 addition & 1 deletion lib/locators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class WebdriverBy {
css: (css: string) => By = By.css;
id: (id: string) => By = By.id;
linkText: (linkText: string) => By = By.linkText;
js: (js: string|Function, ...var_args: any[]) => By = By.js;
js: (js: string|Function, ...var_args: any[]) => (webdriver: WebDriver) => Promise<any> = By.js;
name: (name: string) => By = By.name;
partialLinkText: (partialText: string) => By = By.partialLinkText;
tagName: (tagName: string) => By = By.tagName;
Expand Down
Loading

0 comments on commit 0a2809e

Please sign in to comment.