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

Commit f23d027

Browse files
authored
chore(types): webdriver typings for locators (#3507)
- temporarily add typings for selenium-webdriver.d.ts - include selenium-webdriver dependency to built/index.d.ts - add webdriver typings to locators - update example and spec/install to not use typings.json - spec test updated to get the tsc test to pass - includes clang formatting fixes
1 parent 30102fb commit f23d027

File tree

12 files changed

+6641
-215
lines changed

12 files changed

+6641
-215
lines changed

exampleTypescript/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"typescript": "^2.0.0"
1616
},
1717
"devDependencies": {
18-
"@types/jasmine": "^2.2.31",
19-
"@types/node": "^6.0.35"
18+
"@types/jasmine": "^2.2.33",
19+
"@types/node": "^6.0.38"
2020
}
2121
}

gulpfile.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ gulp.task('types', function(done) {
8888
var files = ['browser', 'element', 'locators', 'expectedConditions',
8989
'config', 'plugins', 'ptor'];
9090
var outputFile = path.resolve(folder, 'index.d.ts');
91-
var contents = '';
91+
var contents = '/// <reference path="../typings/index.d.ts" />\n';
92+
contents += 'import {By, WebDriver, WebElement, promise} from \'selenium-webdriver\';\n';
9293
files.forEach(file => {
9394
contents += parseTypingsFile(folder, file);
9495
});

lib/browser.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ export class ProtractorBrowser extends Webdriver {
269269
// Mix all other driver functionality into Protractor.
270270
Object.getOwnPropertyNames(webdriver.WebDriver.prototype)
271271
.forEach((method: string) => {
272-
if (!this[method] && typeof webdriverInstance[method] == 'function') {
272+
if (!this[method] &&
273+
typeof(webdriverInstance as any)[method] == 'function') {
273274
if (methodsToSync.indexOf(method) !== -1) {
274275
ptorMixin(
275276
this, webdriverInstance, method,
@@ -570,9 +571,10 @@ export class ProtractorBrowser extends Webdriver {
570571
*/
571572
isElementPresent(locatorOrElement: webdriver.Locator|
572573
webdriver.WebElement): webdriver.promise.Promise<any> {
573-
let element = (locatorOrElement.isPresent) ? locatorOrElement :
574-
this.element(locatorOrElement);
575-
return element.isPresent();
574+
let element = ((locatorOrElement as any).isPresent) ?
575+
locatorOrElement :
576+
this.element(locatorOrElement);
577+
return (element as any).isPresent();
576578
}
577579

578580
/**
@@ -870,7 +872,7 @@ export class ProtractorBrowser extends Webdriver {
870872
* Mixin navigation methods back into the navigation object so that
871873
* they are invoked as before, i.e. driver.navigate().refresh()
872874
*/
873-
navigate() {
875+
navigate(): any {
874876
let nav = this.driver.navigate();
875877
ptorMixin(nav, this, 'refresh');
876878
return nav;

lib/element.ts

+24-27
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,8 @@ export class ElementArrayFinder extends WebdriverWebElement {
131131
/**
132132
* Calls to ElementArrayFinder may be chained to find an array of elements
133133
* using the current elements in this ElementArrayFinder as the starting
134-
* point.
135-
* This function returns a new ElementArrayFinder which would contain the
136-
* children elements found (and could also be empty).
134+
* point. This function returns a new ElementArrayFinder which would contain
135+
* the children elements found (and could also be empty).
137136
*
138137
* @alias element.all(locator).all(locator)
139138
* @view
@@ -167,13 +166,15 @@ export class ElementArrayFinder extends WebdriverWebElement {
167166
let getWebElements = () => {
168167
if (this.getWebElements === null) {
169168
// This is the first time we are looking for an element
170-
return ptor.waitForAngular('Locator: ' + locator).then(() => {
171-
if (locator.findElementsOverride) {
172-
return locator.findElementsOverride(ptor.driver, null, ptor.rootEl);
173-
} else {
174-
return ptor.driver.findElements(locator);
175-
}
176-
});
169+
return ptor.waitForAngular('Locator: ' + locator)
170+
.then((): webdriver.promise.Promise<webdriver.WebElement[]> => {
171+
if (locator.findElementsOverride) {
172+
return locator.findElementsOverride(
173+
ptor.driver, null, ptor.rootEl);
174+
} else {
175+
return ptor.driver.findElements(locator);
176+
}
177+
});
177178
} else {
178179
return this.getWebElements().then(
179180
(parentWebElements: webdriver.WebElement[]) => {
@@ -208,13 +209,10 @@ export class ElementArrayFinder extends WebdriverWebElement {
208209

209210
/**
210211
* Apply a filter function to each element within the ElementArrayFinder.
211-
* Returns
212-
* a new ElementArrayFinder with all elements that pass the filter function.
213-
* The
214-
* filter function receives the ElementFinder as the first argument
215-
* and the index as a second arg.
216-
* This does not actually retrieve the underlying list of elements, so it can
217-
* be used in page objects.
212+
* Returns a new ElementArrayFinder with all elements that pass the filter
213+
* function. The filter function receives the ElementFinder as the first
214+
* argument and the index as a second arg. This does not actually retrieve
215+
* the underlying list of elements, so it can be used in page objects.
218216
*
219217
* @alias element.all(locator).filter(filterFn)
220218
* @view
@@ -263,8 +261,7 @@ export class ElementArrayFinder extends WebdriverWebElement {
263261

264262
/**
265263
* Get an element within the ElementArrayFinder by index. The index starts at
266-
* 0.
267-
* Negative indices are wrapped (i.e. -i means ith element from last)
264+
* 0. Negative indices are wrapped (i.e. -i means ith element from last)
268265
* This does not actually retrieve the underlying element.
269266
*
270267
* @alias element.all(locator).get(index)
@@ -751,7 +748,7 @@ export class ElementFinder extends WebdriverWebElement {
751748
// This filter verifies that there is only 1 element returned by the
752749
// elementArrayFinder. It will warn if there are more than 1 element and
753750
// throw an error if there are no elements.
754-
let getWebElements = () => {
751+
let getWebElements = (): webdriver.WebElement[] => {
755752
return elementArrayFinder.getWebElements().then(
756753
(webElements: webdriver.WebElement[]) => {
757754
if (webElements.length === 0) {
@@ -961,12 +958,12 @@ export class ElementFinder extends WebdriverWebElement {
961958
* // Element not present.
962959
* expect(element(by.binding('notPresent')).isPresent()).toBe(false);
963960
*
964-
* @returns {ElementFinder} which resolves to whether
961+
* @returns {webdriver.promise.Promise<boolean>} which resolves to whether
965962
* the element is present on the page.
966963
*/
967-
isPresent(): ElementFinder {
964+
isPresent(): webdriver.promise.Promise<boolean> {
968965
return this.parentElementArrayFinder.getWebElements().then(
969-
(arr: webdriver.WebElement[]) => {
966+
(arr: any) => {
970967
if (arr.length === 0) {
971968
return false;
972969
}
@@ -995,16 +992,16 @@ export class ElementFinder extends WebdriverWebElement {
995992
/**
996993
* Same as ElementFinder.isPresent(), except this checks whether the element
997994
* identified by the subLocator is present, rather than the current element
998-
* finder. i.e. `element(by.css('#abc')).element(by.css('#def')).isPresent()` is
999-
* identical to `element(by.css('#abc')).isElementPresent(by.css('#def'))`.
995+
* finder. i.e. `element(by.css('#abc')).element(by.css('#def')).isPresent()`
996+
* is identical to `element(by.css('#abc')).isElementPresent(by.css('#def'))`.
1000997
*
1001998
* @see ElementFinder.isPresent
1002999
*
10031000
* @param {webdriver.Locator} subLocator Locator for element to look for.
1004-
* @returns {ElementFinder} which resolves to whether
1001+
* @returns {webdriver.promise.Promise<boolean>} which resolves to whether
10051002
* the subelement is present on the page.
10061003
*/
1007-
isElementPresent(subLocator: any): ElementFinder {
1004+
isElementPresent(subLocator: any): webdriver.promise.Promise<boolean> {
10081005
if (!subLocator) {
10091006
throw new Error(
10101007
'SubLocator is not supplied as a parameter to ' +

lib/globals.d.ts

-53
Original file line numberDiff line numberDiff line change
@@ -33,73 +33,20 @@ declare namespace NodeJS {
3333
declare interface String { startsWith: Function; }
3434

3535
declare namespace webdriver {
36-
var error: any;
37-
38-
class ActionSequence {}
39-
40-
class WebDriver {
41-
findElements: Function;
42-
getSession: Function;
43-
quit: Function;
44-
executeScript: Function;
45-
getCapabilities: Function;
46-
getCurrentUrl: Function;
47-
getPageSource: Function;
48-
getTitle: Function;
49-
navigate: Function;
50-
get: Function;
51-
wait: Function;
52-
schedule: Function;
53-
switchTo: Function;
54-
controlFlow: Function;
55-
static attachToSession: Function;
56-
// This index type allows looking up methods by name so we can do mixins.
57-
[key: string]: any;
58-
}
59-
60-
class Session {
61-
getId: Function;
62-
getCapabilities: Function;
63-
}
64-
6536
namespace promise {
6637
interface Promise<T> {
6738
controlFlow: Function;
68-
then: Function;
6939
}
7040
}
7141

7242
namespace util {
7343
interface Condition {}
7444
}
75-
class Capabilities {
76-
get: Function;
77-
}
78-
79-
class WebElement {
80-
getDriver: Function;
81-
isEnabled: Function;
82-
findElements: Function;
83-
isPresent: Function;
84-
getText: Function;
85-
}
8645

8746
class ErrorCode {
8847
code: number;
8948
}
9049

91-
class By {
92-
static css: (css: string) => webdriver.By;
93-
static id: (id: string) => webdriver.By;
94-
static linkText: (linkText: string) => webdriver.By;
95-
static js: (js: string) => webdriver.By;
96-
static name: (name: string) => webdriver.By;
97-
static partialLinkText: (partialLinkText: string) => webdriver.By;
98-
static tagName: (tagName: string) => webdriver.By;
99-
static xpath: (xpath: string) => webdriver.By;
100-
toString(): string;
101-
}
102-
10350
interface Locator {
10451
toString(): string;
10552
isPresent?: Function;

0 commit comments

Comments
 (0)