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

Commit e4dccbe

Browse files
authored
chore(types): add function types to element and locators (#3279)
- add 'gulp types' to output index.d.ts - some clean up for functions to use fat arrow - clang clean up
1 parent 75f3481 commit e4dccbe

File tree

4 files changed

+75
-20
lines changed

4 files changed

+75
-20
lines changed

gulpfile.js

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ var clangFormat = require('clang-format');
55
var gulpFormat = require('gulp-clang-format');
66
var runSequence = require('run-sequence');
77
var spawn = require('child_process').spawn;
8+
var fs = require('fs');
9+
var path = require('path');
10+
var glob = require('glob');
811

912
var runSpawn = function(done, task, opt_arg) {
1013
opt_arg = typeof opt_arg !== 'undefined' ? opt_arg : [];
@@ -56,12 +59,53 @@ gulp.task('tsc', function(done) {
5659
});
5760

5861
gulp.task('prepublish', function(done) {
59-
runSequence(['typings', 'jshint', 'clang'],'tsc', 'built:copy', done);
62+
runSequence(['typings', 'jshint', 'clang'], 'tsc', 'types', 'built:copy', done);
6063
});
6164

6265
gulp.task('pretest', function(done) {
6366
runSequence(
64-
['webdriver:update', 'typings', 'jshint', 'clang'], 'tsc', 'built:copy', done);
67+
['webdriver:update', 'typings', 'jshint', 'clang'], 'tsc', 'types',
68+
'built:copy', done);
6569
});
6670

6771
gulp.task('default',['prepublish']);
72+
73+
gulp.task('types', function(done) {
74+
var folder = 'built';
75+
var files = ['ptor', 'browser', 'locators'];
76+
var outputFile = path.resolve(folder, 'index.d.ts');
77+
var contents = '';
78+
files.forEach(file => {
79+
contents += parseTypingsFile(folder, file);
80+
});
81+
82+
// add module declaration
83+
contents += 'declare module "protractor" {\n';
84+
contents += ' export = protractor\n';
85+
contents += '}\n';
86+
87+
// remove files with d.ts
88+
glob.sync(folder + '/**/*.d.ts').forEach(file => {
89+
fs.unlinkSync(path.resolve(file));
90+
});
91+
92+
// write contents to 'built/index.d.ts'
93+
fs.writeFileSync(outputFile, contents);
94+
done();
95+
});
96+
97+
var parseTypingsFile = function(folder, file) {
98+
var fileContents = fs.readFileSync(path.resolve(folder, file + '.d.ts')).toString();
99+
var lines = fileContents.split('\n');
100+
var contents = '';
101+
for (var linePos in lines) {
102+
var line = lines[linePos];
103+
if (!line.startsWith('import')) {
104+
if (line.indexOf('export') !== -1) {
105+
line = line.replace('export', '').trim();
106+
}
107+
contents += line + '\n';
108+
}
109+
}
110+
return contents;
111+
}

lib/browser.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as util from 'util';
55

66
import {ElementArrayFinder, ElementFinder, build$, build$$} from './element';
77
import * as EC from './expectedConditions';
8-
import {ProtractorBy} from './locators';
8+
import {Locator, ProtractorBy} from './locators';
99
import {Logger} from './logger2';
1010
import {Plugins} from './plugins';
1111
import {protractor} from './ptor';
@@ -50,8 +50,8 @@ function mixin(to: any, from: any, fnName: string, setupFn?: Function) {
5050
};
5151

5252
export interface ElementHelper extends Function {
53-
(locator: webdriver.Locator): ElementFinder;
54-
all?: (locator: webdriver.Locator) => ElementArrayFinder;
53+
(locator: Locator): ElementFinder;
54+
all?: (locator: Locator) => ElementArrayFinder;
5555
}
5656

5757
/**
@@ -62,14 +62,14 @@ export interface ElementHelper extends Function {
6262
* @return {function(webdriver.Locator): ElementFinder}
6363
*/
6464
function buildElementHelper(browser: Browser): ElementHelper {
65-
let element: ElementHelper = function(locator: webdriver.Locator) {
65+
let element: ElementHelper = (locator: Locator) => {
6666
return new ElementArrayFinder(browser).all(locator).toElementFinder_();
6767
};
6868

6969
element.all =
70-
function(locator: webdriver.Locator) {
71-
return new ElementArrayFinder(browser).all(locator);
72-
}
70+
(locator: Locator) => {
71+
return new ElementArrayFinder(browser).all(locator);
72+
}
7373

7474
return element;
7575
};
@@ -294,7 +294,8 @@ export class Browser {
294294
* @param {boolean} opt_copyMockModules Whether to apply same mock modules on creation
295295
* @return {Protractor} a protractor instance.
296296
*/
297-
forkNewDriverInstance: (opt_useSameUrl?: boolean, opt_copyMockModules?: boolean) => Browser;
297+
forkNewDriverInstance:
298+
(opt_useSameUrl?: boolean, opt_copyMockModules?: boolean) => Browser;
298299

299300
/**
300301
* Restart the browser instance.
@@ -499,7 +500,7 @@ export class Browser {
499500
* @see webdriver.WebDriver.findElement
500501
* @return {!webdriver.WebElement}
501502
*/
502-
findElement(locator: webdriver.Locator): webdriver.WebElement {
503+
findElement(locator: Locator): webdriver.WebElement {
503504
return this.element(locator).getWebElement();
504505
}
505506

@@ -509,7 +510,7 @@ export class Browser {
509510
* @return {!webdriver.promise.Promise} A promise that will be resolved to an
510511
* array of the located {@link webdriver.WebElement}s.
511512
*/
512-
findElements(locator: webdriver.Locator): webdriver.Promise {
513+
findElements(locator: Locator): webdriver.Promise {
513514
return this.element.all(locator).getWebElements();
514515
}
515516

@@ -969,7 +970,7 @@ export class Browser {
969970
let flow = webdriver.promise.controlFlow();
970971

971972
let context: Object = {require: require};
972-
global.list = (locator: webdriver.Locator) => {
973+
global.list = (locator: Locator) => {
973974
/* globals browser */
974975
return protractor.browser.findElements(locator).then(
975976
(arr: webdriver.WebElement[]) => {

lib/element.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ let clientSideScripts = require('./clientsidescripts');
33

44
import {Logger} from './logger2';
55
import {Browser} from './browser';
6+
import {Locator} from './locators';
67

78
let logger = new Logger('element');
89

@@ -131,7 +132,7 @@ export class ElementArrayFinder {
131132
* @param {webdriver.Locator} subLocator
132133
* @return {ElementArrayFinder}
133134
*/
134-
all(locator: any): ElementArrayFinder {
135+
all(locator: Locator): ElementArrayFinder {
135136
let ptor = this.browser_;
136137
let getWebElements = () => {
137138
if (this.getWebElements === null) {
@@ -396,7 +397,8 @@ export class ElementArrayFinder {
396397
})
397398
.then(null, (e: Error) => {
398399
let noSuchErr = (e as any);
399-
noSuchErr.stack = noSuchErr.stack + (callerError as any).stack;
400+
noSuchErr.stack =
401+
noSuchErr.stack + (callerError as any).stack;
400402
throw noSuchErr;
401403
});
402404
return new ElementArrayFinder(

lib/locators.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@ let clientSideScripts: any = require('./clientsidescripts');
66
* webdriver's By is an enum of locator functions, so we must set it to
77
* a prototype before inheriting from it.
88
*/
9-
export class WebdriverBy {};
9+
export class WebdriverBy {}
1010
WebdriverBy.prototype = webdriver.By;
1111

12-
export interface Locator extends webdriver.Locator {
12+
export interface Locator {
1313
findElementsOverride?:
1414
(driver: webdriver.WebDriver, using: webdriver.WebElement,
1515
rootSelector: string) => webdriver.WebElement;
1616
row?: (index: number) => Locator;
1717
column?: (index: string) => Locator;
1818
}
19-
;
2019

2120
/**
2221
* The Protractor Locators. These provide ways of finding elements in
@@ -26,9 +25,19 @@ export interface Locator extends webdriver.Locator {
2625
* @extends {webdriver.By}
2726
*/
2827
export class ProtractorBy extends WebdriverBy {
29-
// Explicit index signature to fix TS warining.
3028
[key: string]: any;
3129

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+
3241
/**
3342
* Add a locator to this instance of ProtractorBy. This locator can then be
3443
* used with element(by.locatorName(args)).
@@ -378,7 +387,6 @@ export class ProtractorBy extends WebdriverBy {
378387
return this.byRepeaterInner(true, repeatDescriptor);
379388
}
380389

381-
382390
/**
383391
* Find elements by CSS which contain a certain string.
384392
*

0 commit comments

Comments
 (0)