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

Commit 0a26e70

Browse files
committed
chore(types): add in types inherited from selenium-webdriver
- add element.ts to the gulp task for 'gulp types' - clang clean up - separate out selenium-webdriver types and extend them in protractor - remove webdriver types for index.d.ts file
1 parent bc9995b commit 0a26e70

File tree

7 files changed

+157
-71
lines changed

7 files changed

+157
-71
lines changed

gulpfile.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ gulp.task('default',['prepublish']);
7272

7373
gulp.task('types', function(done) {
7474
var folder = 'built';
75-
var files = ['ptor', 'browser', 'locators', 'expectedConditions'];
75+
var files = ['ptor', 'browser', 'element', 'locators', 'expectedConditions'];
7676
var outputFile = path.resolve(folder, 'index.d.ts');
7777
var contents = '';
7878
files.forEach(file => {
@@ -104,8 +104,29 @@ var parseTypingsFile = function(folder, file) {
104104
if (line.indexOf('export') !== -1) {
105105
line = line.replace('export', '').trim();
106106
}
107+
108+
// Remove webdriver types and plugins for now
109+
line = removeTypes(line,'webdriver.ActionSequence');
110+
line = removeTypes(line,'webdriver.promise.Promise');
111+
line = removeTypes(line,'webdriver.util.Condition');
112+
line = removeTypes(line,'webdriver.WebDriver');
113+
line = removeTypes(line,'webdriver.Locator');
114+
line = removeTypes(line,'webdriver.WebElement');
115+
line = removeTypes(line,'Plugins');
107116
contents += line + '\n';
108117
}
118+
109119
}
110120
return contents;
111121
}
122+
123+
var removeTypes = function(line, webdriverType) {
124+
var tempLine = line.trim();
125+
if (tempLine.startsWith('/**') || tempLine.startsWith('*')) {
126+
return line;
127+
}
128+
if (line.indexOf(webdriverType) !== -1) {
129+
return line.replace(new RegExp(webdriverType,'g'), 'any');
130+
}
131+
return line;
132+
}

lib/browser.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as url from 'url';
44
import * as util from 'util';
55

66
import {ElementArrayFinder, ElementFinder, build$, build$$} from './element';
7-
import * as EC from './expectedConditions';
7+
import {ExpectedConditions_} from './expectedConditions';
88
import {Locator, ProtractorBy} from './locators';
99
import {Logger} from './logger2';
1010
import {Plugins} from './plugins';
@@ -30,6 +30,23 @@ for (let foo in webdriver) {
3030
exports[foo] = webdriver[foo];
3131
}
3232

33+
// explicitly extend selenium webdriver to define functions that
34+
// are supported per the website
35+
export class Webdriver {
36+
actions: () => webdriver.ActionSequence = webdriver.WebDriver.actions;
37+
wait:
38+
(condition: webdriver.promise.Promise|webdriver.util.Condition|Function,
39+
opt_timeout?: number,
40+
opt_message?:
41+
string) => webdriver.promise.Promise = webdriver.WebDriver.wait;
42+
sleep: (ms: number) => webdriver.promise.Promise = webdriver.WebDriver.sleep;
43+
getCurrentUrl:
44+
() => webdriver.promise.Promise = webdriver.WebDriver.getCurrentUrl;
45+
getTitle: () => webdriver.promise.Promise = webdriver.WebDriver.getTitle;
46+
takeScreenshot:
47+
() => webdriver.promise.Promise = webdriver.WebDriver.takeScreenshot;
48+
}
49+
3350
/**
3451
* Mix a function from one object onto another. The function will still be
3552
* called in the context of the original object.
@@ -85,7 +102,7 @@ function buildElementHelper(browser: Browser): ElementHelper {
85102
* @param {boolean=} opt_untrackOutstandingTimeouts Whether Protractor should
86103
* stop tracking outstanding $timeouts.
87104
*/
88-
export class Browser {
105+
export class Browser extends Webdriver {
89106
/**
90107
* @type {ProtractorBy}
91108
*/
@@ -94,7 +111,7 @@ export class Browser {
94111
/**
95112
* @type {ExpectedConditions}
96113
*/
97-
static ExpectedConditions = new EC.ExpectedConditions();
114+
static ExpectedConditions = new ExpectedConditions_();
98115

99116
/**
100117
* The wrapped webdriver instance. Use this to interact with pages that do
@@ -225,6 +242,7 @@ export class Browser {
225242
constructor(
226243
webdriverInstance: webdriver.WebDriver, opt_baseUrl?: string,
227244
opt_rootElement?: string, opt_untrackOutstandingTimeouts?: boolean) {
245+
super();
228246
// These functions should delegate to the webdriver instance, but should
229247
// wait for Angular to sync up before performing the action. This does not
230248
// include functions which are overridden by protractor below.
@@ -281,7 +299,7 @@ export class Browser {
281299
*
282300
* Set by the runner.
283301
*
284-
* @return {q.Promise} A promise which resolves to the capabilities object.
302+
* @return {webdriver.promise.Promise} A promise which resolves to the capabilities object.
285303
*/
286304
getProcessedConfig: () => webdriver.promise.Promise;
287305

lib/element.ts

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,41 @@ let WEB_ELEMENT_FUNCTIONS = [
1414
'serialize', 'takeScreenshot'
1515
];
1616

17+
// explicitly extend selenium webdriver to define functions that
18+
// are supported per the website
19+
export class WebdriverWebElement {
20+
getDriver: () => webdriver.WebDriver = webdriver.WebElement.getDriver;
21+
getId: () => webdriver.promise.Promise = webdriver.WebElement.getId;
22+
getRawId: () => webdriver.promise.Promise = webdriver.WebElement.getRawId;
23+
serialize: () => webdriver.promise.Promise = webdriver.WebElement.seralize;
24+
findElement: (subLocator: Locator) => webdriver.promise.Promise =
25+
webdriver.WebElement.findElement;
26+
click: () => webdriver.promise.Promise = webdriver.WebElement.click;
27+
sendKeys: (...args: (string|webdriver.promise.Promise)[]) =>
28+
webdriver.promise.Promise = webdriver.WebElement.sendKeys;
29+
getTagName: () => webdriver.promise.Promise = webdriver.WebElement.getTagName;
30+
getCssValue: (cssStyleProperty: string) => webdriver.promise.Promise =
31+
webdriver.WebElement.getCssValue;
32+
getAttribute: (attributeName: string) => webdriver.promise.Promise =
33+
webdriver.WebElement.getAttribute;
34+
getText: () => webdriver.promise.Promise = webdriver.WebElement.getText;
35+
getSize: () => webdriver.promise.Promise = webdriver.WebElement.getSize;
36+
getLocation:
37+
() => webdriver.promise.Promise = webdriver.WebElement.getLocation;
38+
isEnabled: () => webdriver.promise.Promise = webdriver.WebElement.isEnabled;
39+
isSelected: () => webdriver.promise.Promise = webdriver.WebElement.isSelected;
40+
submit: () => webdriver.promise.Promise = webdriver.WebElement.submit;
41+
clear: () => webdriver.promise.Promise = webdriver.WebElement.clear;
42+
isDisplayed:
43+
() => webdriver.promise.Promise = webdriver.WebElement.isDisplayed;
44+
takeScreenshot: (opt_scroll?: boolean) => webdriver.promise.Promise =
45+
webdriver.WebElement.takeScreenshot;
46+
getOuterHtml:
47+
() => webdriver.promise.Promise = webdriver.WebElement.getOuterHtml;
48+
getInnerHtml:
49+
() => webdriver.promise.Promise = webdriver.WebElement.getInnerHtml;
50+
}
51+
1752
/**
1853
* ElementArrayFinder is used for operations on an array of elements (as opposed
1954
* to a single element).
@@ -379,7 +414,7 @@ export class ElementArrayFinder {
379414
*
380415
* @return {webdriver.Locator}
381416
*/
382-
locator(): any { return this.locator_; }
417+
locator(): Locator { return this.locator_; }
383418

384419
/**
385420
* Apply an action function to every element in the ElementArrayFinder,
@@ -661,12 +696,14 @@ export class ElementArrayFinder {
661696
* that this is branched from.
662697
* @return {ElementFinder}
663698
*/
664-
export class ElementFinder {
699+
export class ElementFinder extends WebdriverWebElement {
665700
parentElementArrayFinder: ElementArrayFinder;
666701
elementArrayFinder_: ElementArrayFinder;
667-
then: Function = null;
702+
then: (fn: Function, errorFn: Function) => webdriver.promise.Promise = null;
668703

669-
constructor(private browser_: Browser, elementArrayFinder: ElementArrayFinder) {
704+
constructor(
705+
private browser_: Browser, elementArrayFinder: ElementArrayFinder) {
706+
super();
670707
if (!elementArrayFinder) {
671708
throw new Error('BUG: elementArrayFinder cannot be empty');
672709
}
@@ -680,6 +717,7 @@ export class ElementFinder {
680717
*
681718
* @param {function(webdriver.promise.Promise)} fn Function which takes
682719
* the value of the underlying actionResult.
720+
* @param {function(Error)} errorFn
683721
*
684722
* @return {webdriver.promise.Promise} Promise which contains the results of
685723
* evaluating fn.
@@ -731,10 +769,12 @@ export class ElementFinder {
731769
});
732770
}
733771

734-
static fromWebElement_(ptor: any, webElem: any, locator: any): ElementFinder {
772+
static fromWebElement_(
773+
browser: Browser, webElem: webdriver.WebElement,
774+
locator: Locator): ElementFinder {
735775
let getWebElements =
736776
() => { return webdriver.promise.fulfilled([webElem]); };
737-
return new ElementArrayFinder(ptor, getWebElements, locator)
777+
return new ElementArrayFinder(browser, getWebElements, locator)
738778
.toElementFinder_();
739779
}
740780

@@ -802,7 +842,7 @@ export class ElementFinder {
802842
* @param {webdriver.Locator} subLocator
803843
* @return {ElementArrayFinder}
804844
*/
805-
all(subLocator: any): ElementArrayFinder {
845+
all(subLocator: Locator): ElementArrayFinder {
806846
return this.elementArrayFinder_.all(subLocator);
807847
}
808848

@@ -833,7 +873,7 @@ export class ElementFinder {
833873
* @param {webdriver.Locator} subLocator
834874
* @return {ElementFinder}
835875
*/
836-
element(subLocator: any): ElementFinder {
876+
element(subLocator: Locator): ElementFinder {
837877
return this.all(subLocator).toElementFinder_();
838878
}
839879

lib/expectedConditions.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import {protractor} from './ptor';
21
import {ElementFinder} from './element';
2+
import {protractor} from './ptor';
3+
34
let webdriver = require('selenium-webdriver');
45

56
/* globals browser */
@@ -44,7 +45,7 @@ let webdriver = require('selenium-webdriver');
4445
*
4546
* @constructor
4647
*/
47-
export class ExpectedConditions {
48+
export class ExpectedConditions_ {
4849
/**
4950
* Negates the result of a promise.
5051
*
@@ -189,7 +190,8 @@ export class ExpectedConditions {
189190
* @return {!function} An expected condition that returns a promise
190191
* representing whether the text is present in the element.
191192
*/
192-
textToBePresentInElement(elementFinder: ElementFinder, text: string): Function {
193+
textToBePresentInElement(elementFinder: ElementFinder, text: string):
194+
Function {
193195
var hasText = () => {
194196
return elementFinder.getText().then((actualText: string): boolean => {
195197
return actualText.indexOf(text) > -1;
@@ -213,7 +215,8 @@ export class ExpectedConditions {
213215
* @return {!function} An expected condition that returns a promise
214216
* representing whether the text is present in the element's value.
215217
*/
216-
textToBePresentInElementValue(elementFinder: ElementFinder, text: string): Function {
218+
textToBePresentInElementValue(elementFinder: ElementFinder, text: string):
219+
Function {
217220
var hasText = () => {
218221
return elementFinder.getAttribute('value').then(
219222
(actualText: string): boolean => {

lib/globals.d.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ declare interface String { startsWith: Function; }
3535

3636
declare namespace webdriver {
3737
var error: any;
38+
39+
class ActionSequence {}
40+
3841
class WebDriver {
3942
findElements: Function;
4043
getSession: Function;
@@ -61,12 +64,14 @@ declare namespace webdriver {
6164
}
6265

6366
namespace promise {
64-
interface Promise {
65-
controlFlow: Function,
66-
then: Function
67-
}
67+
interface Promise {
68+
controlFlow: Function, then: Function
69+
}
6870
}
6971

72+
namespace util {
73+
interface Condition {}
74+
}
7075
class Capabilities {
7176
get: Function;
7277
}

lib/locators.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@ import * as util from 'util';
22
let webdriver: any = require('selenium-webdriver');
33
let clientSideScripts: any = require('./clientsidescripts');
44

5-
/**
6-
* webdriver's By is an enum of locator functions, so we must set it to
7-
* a prototype before inheriting from it.
8-
*/
9-
export class WebdriverBy {}
10-
WebdriverBy.prototype = webdriver.By;
5+
// explicitly extend selenium webdriver to define functions that
6+
// are supported per the website
7+
export class WebdriverBy {
8+
className: (className: string) => Locator = webdriver.By.className;
9+
css: (css: string) => Locator = webdriver.By.css;
10+
id: (id: string) => Locator = webdriver.By.id;
11+
linkText: (linkText: string) => Locator = webdriver.By.linkText;
12+
js: (js: string) => Locator = webdriver.By.js;
13+
name: (name: string) => Locator = webdriver.By.name;
14+
partialLinkText:
15+
(partialText: string) => Locator = webdriver.By.partialLinkText;
16+
tagName: (tagName: string) => Locator = webdriver.By.tagName;
17+
xpath: (xpath: string) => Locator = webdriver.By.xpath;
18+
}
1119

20+
// interface for webdriver.Locator
1221
export interface Locator {
1322
findElementsOverride?:
1423
(driver: webdriver.WebDriver, using: webdriver.WebElement,
@@ -27,17 +36,6 @@ export interface Locator {
2736
export class ProtractorBy extends WebdriverBy {
2837
[key: string]: any;
2938

30-
className: (className: string) => Locator = webdriver.By.className;
31-
css: (css: string) => Locator = webdriver.By.css;
32-
id: (id: string) => Locator = webdriver.By.id;
33-
linkText: (linkText: string) => Locator = webdriver.By.linkText;
34-
js: (js: string) => Locator = webdriver.By.js;
35-
name: (name: string) => Locator = webdriver.By.name;
36-
partialLinkText:
37-
(partialText: string) => Locator = webdriver.By.partialLinkText;
38-
tagName: (tagName: string) => Locator = webdriver.By.tagName;
39-
xpath: (xpath: string) => Locator = webdriver.By.xpath;
40-
4139
/**
4240
* Add a locator to this instance of ProtractorBy. This locator can then be
4341
* used with element(by.locatorName(args)).

0 commit comments

Comments
 (0)