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

Commit

Permalink
chore(types): webdriver typings for locators (#3507)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
cnishina authored Sep 1, 2016
1 parent 30102fb commit f23d027
Show file tree
Hide file tree
Showing 12 changed files with 6,641 additions and 215 deletions.
4 changes: 2 additions & 2 deletions exampleTypescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"typescript": "^2.0.0"
},
"devDependencies": {
"@types/jasmine": "^2.2.31",
"@types/node": "^6.0.35"
"@types/jasmine": "^2.2.33",
"@types/node": "^6.0.38"
}
}
3 changes: 2 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ gulp.task('types', function(done) {
var files = ['browser', 'element', 'locators', 'expectedConditions',
'config', 'plugins', 'ptor'];
var outputFile = path.resolve(folder, 'index.d.ts');
var contents = '';
var contents = '/// <reference path="../typings/index.d.ts" />\n';
contents += 'import {By, WebDriver, WebElement, promise} from \'selenium-webdriver\';\n';
files.forEach(file => {
contents += parseTypingsFile(folder, file);
});
Expand Down
12 changes: 7 additions & 5 deletions lib/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ export class ProtractorBrowser extends Webdriver {
// Mix all other driver functionality into Protractor.
Object.getOwnPropertyNames(webdriver.WebDriver.prototype)
.forEach((method: string) => {
if (!this[method] && typeof webdriverInstance[method] == 'function') {
if (!this[method] &&
typeof(webdriverInstance as any)[method] == 'function') {
if (methodsToSync.indexOf(method) !== -1) {
ptorMixin(
this, webdriverInstance, method,
Expand Down Expand Up @@ -570,9 +571,10 @@ export class ProtractorBrowser extends Webdriver {
*/
isElementPresent(locatorOrElement: webdriver.Locator|
webdriver.WebElement): webdriver.promise.Promise<any> {
let element = (locatorOrElement.isPresent) ? locatorOrElement :
this.element(locatorOrElement);
return element.isPresent();
let element = ((locatorOrElement as any).isPresent) ?
locatorOrElement :
this.element(locatorOrElement);
return (element as any).isPresent();
}

/**
Expand Down Expand Up @@ -870,7 +872,7 @@ export class ProtractorBrowser extends Webdriver {
* Mixin navigation methods back into the navigation object so that
* they are invoked as before, i.e. driver.navigate().refresh()
*/
navigate() {
navigate(): any {
let nav = this.driver.navigate();
ptorMixin(nav, this, 'refresh');
return nav;
Expand Down
51 changes: 24 additions & 27 deletions lib/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,8 @@ export class ElementArrayFinder extends WebdriverWebElement {
/**
* Calls to ElementArrayFinder may be chained to find an array of elements
* using the current elements in this ElementArrayFinder as the starting
* point.
* This function returns a new ElementArrayFinder which would contain the
* children elements found (and could also be empty).
* point. This function returns a new ElementArrayFinder which would contain
* the children elements found (and could also be empty).
*
* @alias element.all(locator).all(locator)
* @view
Expand Down Expand Up @@ -167,13 +166,15 @@ export class ElementArrayFinder extends WebdriverWebElement {
let getWebElements = () => {
if (this.getWebElements === null) {
// This is the first time we are looking for an element
return ptor.waitForAngular('Locator: ' + locator).then(() => {
if (locator.findElementsOverride) {
return locator.findElementsOverride(ptor.driver, null, ptor.rootEl);
} else {
return ptor.driver.findElements(locator);
}
});
return ptor.waitForAngular('Locator: ' + locator)
.then((): webdriver.promise.Promise<webdriver.WebElement[]> => {
if (locator.findElementsOverride) {
return locator.findElementsOverride(
ptor.driver, null, ptor.rootEl);
} else {
return ptor.driver.findElements(locator);
}
});
} else {
return this.getWebElements().then(
(parentWebElements: webdriver.WebElement[]) => {
Expand Down Expand Up @@ -208,13 +209,10 @@ export class ElementArrayFinder extends WebdriverWebElement {

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

/**
* Get an element within the ElementArrayFinder by index. The index starts at
* 0.
* Negative indices are wrapped (i.e. -i means ith element from last)
* 0. Negative indices are wrapped (i.e. -i means ith element from last)
* This does not actually retrieve the underlying element.
*
* @alias element.all(locator).get(index)
Expand Down Expand Up @@ -751,7 +748,7 @@ export class ElementFinder extends WebdriverWebElement {
// This filter verifies that there is only 1 element returned by the
// elementArrayFinder. It will warn if there are more than 1 element and
// throw an error if there are no elements.
let getWebElements = () => {
let getWebElements = (): webdriver.WebElement[] => {
return elementArrayFinder.getWebElements().then(
(webElements: webdriver.WebElement[]) => {
if (webElements.length === 0) {
Expand Down Expand Up @@ -961,12 +958,12 @@ export class ElementFinder extends WebdriverWebElement {
* // Element not present.
* expect(element(by.binding('notPresent')).isPresent()).toBe(false);
*
* @returns {ElementFinder} which resolves to whether
* @returns {webdriver.promise.Promise<boolean>} which resolves to whether
* the element is present on the page.
*/
isPresent(): ElementFinder {
isPresent(): webdriver.promise.Promise<boolean> {
return this.parentElementArrayFinder.getWebElements().then(
(arr: webdriver.WebElement[]) => {
(arr: any) => {
if (arr.length === 0) {
return false;
}
Expand Down Expand Up @@ -995,16 +992,16 @@ export class ElementFinder extends WebdriverWebElement {
/**
* Same as ElementFinder.isPresent(), except this checks whether the element
* identified by the subLocator is present, rather than the current element
* finder. i.e. `element(by.css('#abc')).element(by.css('#def')).isPresent()` is
* identical to `element(by.css('#abc')).isElementPresent(by.css('#def'))`.
* finder. i.e. `element(by.css('#abc')).element(by.css('#def')).isPresent()`
* is identical to `element(by.css('#abc')).isElementPresent(by.css('#def'))`.
*
* @see ElementFinder.isPresent
*
* @param {webdriver.Locator} subLocator Locator for element to look for.
* @returns {ElementFinder} which resolves to whether
* @returns {webdriver.promise.Promise<boolean>} which resolves to whether
* the subelement is present on the page.
*/
isElementPresent(subLocator: any): ElementFinder {
isElementPresent(subLocator: any): webdriver.promise.Promise<boolean> {
if (!subLocator) {
throw new Error(
'SubLocator is not supplied as a parameter to ' +
Expand Down
53 changes: 0 additions & 53 deletions lib/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,73 +33,20 @@ declare namespace NodeJS {
declare interface String { startsWith: Function; }

declare namespace webdriver {
var error: any;

class ActionSequence {}

class WebDriver {
findElements: Function;
getSession: Function;
quit: Function;
executeScript: Function;
getCapabilities: Function;
getCurrentUrl: Function;
getPageSource: Function;
getTitle: Function;
navigate: Function;
get: Function;
wait: Function;
schedule: Function;
switchTo: Function;
controlFlow: Function;
static attachToSession: Function;
// This index type allows looking up methods by name so we can do mixins.
[key: string]: any;
}

class Session {
getId: Function;
getCapabilities: Function;
}

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

namespace util {
interface Condition {}
}
class Capabilities {
get: Function;
}

class WebElement {
getDriver: Function;
isEnabled: Function;
findElements: Function;
isPresent: Function;
getText: Function;
}

class ErrorCode {
code: number;
}

class By {
static css: (css: string) => webdriver.By;
static id: (id: string) => webdriver.By;
static linkText: (linkText: string) => webdriver.By;
static js: (js: string) => webdriver.By;
static name: (name: string) => webdriver.By;
static partialLinkText: (partialLinkText: string) => webdriver.By;
static tagName: (tagName: string) => webdriver.By;
static xpath: (xpath: string) => webdriver.By;
toString(): string;
}

interface Locator {
toString(): string;
isPresent?: Function;
Expand Down
Loading

0 comments on commit f23d027

Please sign in to comment.