This repository has been archived by the owner on Mar 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
api.test.ts
409 lines (365 loc) · 11.4 KB
/
api.test.ts
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import { Chrome } from '../src';
describe('Chrome', () => {
let chrome = null;
beforeEach(() => {
chrome = new Chrome();
});
afterEach(() => {
chrome.done();
});
describe('#evaluate', () => {
it('should run an expression and return the value', async () => {
const res = await chrome.evaluate(() => window.location.href);
expect(res).toEqual('about:blank');
});
it('should handle async functions', async () => {
const res = await chrome.evaluate(() => {
return new Promise(resolve => {
setTimeout(() => {
resolve(window.location.href);
}, 10);
});
});
expect(res).toEqual('about:blank');
});
it('should throw when errors happen in Chrome', async () => {
return chrome
.evaluate(() => {
throw new Error('I should propogate');
})
.end()
.then(res => {
expect(res).toBeNull();
})
.catch(error => {
expect(error).toMatchSnapshot();
});
});
it('should allow variables to be passed in', async () => {
const res = await chrome.evaluate(
(a, b, c) => {
return [a, b, c];
},
'foo',
'bar',
'baz',
);
expect(res).toEqual(['foo', 'bar', 'baz']);
});
});
describe('#exists', () => {
it('should return true if something exists', async () => {
const exists = await chrome.exists('body');
expect(exists).toEqual(true);
});
it('should return false if it does not', async () => {
const exists = await chrome.exists('.i-am-not-there', { wait: false });
expect(exists).toEqual(false);
});
it('should wait for items to appear', async () => {
await chrome.evaluate(() => {
setTimeout(() => {
var div = document.createElement('div');
div.innerHTML = '<div class="arriving-late"></div>';
while (div.children.length > 0) {
document.body.appendChild(div.children[0]);
}
}, 10);
});
const exists = await chrome.exists('.arriving-late');
expect(exists).toEqual(true);
});
it('should wait for items to appear and return false', async () => {
await chrome.evaluate(() => {
setTimeout(() => {
var div = document.createElement('div');
div.innerHTML = '<div class="arriving-late"></div>';
while (div.children.length > 0) {
document.body.appendChild(div.children[0]);
}
}, 10);
});
const exists = await chrome.exists('.arriving-late', { timeout: 5 });
expect(exists).toEqual(false);
});
});
describe('#html', () => {
it('should return the html of the page with no args', async () => {
const html = await chrome.html();
expect(html).toEqual('<html><head></head><body></body></html>');
});
});
describe('#text', () => {
it('should return the text an element', async () => {
const text = `All my life I've been searching for something`;
await chrome.evaluate(text => {
var div = document.createElement('div');
div.innerHTML = `<div>${text}</div>`;
while (div.children.length > 0) {
document.body.appendChild(div.children[0]);
}
}, text);
const textRes = await chrome.text('div');
expect(textRes).toEqual(text);
});
it('should wait for the element before trying', async () => {
const text = `All my life I've been searching for something`;
await chrome.evaluate(text => {
setTimeout(() => {
var div = document.createElement('div');
div.innerHTML = `<div>${text}</div>`;
while (div.children.length > 0) {
document.body.appendChild(div.children[0]);
}
}, 5);
}, text);
const textRes = await chrome.text('div');
expect(textRes).toEqual(text);
});
it("should throw an error if the element isn't found", async () => {
return chrome
.text('div', { wait: false })
.then(textRes => {
expect(textRes).toEqual(null);
})
.catch(error => {
expect(error).toMatchSnapshot();
});
});
});
describe('#click"', () => {
it('should click elements', async () => {
await chrome.evaluate(() => {
var div = document.createElement('div');
div.innerHTML = `
<button class="clickable" onclick="javascript:document.body.appendChild(document.createElement('span'))">Click Me!</button>
`;
while (div.children.length > 0) {
document.body.appendChild(div.children[0]);
}
});
await chrome.click('.clickable');
const exists = await chrome.exists('span');
expect(exists).toEqual(true);
});
it('should wait for elements by default', async () => {
return chrome
.evaluate(() => {
setTimeout(() => {
var div = document.createElement('div');
div.innerHTML = `<button class="clickable">Click Me!</button>`;
while (div.children.length > 0) {
document.body.appendChild(div.children[0]);
}
}, 5);
})
.click('.clickable')
.then(([evResult, click]) => {
expect(click).toEqual(true);
});
});
it('should throw an error for elements that are not there', async () => {
return chrome
.click('.nope', { wait: false })
.then(res => expect(res).toBe(null))
.catch(error => {
expect(error).toMatchSnapshot();
});
});
});
describe('#focus', () => {
it('should focus an input', () => {
return chrome
.evaluate(() => {
var div = document.createElement('div');
div.innerHTML = `<input type="text" />`;
while (div.children.length > 0) {
document.body.appendChild(div.children[0]);
}
})
.focus('input')
.evaluate(() => {
return document.activeElement === document.querySelector('input');
})
.then(results => {
expect(results[2]).toEqual(true);
});
});
it('should wait for the element then focus', () => {
return chrome
.evaluate(() => {
setTimeout(() => {
var div = document.createElement('div');
div.innerHTML = `<input type="text" />`;
while (div.children.length > 0) {
document.body.appendChild(div.children[0]);
}
}, 5);
})
.focus('input')
.evaluate(() => {
return document.activeElement === document.querySelector('input');
})
.then(results => {
expect(results[2]).toEqual(true);
});
});
it("should throw an error if the element isn't there", () => {
return chrome
.focus('input', { wait: false })
.then(results => {
expect(results).toEqual(null);
})
.catch(error => {
expect(error).toMatchSnapshot();
});
});
});
describe('#type', () => {
it('should type text into an element', () => {
const text = 'Hello Goodbye';
return chrome
.evaluate(() => {
var div = document.createElement('div');
div.innerHTML = `<input type="text" />`;
while (div.children.length > 0) {
document.body.appendChild(div.children[0]);
}
})
.type('input', text)
.evaluate(() => {
return document.querySelector('input').value;
})
.then(results => {
expect(results[2]).toEqual(text);
});
});
it('should wait to type text into an element', () => {
const text = 'Hello Goodbye';
return chrome
.evaluate(() => {
setTimeout(() => {
var div = document.createElement('div');
div.innerHTML = `<input type="text" />`;
while (div.children.length > 0) {
document.body.appendChild(div.children[0]);
}
}, 5);
})
.type('input', text)
.evaluate(() => {
return document.querySelector('input').value;
})
.then(results => {
expect(results[2]).toEqual(text);
});
});
it('should should throw if the element never shows up', () => {
const text = 'Hello Goodbye';
return chrome
.type('input', text, { wait: false })
.then(results => {
expect(results).toEqual(null);
})
.catch(error => {
expect(error).toMatchSnapshot();
});
});
});
describe('#check', () => {
it('should check a checkbox', () => {
return chrome
.evaluate(() => {
var div = document.createElement('div');
div.innerHTML = `<input type="checkbox" />`;
while (div.children.length > 0) {
document.body.appendChild(div.children[0]);
}
})
.check('input')
.evaluate(() => {
return document.querySelector('input').checked;
})
.then(results => {
expect(results[2]).toEqual(true);
});
});
it('should wait to check a checkbox', () => {
return chrome
.evaluate(() => {
setTimeout(() => {
var div = document.createElement('div');
div.innerHTML = `<input type="checkbox" />`;
while (div.children.length > 0) {
document.body.appendChild(div.children[0]);
}
}, 5);
})
.check('input')
.evaluate(() => {
return document.querySelector('input').checked;
})
.then(results => {
expect(results[2]).toEqual(true);
});
});
it('should throw if the element is not found', () => {
return chrome
.check('input', { wait: false })
.then(res => {
expect(res).toEqual(null);
})
.catch(error => {
expect(error).toMatchSnapshot();
});
});
});
describe('#uncheck', () => {
it('should uncheck a checkbox', () => {
return chrome
.evaluate(() => {
var div = document.createElement('div');
div.innerHTML = `<input type="checkbox" checked/>`;
while (div.children.length > 0) {
document.body.appendChild(div.children[0]);
}
})
.uncheck('input')
.evaluate(() => {
return document.querySelector('input').checked;
})
.then(results => {
expect(results[2]).toEqual(false);
});
});
it('should wait to check a checkbox', () => {
return chrome
.evaluate(() => {
setTimeout(() => {
var div = document.createElement('div');
div.innerHTML = `<input type="checkbox" checked/>`;
while (div.children.length > 0) {
document.body.appendChild(div.children[0]);
}
}, 5);
})
.uncheck('input')
.evaluate(() => {
return document.querySelector('input').checked;
})
.then(results => {
expect(results[2]).toEqual(false);
});
});
it('should throw if the element is not found', () => {
return chrome
.uncheck('input', { wait: false })
.then(results => {
expect(results).toEqual(null);
})
.catch(error => {
expect(error).toMatchSnapshot();
});
});
});
});