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

Commit 5a929e7

Browse files
committed
chore(types): clean up TypeScript support
- fix exampleTypescript to have noGlobals - change the exampleTypescript tsconfig.json to include @types - alias promise as wdpromise everywhere - refactor gulpfile task 'types' to use multiple files from tsc - add browser method types applied from webdriver this closes #3430, closes #3477, and closes #3500
1 parent c7fff5e commit 5a929e7

File tree

8 files changed

+120
-163
lines changed

8 files changed

+120
-163
lines changed

exampleTypescript/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.js
22
node_modules
3+
tmp/

exampleTypescript/conf.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,9 @@ export let config: Config = {
1616
browserName: 'chrome'
1717
},
1818
specs: [ 'spec.js' ],
19-
seleniumAddress: 'http://localhost:4444/wd/hub'
19+
seleniumAddress: 'http://localhost:4444/wd/hub',
20+
21+
// You could set no globals to true to avoid jQuery '$' and protractor '$'
22+
// collisions on the global namespace.
23+
noGlobals: true
2024
};

exampleTypescript/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"sourceMap": false,
77
"declaration": false,
88
"noImplicitAny": false,
9-
"outDir": "tmp"
9+
"outDir": "tmp",
10+
"types": ["node", "jasmine"]
1011
},
1112
"exclude": [
1213
"node_modules",

gulpfile.js

Lines changed: 11 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var runSpawn = function(done, task, opt_arg, opt_io) {
3333
};
3434

3535
gulp.task('built:copy', function() {
36-
return gulp.src(['lib/**/*','!lib/**/*.ts'])
36+
return gulp.src(['lib/**/*.js','lib/globals.d.ts'])
3737
.pipe(gulp.dest('built/'));
3838
});
3939

@@ -72,91 +72,28 @@ gulp.task('tsc:globals', function(done) {
7272

7373
gulp.task('prepublish', function(done) {
7474
runSequence(['jshint', 'format'], 'tsc', 'tsc:globals', 'types',
75-
'ambient', 'built:copy', done);
75+
'built:copy', done);
7676
});
7777

7878
gulp.task('pretest', function(done) {
7979
runSequence(
8080
['webdriver:update', 'jshint', 'format'], 'tsc', 'tsc:globals',
81-
'types', 'ambient', 'built:copy', done);
81+
'types', 'built:copy', done);
8282
});
8383

8484
gulp.task('default',['prepublish']);
8585

8686
gulp.task('types', function(done) {
87-
var folder = 'built';
88-
var files = ['browser', 'element', 'locators', 'expectedConditions',
89-
'config', 'plugins', 'ptor'];
90-
var outputFile = path.resolve(folder, 'index.d.ts');
87+
var outputFile = path.resolve('built', 'index.d.ts');
9188
var contents = '';
92-
contents += '/// <reference path="../../@types/node/index.d.ts" />\n';
93-
contents += '/// <reference path="../../@types/jasmine/index.d.ts" />\n';
9489
contents += '/// <reference path="../typings/index.d.ts" />\n';
95-
contents += 'import {ActionSequence, By, WebDriver, WebElement, WebElementPromise, promise, promise as wdpromise, until} from \'selenium-webdriver\';\n';
96-
files.forEach(file => {
97-
contents += parseTypingsFile(folder, file);
98-
});
99-
100-
// remove files with d.ts
101-
glob.sync(folder + '/**/*.d.ts').forEach(file => {
102-
fs.unlinkSync(path.resolve(file));
103-
});
104-
105-
// write contents to 'built/index.d.ts'
90+
contents += '/// <reference path="./globals.d.ts" />\n';
91+
contents += 'export { ElementHelper, ProtractorBrowser } from \'./browser\';\n';
92+
contents += 'export { ElementArrayFinder, ElementFinder } from \'./element\';\n';
93+
contents += 'export { ProtractorExpectedConditions } from \'./expectedConditions\';\n';
94+
contents += 'export { ProtractorBy } from \'./locators\';\n';
95+
contents += 'export { Config } from \'./config\';\n';
96+
contents += 'export { Ptor } from \'./ptor\';\n';
10697
fs.writeFileSync(outputFile, contents);
10798
done();
10899
});
109-
110-
var parseTypingsFile = function(folder, file) {
111-
var fileContents = fs.readFileSync(path.resolve(folder, file + '.d.ts')).toString();
112-
// Remove new lines inside types
113-
fileContents = fileContents.replace(
114-
/webdriver.promise.Promise<\{[a-zA-Z:,; \n]+\}>/g, (type) => {
115-
return type.replace(/\n/g, '');
116-
}
117-
);
118-
var lines = fileContents.split('\n');
119-
var contents = '';
120-
for (var linePos in lines) {
121-
var line = lines[linePos];
122-
if (!line.startsWith('import')) {
123-
if (line.indexOf('declare') !== -1) {
124-
line = line.replace('declare', '').trim();
125-
}
126-
127-
// Remove webdriver types, q, http proxy agent
128-
line = removeTypes(line,'webdriver.ActionSequence');
129-
line = removeTypes(line,'webdriver.promise.Promise<[a-zA-Z{},:; ]+>');
130-
line = removeTypes(line,'webdriver.util.Condition');
131-
line = removeTypes(line,'webdriver.WebDriver');
132-
line = removeTypes(line,'webdriver.Locator');
133-
line = removeTypes(line,'webdriver.WebElement');
134-
line = removeTypes(line,'HttpProxyAgent');
135-
line = removeTypes(line,'Q.Promise<[a-zA-Z{},:; ]+>');
136-
contents += line + '\n';
137-
}
138-
}
139-
return contents;
140-
}
141-
142-
var removeTypes = function(line, webdriverType) {
143-
var tempLine = line.trim();
144-
if (tempLine.startsWith('/**') || tempLine.startsWith('*')) {
145-
return line;
146-
}
147-
return line.replace(new RegExp(webdriverType,'g'), 'any');
148-
}
149-
150-
gulp.task('ambient', function(done) {
151-
var fileContents = fs.readFileSync(path.resolve('built/index.d.ts')).toString();
152-
var contents = '';
153-
contents += 'declare namespace protractor {\n';
154-
contents += fileContents + '\n';
155-
contents += '}\n';
156-
contents += 'declare module "protractor" {\n';
157-
158-
contents += ' export = protractor; \n';
159-
contents += '}\n';
160-
fs.writeFileSync(path.resolve('built/ambient.d.ts'), contents);
161-
done();
162-
});

lib/browser.ts

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
// Util from NodeJs
22
import * as net from 'net';
3-
import {ActionSequence, promise as wdpromise, until, WebDriver, WebElement} from 'selenium-webdriver';
3+
import {
4+
ActionSequence,
5+
Capabilities,
6+
Command as WdCommand,
7+
FileDetector,
8+
Options,
9+
promise as wdpromise,
10+
Session, TargetLocator,
11+
TouchSequence, until,
12+
WebDriver,
13+
WebElement
14+
} from 'selenium-webdriver';
15+
416
import * as url from 'url';
517
import * as util from 'util';
618

@@ -32,18 +44,29 @@ for (let foo in webdriver) {
3244
exports[foo] = webdriver[foo];
3345
}
3446

35-
// Explicitly define webdriver.WebDriver.
47+
// Explicitly define webdriver.WebDriver
3648
export class Webdriver {
37-
actions: () => ActionSequence = webdriver.WebDriver.actions;
38-
wait:
39-
(condition: wdpromise.Promise<any>|until.Condition<any>|Function,
40-
opt_timeout?: number,
41-
opt_message?:
42-
string) => wdpromise.Promise<any> = webdriver.WebDriver.wait;
43-
sleep: (ms: number) => wdpromise.Promise<any> = webdriver.WebDriver.sleep;
44-
getCurrentUrl:
45-
() => wdpromise.Promise<any> = webdriver.WebDriver.getCurrentUrl;
46-
getTitle: () => wdpromise.Promise<any> = webdriver.WebDriver.getTitle;
49+
actions: () => ActionSequence;
50+
close: () => void;
51+
controlFlow: () => wdpromise.ControlFlow;
52+
executeScript: (script: string|Function, ...var_args: any[]) => wdpromise.Promise<any>;
53+
executeAsyncScript: (script: string|Function, ...var_args: any[]) => wdpromise.Promise<any>;
54+
getCapabilities: () => Capabilities;
55+
getCurrentUrl: () => wdpromise.Promise<string>;
56+
getPageSource: () => wdpromise.Promise<string>;
57+
getSession: () => wdpromise.Promise<Session>;
58+
getTitle: () => wdpromise.Promise<string>;
59+
getWindowHandle: () => wdpromise.Promise<string>;
60+
getAllWindowHandles: () => wdpromise.Promise<string[]>;
61+
manage: () => Options;
62+
quit: () => void;
63+
schedule: (command: WdCommand, description: string) => webdriver.promise.Promise<any>;
64+
setFileDetector: (detector: FileDetector) => void;
65+
sleep: (ms: number) => wdpromise.Promise<void>;
66+
switchTo: () => TargetLocator;
67+
touchActions: () => TouchSequence;
68+
wait: (condition: wdpromise.Promise<any>|until.Condition<any>|Function,
69+
opt_timeout?: number, opt_message?: string) => wdpromise.Promise<any>;
4770
takeScreenshot:
4871
() => wdpromise.Promise<any> = webdriver.WebDriver.takeScreenshot;
4972
}
@@ -266,7 +289,13 @@ export class ProtractorBrowser extends Webdriver {
266289
// These functions should delegate to the webdriver instance, but should
267290
// wait for Angular to sync up before performing the action. This does not
268291
// include functions which are overridden by protractor below.
269-
let methodsToSync = ['getCurrentUrl', 'getPageSource', 'getTitle'];
292+
let methodsToSync = [
293+
'actions', 'close', 'controlFlow', 'executeScript', 'executeAsyncScript',
294+
'getCapabilities', 'getCurrentUrl', 'getPageSource', 'getSession',
295+
'getTitle', 'getWindowHandle', 'getAllWindowHandles', 'manage', 'quit',
296+
'schedule', 'setFileDetector', 'sleep', 'switchTo', 'touchActions',
297+
'touchSequence', 'wait'
298+
];
270299

271300

272301
// Mix all other driver functionality into Protractor.
@@ -572,7 +601,7 @@ export class ProtractorBrowser extends Webdriver {
572601
* @returns {!webdriver.promise.Promise} A promise that will resolve to whether
573602
* the element is present on the page.
574603
*/
575-
isElementPresent(locatorOrElement: webdriver.Locator|
604+
isElementPresent(locatorOrElement: ProtractorBy|
576605
webdriver.WebElement): webdriver.promise.Promise<any> {
577606
let element = ((locatorOrElement as any).isPresent) ?
578607
locatorOrElement :

0 commit comments

Comments
 (0)