Skip to content

Commit 5b5b394

Browse files
committed
feat(typescript): add types
1 parent 27b4850 commit 5b5b394

File tree

8 files changed

+144
-60
lines changed

8 files changed

+144
-60
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ describe('tests with webdriver', function() {
6161
})
6262
```
6363

64+
TypeScript
65+
----------
66+
67+
jasminewd only exports one function: `init`. The type for `init` is bundled
68+
with the `jasminewd2` package.
69+
70+
However, jasminewd also modifies some of the global jasmine variables (e.g.
71+
`it`). Because some packages (e.g. Protractor) depend on jasminewd, but don't
72+
always actually `require('jasminewd2')`, we have put the global type changes in
73+
a separate package: `@types/jasminewd2`. If you are writing tests using
74+
jasminewd (including Protractor tests), be sure to include `@types/jasminewd2`
75+
in your `devDependencies`.
76+
6477
`async` functions / `await`
6578
---------------------------
6679

index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// See README.md#typescript for details
2+
3+
import {promise as wdpromise} from 'selenium-webdriver';
4+
5+
export function init(flow: wdpromise.ControlFlow): void;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"selenium-webdriver": "3.0.1"
1717
},
1818
"devDependencies": {
19+
"@types/jasmine": "^2.5.40",
1920
"@types/node": "^6.0.56",
2021
"@types/selenium-webdriver": "^2.53.38",
2122
"jasmine": "2.4.1",

spec/@types_jasminewd2.d.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// This is jasminewd's internal version of @types/jasminewd2. If you need types
2+
// for jasminewd2, please use @types/jasminewd2 instead
3+
4+
// Type definitions for jasminewd2
5+
// Project https://github.com/angular/jasminewd
6+
// Definitions by: Sammy Jelin <https://github.com/sjelin>
7+
8+
declare function it(expectation: string, assertion?: () => Promise<void>, timeout?: number): void;
9+
declare function fit(expectation: string, assertion?: () => Promise<void>, timeout?: number): void;
10+
declare function xit(expectation: string, assertion?: () => Promise<void>, timeout?: number): void;
11+
declare function beforeEach(action: () => Promise<void>, timeout?: number): void;
12+
declare function afterEach(action: () => Promise<void>, timeout?: number): void;
13+
declare function beforeAll(action: () => Promise<void>, timeout?: number): void;
14+
declare function afterAll(action: () => Promise<void>, timeout?: number): void;
15+
16+
declare namespace jasmine {
17+
// The global `Promise` type is too strict and kinda wrong
18+
interface Promise<T> {
19+
then<U>(onFulfill?: (value: T) => U | Promise<U>, onReject?: (error: any) => U | Promise<U>): Promise<U>;
20+
}
21+
22+
interface Matchers {
23+
toBe(expected: any, expectationFailOutput?: any): Promise<void>;
24+
toEqual(expected: any, expectationFailOutput?: any): Promise<void>;
25+
toMatch(expected: string | RegExp | Promise<string | RegExp>, expectationFailOutput?: any): Promise<void>;
26+
toBeDefined(expectationFailOutput?: any): Promise<void>;
27+
toBeUndefined(expectationFailOutput?: any): Promise<void>;
28+
toBeNull(expectationFailOutput?: any): Promise<void>;
29+
toBeNaN(): Promise<void>;
30+
toBeTruthy(expectationFailOutput?: any): Promise<void>;
31+
toBeFalsy(expectationFailOutput?: any): Promise<void>;
32+
toHaveBeenCalled(): Promise<void>;
33+
toHaveBeenCalledWith(...params: any[]): Promise<void>;
34+
toHaveBeenCalledTimes(expected: number | Promise<number>): Promise<void>;
35+
toContain(expected: any, expectationFailOutput?: any): Promise<void>;
36+
toBeLessThan(expected: number | Promise<number>, expectationFailOutput?: any): Promise<void>;
37+
toBeLessThanOrEqual(expected: number | Promise<number>, expectationFailOutput?: any): Promise<void>;
38+
toBeGreaterThan(expected: number | Promise<number>, expectationFailOutput?: any): Promise<void>;
39+
toBeGreaterThanOrEqual(expected: number | Promise<number>, expectationFailOutput?: any): Promise<void>;
40+
toBeCloseTo(expected: number | Promise<number>, precision?: any, expectationFailOutput?: any): Promise<void>;
41+
toThrow(expected?: any): Promise<void>;
42+
toThrowError(message?: string | RegExp | Promise<string | RegExp>): Promise<void>;
43+
toThrowError(expected?: new (...args: any[]) => Error | Promise<new (...args: any[]) => Error>, message?: string | RegExp | Promise<string | RegExp>): Promise<void>;
44+
}
45+
46+
function addMatchers(matchers: AsyncCustomMatcherFactories): void;
47+
48+
interface Env {
49+
addMatchers(matchers: AsyncCustomMatcherFactories): void;
50+
}
51+
52+
interface Spec {
53+
addMatchers(matchers: AsyncCustomMatcherFactories): void;
54+
}
55+
56+
interface AsyncCustomMatcherFactories {
57+
[index: string]: AsyncCustomMatcherFactory;
58+
}
59+
60+
interface AsyncCustomMatcherFactory {
61+
(util: MatchersUtil, customEqualityTesters: Array<CustomEqualityTester>): AsyncCustomMatcher;
62+
}
63+
64+
interface AsyncCustomMatcher {
65+
compare<T>(actual: T, expected: T): AsyncCustomMatcherResult;
66+
compare(actual: any, expected: any): AsyncCustomMatcherResult;
67+
}
68+
69+
interface AsyncCustomMatcherResult {
70+
pass: boolean | Promise<boolean>;
71+
message?: string;
72+
}
73+
}

spec/asyncAwaitAdapterSpec.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
11
import {promise as wdpromise, WebElement} from 'selenium-webdriver';
2-
const common = require('./common');
3-
4-
declare function expect(actual: any): any;
5-
declare function describe(description: string, tests: Function): void;
6-
declare function it(description: string, test?: Function, timeout?: number): any;
7-
declare function xit(description: string, test?: Function, timeout?: number): any;
8-
declare function beforeEach(setup: Function): void;
9-
declare function beforeAll(setup: Function): void;
10-
declare function afterEach(setup: Function): void;
11-
declare function afterAll(setup: Function): void;
12-
declare var jasmine;
2+
import {getFakeDriver, getMatchers} from './common.js';
133

144
/**
155
* This file is very similar to adapterSpec.ts, but we use async/await instead
166
* of the WebDriver Control Flow for synchronization. These tests are desgined
177
* to work regardless of if the WebDriver Control Flow is disabled.
188
*/
199

20-
const fakeDriver = common.getFakeDriver();
10+
const fakeDriver = getFakeDriver();
2111

2212
/* jshint esversion: 6 */
2313
describe('webdriverJS Jasmine adapter plain', function() {
@@ -54,7 +44,7 @@ describe('webdriverJS Jasmine adapter', function() {
5444
let beforeEachMsg: string;
5545

5646
beforeEach(function() {
57-
jasmine.addMatchers(common.getMatchers());
47+
jasmine.addMatchers(getMatchers());
5848
});
5949

6050
beforeEach(async function() {

spec/asyncAwaitErrorSpec.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
1-
const common = require('./common');
2-
3-
declare function expect(actual: any): any;
4-
declare function describe(description: string, tests: Function): void;
5-
declare function it(description: string, test?: Function, timeout?: number): any;
6-
declare function xit(description: string, test?: Function, timeout?: number): any;
7-
declare function beforeEach(setup: Function): void;
8-
declare function beforeAll(setup: Function): void;
9-
declare function afterEach(setup: Function): void;
10-
declare function afterAll(setup: Function): void;
11-
declare var jasmine;
1+
import {getFakeDriver, getMatchers} from './common.js';
122

133
/**
144
* This file is very similar to errorSpec.ts, but we use async/await instead of
155
* the WebDriver Control Flow for synchronization. These tests are desgined to
166
* work regardless of if the WebDriver Control Flow is disabled.
177
*/
188

19-
const fakeDriver = common.getFakeDriver();
9+
const fakeDriver = getFakeDriver();
2010

2111
/* jshint esversion: 6 */
2212
describe('Timeout cases', function() {
@@ -38,7 +28,7 @@ describe('Timeout cases', function() {
3828

3929
describe('things that should fail', function() {
4030
beforeEach(function() {
41-
jasmine.addMatchers(common.getMatchers());
31+
jasmine.addMatchers(getMatchers());
4232
});
4333

4434
it('should pass errors from done callback', function(done) {
Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,111 @@
1-
var webdriver = require('selenium-webdriver');
1+
import {promise as wdpromise, WebElement} from 'selenium-webdriver';
2+
import {init as initJasmineWD} from '../index';
23

3-
var flow = webdriver.promise.controlFlow();
4-
require('../index.js').init(flow);
4+
const flow = wdpromise.controlFlow();
5+
initJasmineWD(flow);
56

6-
exports.getFakeDriver = function() {
7+
export function getFakeDriver() {
78
return {
89
controlFlow: function() {
910
return flow;
1011
},
11-
sleep: function(ms) {
12+
sleep: function(ms: number) {
1213
return flow.timeout(ms);
1314
},
1415
setUp: function() {
1516
return flow.execute(function() {
16-
return webdriver.promise.when('setup done');
17+
return wdpromise.when('setup done');
1718
}, 'setUp');
1819
},
1920
getValueA: function() {
2021
return flow.execute(function() {
21-
return webdriver.promise.delayed(500).then(function() {
22-
return webdriver.promise.when('a');
22+
return wdpromise.delayed(500).then(function() {
23+
return wdpromise.when('a');
2324
});
2425
}, 'getValueA');
2526
},
2627
getOtherValueA: function() {
2728
return flow.execute(function() {
28-
return webdriver.promise.when('a');
29+
return wdpromise.when('a');
2930
}, 'getOtherValueA');
3031
},
3132
getValueB: function() {
3233
return flow.execute(function() {
33-
return webdriver.promise.when('b');
34+
return wdpromise.when('b');
3435
}, 'getValueB');
3536
},
36-
getBigNumber: function() {
37+
getBigNumber: function(): wdpromise.Promise<number> {
3738
return flow.execute(function() {
38-
return webdriver.promise.when(1111);
39+
return wdpromise.when(1111);
3940
}, 'getBigNumber');
4041
},
41-
getSmallNumber: function() {
42+
getSmallNumber: function(): wdpromise.Promise<number> {
4243
return flow.execute(function() {
43-
return webdriver.promise.when(11);
44+
return wdpromise.when(11);
4445
}, 'getSmallNumber');
4546
},
46-
getDecimalNumber: function() {
47+
getDecimalNumber: function(): wdpromise.Promise<number> {
4748
return flow.execute(function() {
48-
return webdriver.promise.when(3.14159);
49+
return wdpromise.when(3.14159);
4950
}, 'getDecimalNumber');
5051
},
5152
getDisplayedElement: function() {
5253
return flow.execute(function() {
53-
return webdriver.promise.when({
54+
return wdpromise.when({
5455
isDisplayed: function() {
55-
return webdriver.promise.when(true);
56+
return wdpromise.when(true);
5657
}
5758
});
5859
}, 'getDisplayedElement');
5960
},
6061
getHiddenElement: function() {
6162
return flow.execute(function() {
62-
return webdriver.promise.when({
63+
return wdpromise.when({
6364
isDisplayed: function() {
64-
return webdriver.promise.when(false);
65+
return wdpromise.when(false);
6566
}
6667
});
6768
}, 'getHiddenElement');
6869
},
69-
getValueList: function() {
70+
getValueList: function(): wdpromise.Promise<Array<{getText: () => wdpromise.Promise<string>}>> {
7071
return flow.execute(function() {
71-
return webdriver.promise.when([{
72+
return wdpromise.when([{
7273
getText: function() {
73-
return flow.execute(function() { return webdriver.promise.when('a');});
74+
return flow.execute(function() { return wdpromise.when('a');});
7475
}
7576
}, {
7677
getText: function() {
77-
return flow.execute(function() { return webdriver.promise.when('b');});
78+
return flow.execute(function() { return wdpromise.when('b');});
7879
}
7980
}, {
8081
getText: function() {
81-
return flow.execute(function() { return webdriver.promise.when('c');});
82+
return flow.execute(function() { return wdpromise.when('c');});
8283
}
8384
}, {
8485
getText: function() {
85-
return flow.execute(function() { return webdriver.promise.when('d');});
86+
return flow.execute(function() { return wdpromise.when('d');});
8687
}
8788
}]);
8889
}, 'getValueList');
8990
},
9091
displayedElement: {
9192
isDisplayed: function() {
92-
return webdriver.promise.when(true);
93+
return wdpromise.when(true);
9394
}
9495
},
9596
hiddenElement: {
9697
isDisplayed: function() {
97-
return webdriver.promise.when(false);
98+
return wdpromise.when(false);
9899
}
99100
}
100101
};
101102
};
102103

103-
exports.getMatchers = function() {
104+
export function getMatchers() {
104105
return {
105106
toBeLotsMoreThan: function() {
106107
return {
107-
compare: function(actual, expected) {
108+
compare: function(actual: number, expected: number) {
108109
return {
109110
pass: actual > expected + 100
110111
};
@@ -114,7 +115,7 @@ exports.getMatchers = function() {
114115
// Example custom matcher returning a promise that resolves to true/false.
115116
toBeDisplayed: function() {
116117
return {
117-
compare: function(actual, expected) {
118+
compare: function(actual: WebElement, expected: void) {
118119
return {
119120
pass: actual.isDisplayed()
120121
};
@@ -124,6 +125,17 @@ exports.getMatchers = function() {
124125
};
125126
};
126127

127-
exports.isPending = function(managedPromise) {
128-
return managedPromise.state_ === 'pending';
128+
// declare custom matcher types
129+
declare global {
130+
namespace jasmine {
131+
interface Matchers {
132+
toBeLotsMoreThan(expected: number | Promise<number>): Promise<void>;
133+
toBeDisplayed(): Promise<void>;
134+
}
135+
}
136+
}
137+
138+
139+
export function isPending(managedPromise: wdpromise.Promise<any>) {
140+
return (managedPromise as any).state_ === 'pending';
129141
};

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"sourceMap": false,
77
"declaration": false,
88
"removeComments": false,
9-
"noImplicitAny": false,
9+
"noImplicitAny": true,
1010
"outDir": "built_spec"
1111
},
1212
"include": [

0 commit comments

Comments
 (0)