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

Commit

Permalink
chore(types): make webdriver.promise.Promise generic
Browse files Browse the repository at this point in the history
  • Loading branch information
sjelin committed Jul 6, 2016
1 parent 1e9f500 commit e65beac
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 49 deletions.
40 changes: 22 additions & 18 deletions lib/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,18 @@ for (let foo in webdriver) {
export class Webdriver {
actions: () => webdriver.ActionSequence = webdriver.WebDriver.actions;
wait:
(condition: webdriver.promise.Promise|webdriver.util.Condition|Function,
(condition: webdriver.promise.Promise<any> | webdriver.util.Condition |
Function,
opt_timeout?: number,
opt_message?:
string) => webdriver.promise.Promise = webdriver.WebDriver.wait;
sleep: (ms: number) => webdriver.promise.Promise = webdriver.WebDriver.sleep;
string) => webdriver.promise.Promise<any> = webdriver.WebDriver.wait;
sleep: (ms: number) => webdriver.promise.Promise<any> =
webdriver.WebDriver.sleep;
getCurrentUrl:
() => webdriver.promise.Promise = webdriver.WebDriver.getCurrentUrl;
getTitle: () => webdriver.promise.Promise = webdriver.WebDriver.getTitle;
() => webdriver.promise.Promise<any> = webdriver.WebDriver.getCurrentUrl;
getTitle: () => webdriver.promise.Promise<any> = webdriver.WebDriver.getTitle;
takeScreenshot:
() => webdriver.promise.Promise = webdriver.WebDriver.takeScreenshot;
() => webdriver.promise.Promise<any> = webdriver.WebDriver.takeScreenshot;
}

/**
Expand Down Expand Up @@ -188,7 +190,7 @@ export class Browser extends Webdriver {
*
* @type {q.Promise} Done when the new browser is ready for use
*/
ready: webdriver.promise.Promise;
ready: webdriver.promise.Promise<any>;

/*
* Set by the runner.
Expand Down Expand Up @@ -247,6 +249,7 @@ export class Browser extends Webdriver {
// include functions which are overridden by protractor below.
let methodsToSync = ['getCurrentUrl', 'getPageSource', 'getTitle'];


// Mix all other driver functionality into Protractor.
Object.getOwnPropertyNames(webdriver.WebDriver.prototype)
.forEach((method: string) => {
Expand Down Expand Up @@ -300,7 +303,7 @@ export class Browser extends Webdriver {
*
* @returns {webdriver.promise.Promise} A promise which resolves to the capabilities object.
*/
getProcessedConfig: () => webdriver.promise.Promise;
getProcessedConfig: () => webdriver.promise.Promise<any>;

/**
* Fork another instance of protractor for use in interactive tests.
Expand Down Expand Up @@ -346,7 +349,7 @@ export class Browser extends Webdriver {
*/
private executeScript_(
script: string|Function, description: string,
...scriptArgs: any[]): webdriver.promise.Promise {
...scriptArgs: any[]): webdriver.promise.Promise<any> {
if (typeof script === 'function') {
script = 'return (' + script + ').apply(null, arguments);';
}
Expand All @@ -372,7 +375,7 @@ export class Browser extends Webdriver {
*/
private executeAsyncScript_(
script: string|Function, description: string,
...scriptArgs: any[]): webdriver.promise.Promise {
...scriptArgs: any[]): webdriver.promise.Promise<any> {
if (typeof script === 'function') {
script = 'return (' + script + ').apply(null, arguments);';
}
Expand All @@ -394,15 +397,15 @@ export class Browser extends Webdriver {
* @returns {!webdriver.promise.Promise} A promise that will resolve to the
* scripts return value.
*/
waitForAngular(opt_description?: string): webdriver.promise.Promise {
waitForAngular(opt_description?: string): webdriver.promise.Promise<any> {
let description = opt_description ? ' - ' + opt_description : '';
if (this.ignoreSynchronization) {
return this.driver.controlFlow().execute(() => {
return true;
}, 'Ignore Synchronization Protractor.waitForAngular()');
}

let runWaitForAngularScript: () => webdriver.promise.Promise = () => {
let runWaitForAngularScript: () => webdriver.promise.Promise<any> = () => {
if (this.plugins_.skipAngularStability()) {
return webdriver.promise.fulfilled();
} else if (this.rootEl) {
Expand Down Expand Up @@ -462,7 +465,7 @@ export class Browser extends Webdriver {
errMsg +=
'\nWhile waiting for element with locator' + description;
}
let pendingTimeoutsPromise: webdriver.promise.Promise;
let pendingTimeoutsPromise: webdriver.promise.Promise<any>;
if (this.trackOutstandingTimeouts_) {
pendingTimeoutsPromise = this.executeScript_(
'return window.NG_PENDING_TIMEOUTS',
Expand Down Expand Up @@ -528,7 +531,7 @@ export class Browser 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): webdriver.promise.Promise<any> {
return this.element.all(locator).getWebElements();
}

Expand All @@ -539,7 +542,7 @@ export class Browser extends Webdriver {
* the element is present on the page.
*/
isElementPresent(locatorOrElement: webdriver.Locator|
webdriver.WebElement): webdriver.promise.Promise {
webdriver.WebElement): webdriver.promise.Promise<any> {
let element = (locatorOrElement.isPresent) ? locatorOrElement :
this.element(locatorOrElement);
return element.isPresent();
Expand Down Expand Up @@ -857,7 +860,7 @@ export class Browser 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): webdriver.promise.Promise<any> {
this.waitForAngular();
return this
.executeScript_(
Expand All @@ -880,7 +883,7 @@ export class Browser extends Webdriver {
* .toBe('http://angular.github.io/protractor/#/api');
* @returns {string} The current absolute url from AngularJS.
*/
getLocationAbsUrl(): webdriver.promise.Promise {
getLocationAbsUrl(): webdriver.promise.Promise<any> {
this.waitForAngular();
return this.executeScript_(
clientSideScripts.getLocationAbsUrl, 'Protractor.getLocationAbsUrl()',
Expand Down Expand Up @@ -924,7 +927,8 @@ export class Browser extends Webdriver {
* 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):
webdriver.promise.Promise<any> {
if (this.debuggerValidated_) {
return webdriver.promise.fulfilled(false);
}
Expand Down
62 changes: 32 additions & 30 deletions lib/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,27 @@ let WEB_ELEMENT_FUNCTIONS = [
// Explicitly define webdriver.WebElement.
export class WebdriverWebElement {
getDriver: () => webdriver.WebDriver;
getId: () => webdriver.promise.Promise;
getRawId: () => webdriver.promise.Promise;
serialize: () => webdriver.promise.Promise;
findElement: (subLocator: Locator) => webdriver.promise.Promise;
click: () => webdriver.promise.Promise;
sendKeys: (...args: (string|webdriver.promise.Promise)[]) =>
webdriver.promise.Promise;
getTagName: () => webdriver.promise.Promise;
getCssValue: (cssStyleProperty: string) => webdriver.promise.Promise;
getAttribute: (attributeName: string) => webdriver.promise.Promise;
getText: () => webdriver.promise.Promise;
getSize: () => webdriver.promise.Promise;
getLocation: () => webdriver.promise.Promise;
isEnabled: () => webdriver.promise.Promise;
isSelected: () => webdriver.promise.Promise;
submit: () => webdriver.promise.Promise;
clear: () => webdriver.promise.Promise;
isDisplayed: () => webdriver.promise.Promise;
takeScreenshot: (opt_scroll?: boolean) => webdriver.promise.Promise;
getOuterHtml: () => webdriver.promise.Promise;
getInnerHtml: () => webdriver.promise.Promise;
getId: () => webdriver.promise.Promise<any>;
getRawId: () => webdriver.promise.Promise<string>;
serialize: () => webdriver.promise.Promise<any>;
findElement: (subLocator: Locator) => webdriver.promise.Promise<any>;
click: () => webdriver.promise.Promise<void>;
sendKeys: (...args: (string|webdriver.promise.Promise<string>)[]) =>
webdriver.promise.Promise<void>;
getTagName: () => webdriver.promise.Promise<string>;
getCssValue: (cssStyleProperty: string) => webdriver.promise.Promise<string>;
getAttribute: (attributeName: string) => webdriver.promise.Promise<string>;
getText: () => webdriver.promise.Promise<string>;
getSize: () => webdriver.promise.Promise<{width: number, height: number}>;
getLocation: () => webdriver.promise.Promise<{x: number, y: number}>;
isEnabled: () => webdriver.promise.Promise<boolean>;
isSelected: () => webdriver.promise.Promise<boolean>;
submit: () => webdriver.promise.Promise<void>;
clear: () => webdriver.promise.Promise<void>;
isDisplayed: () => webdriver.promise.Promise<boolean>;
takeScreenshot: (opt_scroll?: boolean) => webdriver.promise.Promise<string>;
getOuterHtml: () => webdriver.promise.Promise<string>;
getInnerHtml: () => webdriver.promise.Promise<string>;
}

/**
Expand Down Expand Up @@ -99,7 +99,7 @@ export class ElementArrayFinder extends WebdriverWebElement {
constructor(
private browser_: Browser, getWebElements?: Function,
private locator_?: any,
public actionResults_: webdriver.promise.Promise = null) {
public actionResults_: webdriver.promise.Promise<any> = null) {
super();
this.getWebElements = getWebElements || null;

Expand Down Expand Up @@ -379,7 +379,7 @@ export class ElementArrayFinder extends WebdriverWebElement {
* @returns {!webdriver.promise.Promise} A promise which resolves to the
* number of elements matching the locator.
*/
count(): webdriver.promise.Promise {
count(): webdriver.promise.Promise<any> {
return this.getWebElements().then(
(arr: any) => { return arr.length; },
(err: any) => {
Expand Down Expand Up @@ -439,7 +439,7 @@ export class ElementArrayFinder extends WebdriverWebElement {
* @returns {Array.<ElementFinder>} Return a promise, which resolves to a list
* of ElementFinders specified by the locator.
*/
asElementFinders_(): webdriver.promise.Promise {
asElementFinders_(): webdriver.promise.Promise<any> {
return this.getWebElements().then((arr: webdriver.WebElement[]) => {
return arr.map((webElem: webdriver.WebElement) => {
return ElementFinder.fromWebElement_(
Expand Down Expand Up @@ -472,7 +472,7 @@ export class ElementArrayFinder extends WebdriverWebElement {
* @returns {!webdriver.promise.Promise} A promise which will resolve to
* an array of ElementFinders represented by the ElementArrayFinder.
*/
then(fn: Function, errorFn: Function): webdriver.promise.Promise {
then(fn: Function, errorFn: Function): webdriver.promise.Promise<any> {
if (this.actionResults_) {
return this.actionResults_.then(fn, errorFn);
} else {
Expand Down Expand Up @@ -506,7 +506,7 @@ export class ElementArrayFinder extends WebdriverWebElement {
* function has been called on all the ElementFinders. The promise will
* resolve to null.
*/
each(fn: Function): webdriver.promise.Promise {
each(fn: Function): webdriver.promise.Promise<any> {
return this.map(fn).then((): any => { return null; });
}

Expand Down Expand Up @@ -542,7 +542,7 @@ export class ElementArrayFinder extends WebdriverWebElement {
* @returns {!webdriver.promise.Promise} A promise that resolves to an array
* of values returned by the map function.
*/
map(mapFn: Function): webdriver.promise.Promise {
map(mapFn: Function): webdriver.promise.Promise<any> {
return this.asElementFinders_().then((arr: ElementFinder[]) => {
let list = arr.map((elementFinder: ElementFinder, index: number) => {
let mapResult = mapFn(elementFinder, index);
Expand Down Expand Up @@ -584,7 +584,8 @@ export class ElementArrayFinder extends WebdriverWebElement {
* @returns {!webdriver.promise.Promise} A promise that resolves to the final
* value of the accumulator.
*/
reduce(reduceFn: Function, initialValue: any): webdriver.promise.Promise {
reduce(reduceFn: Function, initialValue: any): webdriver.promise.Promise<any>
{
let valuePromise = webdriver.promise.fulfilled(initialValue);
return this.asElementFinders_().then((arr: ElementFinder[]) => {
return arr.reduce(
Expand Down Expand Up @@ -691,7 +692,8 @@ export class ElementArrayFinder extends WebdriverWebElement {
export class ElementFinder extends WebdriverWebElement {
parentElementArrayFinder: ElementArrayFinder;
elementArrayFinder_: ElementArrayFinder;
then: (fn: Function, errorFn: Function) => webdriver.promise.Promise = null;
then: (fn: Function, errorFn: Function) => webdriver.promise.Promise<any> =
null;

constructor(
private browser_: Browser, elementArrayFinder: ElementArrayFinder) {
Expand Down Expand Up @@ -1026,7 +1028,7 @@ export class ElementFinder extends WebdriverWebElement {
* @returns {!webdriver.promise.Promise.<boolean>} A promise that will be
* resolved to whether the two WebElements are equal.
*/
equals(element: any): webdriver.promise.Promise {
equals(element: any): webdriver.promise.Promise<any> {
return webdriver.WebElement.equals(
this.getWebElement(),
element.getWebElement ? element.getWebElement() : element);
Expand Down
2 changes: 1 addition & 1 deletion lib/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ declare namespace webdriver {
}

namespace promise {
interface Promise {
interface Promise<T> {
controlFlow: Function;
then: Function;
}
Expand Down

0 comments on commit e65beac

Please sign in to comment.