-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
adapterSpec.js
219 lines (187 loc) · 6.06 KB
/
adapterSpec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
require('../index.js');
var webdriver = require('selenium-webdriver');
/**
* Tests for the WebDriverJS Jasmine-Node Adapter. These tests use
* WebDriverJS's control flow and promises without setting up the whole
* webdriver.
*/
var getFakeDriver = function() {
var flow = webdriver.promise.controlFlow();
return {
controlFlow: function() {
return flow;
},
sleep: function(ms) {
return flow.timeout(ms);
},
setUp: function() {
return flow.execute(function() {
return webdriver.promise.fulfilled('setup done');
});
},
getValueA: function() {
return flow.execute(function() {
return webdriver.promise.delayed(500).then(function() {
return webdriver.promise.fulfilled('a');
});
});
},
getOtherValueA: function() {
return flow.execute(function() {
return webdriver.promise.fulfilled('a');
});
},
getValueB: function() {
return flow.execute(function() {
return webdriver.promise.fulfilled('b');
});
},
getBigNumber: function() {
return flow.execute(function() {
return webdriver.promise.fulfilled(1111);
});
},
getDecimalNumber: function() {
return flow.execute(function() {
return webdriver.promise.fulfilled(3.14159);
});
},
getDisplayedElement: function() {
return flow.execute(function() {
return webdriver.promise.fulfilled({
isDisplayed: function() {
return webdriver.promise.fulfilled(true);
}
});
});
},
getHiddenElement: function() {
return flow.execute(function() {
return webdriver.promise.fulfilled({
isDisplayed: function() {
return webdriver.promise.fulfilled(false);
}
});
});
}
};
};
var fakeDriver = getFakeDriver();
describe('webdriverJS Jasmine adapter plain', function() {
it('should pass normal synchronous tests', function() {
expect(true).toBe(true);
});
});
describe('webdriverJS Jasmine adapter', function() {
// Shorten this and you should see tests timing out.
jasmine.getEnv().defaultTimeoutInterval = 2000;
beforeEach(function() {
// 'this' should work properly to add matchers.
this.addMatchers({
toBeLotsMoreThan: function(expected) {
return this.actual > expected + 100;
},
// Example custom matcher returning a promise that resolves to true/false.
toBeDisplayed: function() {
return this.actual.isDisplayed();
}
});
});
beforeEach(function() {
fakeDriver.setUp().then(function(value) {
console.log('This should print before each test: ' + value);
});
});
it('should pass normal synchronous tests', function() {
expect(true).toEqual(true);
});
it('should compare a promise to a primitive', function() {
expect(fakeDriver.getValueA()).toEqual('a');
expect(fakeDriver.getValueB()).toEqual('b');
});
it('should wait till the expect to run the flow', function() {
var promiseA = fakeDriver.getValueA();
expect(promiseA.isPending()).toBe(true);
expect(promiseA).toEqual('a');
expect(promiseA.isPending()).toBe(true);
})
it('should compare a promise to a promise', function() {
expect(fakeDriver.getValueA()).toEqual(fakeDriver.getOtherValueA());
});
it('should still allow use of the underlying promise', function() {
var promiseA = fakeDriver.getValueA();
promiseA.then(function(value) {
expect(value).toEqual('a');
});
});
it('should allow scheduling of tasks', function() {
fakeDriver.sleep(300);
expect(fakeDriver.getValueB()).toEqual('b');
});
it('should allow the use of custom matchers', function() {
expect(500).toBeLotsMoreThan(3);
expect(fakeDriver.getBigNumber()).toBeLotsMoreThan(33);
});
it('should allow custom matchers to return a promise', function() {
expect(fakeDriver.getDisplayedElement()).toBeDisplayed();
expect(fakeDriver.getHiddenElement()).not.toBeDisplayed();
});
it('should pass multiple arguments to matcher', function() {
// Passing specific precision
expect(fakeDriver.getDecimalNumber()).toBeCloseTo(3.1, 1);
expect(fakeDriver.getDecimalNumber()).not.toBeCloseTo(3.1, 2);
// Using default precision (2)
expect(fakeDriver.getDecimalNumber()).not.toBeCloseTo(3.1);
expect(fakeDriver.getDecimalNumber()).toBeCloseTo(3.14);
});
describe('not', function() {
it('should still pass normal synchronous tests', function() {
expect(4).not.toEqual(5);
});
it('should compare a promise to a primitive', function() {
expect(fakeDriver.getValueA()).not.toEqual('b');
});
it('should compare a promise to a promise', function() {
expect(fakeDriver.getValueA()).not.toEqual(fakeDriver.getValueB());
});
});
it('should throw an error with a WebElement actual value', function() {
var webElement = new webdriver.WebElement(fakeDriver, 'idstring');
expect(function() {
expect(webElement).toEqual(4);
}).toThrow('expect called with WebElement argment, expected a Promise. ' +
'Did you mean to use .getText()?');
});
// Uncomment to see timeout failures.
// it('should timeout after 200ms', function() {
// expect(fakeDriver.getValueA()).toEqual('a');
// }, 200);
// it('should timeout after 300ms', function() {
// fakeDriver.sleep(9999);
// expect(fakeDriver.getValueB()).toEqual('b');
// }, 300);
// it('should pass errors from done callback', function(done) {
// done('an error');
// });
it('should pass after the timed out tests', function() {
expect(true).toEqual(true);
});
describe('should work for both synchronous and asynchronous tests', function() {
var x;
beforeEach(function() {
x = 0;
});
afterEach(function() {
expect(x).toBe(1);
});
it('should execute a synchronous test', function() {
x = 1;
});
it('should execute an asynchronous test', function(done) {
setTimeout(function(){
x = 1;
done();
}, 500);
});
});
});