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

Commit

Permalink
deps(selenium-webdriver): clean up types selenium-webdriver (#3854)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnishina authored Dec 21, 2016
1 parent 989759a commit c437fd3
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 84 deletions.
8 changes: 5 additions & 3 deletions lib/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {Logger} from './logger';
import {Plugins} from './plugins';

const clientSideScripts = require('./clientsidescripts');
const webdriver = require('selenium-webdriver');
// 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;
Expand All @@ -23,11 +22,14 @@ const DEFAULT_GET_PAGE_TIMEOUT = 10000;

let logger = new Logger('protractor');

// TODO(cnishina): either remove for loop entirely since this does not export anything
// the user might need since everything is composed (with caveat that this could be a
// potential breaking change) or export the types with `export * from 'selenium-webdriver'`;
/*
* Mix in other webdriver functionality to be accessible via protractor.
*/
for (let foo in webdriver) {
exports[foo] = webdriver[foo];
for (let foo in require('selenium-webdriver')) {
exports[foo] = require('selenium-webdriver')[foo];
}

// Explicitly define webdriver.WebDriver
Expand Down
20 changes: 9 additions & 11 deletions lib/debugger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ declare var global: any;
declare var process: any;

let logger = new Logger('protractor');
const webdriver = require('selenium-webdriver');

export class DebugHelper {
/**
Expand Down Expand Up @@ -40,7 +39,7 @@ export class DebugHelper {
* process.
*/
init(debuggerClientPath: string, onStartFn: Function, opt_debugPort?: number) {
webdriver.promise.ControlFlow.prototype.getControlFlowText = function() {
(wdpromise.ControlFlow as any).prototype.getControlFlowText = function() {
let controlFlowText = this.getSchedule(/* opt_includeStackTraces */ true);
// This filters the entire control flow text, not just the stack trace, so
// unless we maintain a good (i.e. non-generic) set of keywords in
Expand All @@ -52,8 +51,8 @@ export class DebugHelper {
return helper.filterStackTrace(controlFlowText);
};

let vm_ = require('vm');
let flow = webdriver.promise.controlFlow();
const vm_ = require('vm');
let flow = wdpromise.controlFlow();

interface Context {
require: any;
Expand All @@ -76,7 +75,7 @@ export class DebugHelper {
}
let sandbox = vm_.createContext(context);

let debuggerReadyPromise = webdriver.promise.defer();
let debuggerReadyPromise = wdpromise.defer();
flow.execute(() => {
process['debugPort'] = opt_debugPort || process['debugPort'];
this.validatePortAvailability_(process['debugPort']).then((firstTime: boolean) => {
Expand Down Expand Up @@ -161,8 +160,8 @@ export class DebugHelper {
// Run code through vm so that we can maintain a local scope which is
// isolated from the rest of the execution.
let res = vm_.runInContext(code, sandbox);
if (!webdriver.promise.isPromise(res)) {
res = webdriver.promise.fulfilled(res);
if (!wdpromise.isPromise(res)) {
res = wdpromise.fulfilled(res);
}

return res.then((res: any) => {
Expand All @@ -171,8 +170,7 @@ export class DebugHelper {
} else {
// The '' forces res to be expanded into a string instead of just
// '[Object]'. Then we remove the extra space caused by the ''
// using
// substring.
// using substring.
return util.format.apply(this, ['', res]).substring(1);
}
});
Expand All @@ -184,7 +182,7 @@ export class DebugHelper {
// Result is a JSON representation of the autocomplete response.
complete: function(line: string) {
let execFn_ = () => {
let deferred = webdriver.promise.defer();
let deferred = wdpromise.defer();
this.replServer_.complete(line, (err: any, res: any) => {
if (err) {
deferred.reject(err);
Expand Down Expand Up @@ -234,7 +232,7 @@ export class DebugHelper {
return wdpromise.fulfilled(false);
}

let doneDeferred = webdriver.promise.defer();
let doneDeferred = wdpromise.defer();

// Resolve doneDeferred if port is available.
let tester = net.connect({port: port}, () => {
Expand Down
2 changes: 1 addition & 1 deletion lib/driverProviders/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class Local extends DriverProvider {
throw new BrowserError(
logger,
'Could not find chromedriver at ' + this.config_.chromeDriver +
'. Run \'webdriver-manager update\' to download binaries.'););
'. Run \'webdriver-manager update\' to download binaries.');
}
}
}
Expand Down
38 changes: 16 additions & 22 deletions lib/element.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import {By, error, ILocation, ISize, promise as wdpromise, WebDriver, WebElement, WebElementPromise} from 'selenium-webdriver';
import {By, error as wderror, ILocation, ISize, promise as wdpromise, WebDriver, WebElement, WebElementPromise} from 'selenium-webdriver';

import {ElementHelper, ProtractorBrowser} from './browser';
import {IError} from './exitCodes';
import {Locator} from './locators';
import {Logger} from './logger';

let webdriver = require('selenium-webdriver');
let clientSideScripts = require('./clientsidescripts');

let logger = new Logger('element');
Expand Down Expand Up @@ -239,7 +238,7 @@ export class ElementArrayFinder extends WebdriverWebElement {

return filterFn(elementFinder, index);
});
return webdriver.promise.all(list).then((resolvedList: any) => {
return wdpromise.all(list).then((resolvedList: any) => {
return parentWebElements.filter((parentWebElement: WebElement, index: number) => {
return resolvedList[index];
});
Expand Down Expand Up @@ -276,21 +275,16 @@ export class ElementArrayFinder extends WebdriverWebElement {
* @param {number|webdriver.promise.Promise} index Element index.
* @returns {ElementFinder} finder representing element at the given index.
*/
get(index: number): ElementFinder {
let getWebElements = () => {
return webdriver.promise.all([index, this.getWebElements()]).then((results: any) => {
let i = results[0];
let parentWebElements = results[1];

get(index: number|wdpromise.Promise<number>): ElementFinder {
let getWebElements = (): wdpromise.Promise<WebElement[]> => {
return wdpromise.all<any>([index, this.getWebElements()]).then(([i, parentWebElements]) => {
if (i < 0) {
// wrap negative indices
i = parentWebElements.length + i;
i += parentWebElements.length;
}
if (i < 0 || i >= parentWebElements.length) {
throw new error.NoSuchElementError(
'Index out of bound. ' +
'Trying to access element at index: ' + index + ', but there are ' +
'only ' + parentWebElements.length + ' elements that match ' +
throw new wderror.NoSuchElementError(
'Index out of bound. Trying to access element at index: ' + index +
', but there are only ' + parentWebElements.length + ' elements that match ' +
'locator ' + this.locator_.toString());
}
return [parentWebElements[i]];
Expand Down Expand Up @@ -383,7 +377,7 @@ export class ElementArrayFinder extends WebdriverWebElement {
* array of the located {@link webdriver.WebElement}s.
*/
$$(selector: string): ElementArrayFinder {
return this.all(webdriver.By.css(selector));
return this.all(By.css(selector));
}

/**
Expand Down Expand Up @@ -427,7 +421,7 @@ export class ElementArrayFinder extends WebdriverWebElement {
return arr.length;
},
(err: IError) => {
if (err.code && err.code == new webdriver.error.NoSuchElementError()) {
if (err.code && err.code == (new wderror.NoSuchSessionError() as any).code) {
return 0;
} else {
throw err;
Expand Down Expand Up @@ -821,7 +815,7 @@ export class ElementFinder extends WebdriverWebElement {
let getWebElements = (): wdpromise.Promise<WebElement[]> => {
return elementArrayFinder.getWebElements().then((webElements: WebElement[]) => {
if (webElements.length === 0) {
throw new error.NoSuchElementError(
throw new wderror.NoSuchElementError(
'No element found using locator: ' + elementArrayFinder.locator().toString());
} else {
if (webElements.length > 1) {
Expand Down Expand Up @@ -999,7 +993,7 @@ export class ElementFinder extends WebdriverWebElement {
* @returns {ElementArrayFinder}
*/
$$(selector: string): ElementArrayFinder {
return this.all(webdriver.By.css(selector));
return this.all(By.css(selector));
}

/**
Expand Down Expand Up @@ -1041,7 +1035,7 @@ export class ElementFinder extends WebdriverWebElement {
* @returns {ElementFinder}
*/
$(selector: string): ElementFinder {
return this.element(webdriver.By.css(selector));
return this.element(By.css(selector));
}

/**
Expand Down Expand Up @@ -1071,15 +1065,15 @@ export class ElementFinder extends WebdriverWebElement {
return true; // is present, whether it is enabled or not
},
(err: any) => {
if (err.code == webdriver.error.ErrorCode.STALE_ELEMENT_REFERENCE) {
if (err.code == (new wderror.StaleElementReferenceError() as any).code) {
return false;
} else {
throw err;
}
});
},
(err: any) => {
if (err.code == webdriver.error.ErrorCode.NO_SUCH_ELEMENT) {
if (err.code == (new wderror.NoSuchElementError() as any).code) {
return false;
} else {
throw err;
Expand Down
5 changes: 2 additions & 3 deletions lib/expectedConditions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {error as wderror} from 'selenium-webdriver';
import {ProtractorBrowser} from './browser';
import {ElementFinder} from './element';

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

/**
* Represents a library of canned expected conditions that are useful for
* protractor, especially when dealing with non-angular apps.
Expand Down Expand Up @@ -161,7 +160,7 @@ export class ProtractorExpectedConditions {
return true;
},
(err: any) => {
if (err.code == webdriver.error.ErrorCode.NO_SUCH_ALERT) {
if (err.code == (new wderror.NoSuchAlertError() as any).code) {
return false;
} else {
throw err;
Expand Down
Loading

0 comments on commit c437fd3

Please sign in to comment.