/
test.js
397 lines (329 loc) · 17.3 KB
/
test.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
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
const { expect } = require('chai');
const createTestCafe = require('../../../../lib');
const path = require('path');
const assertionHelper = require('../../assertion-helper.js');
const { GREEN_PIXEL, RED_PIXEL } = require('../../assertion-helper');
const { readPngFile } = require('../../../../lib/utils/promisified-functions');
const config = require('../../config');
const { createWarningReporter } = require('../../utils/warning-reporter');
const SCREENSHOTS_PATH = config.testScreenshotsDir;
const experimentalDebug = !!process.env.EXPERIMENTAL_DEBUG;
let testCafeInstance = null;
async function assertScreenshotColor (fileName, pixel) {
for (const browser of config.currentEnvironment.browsers) {
const filePath = path.join(SCREENSHOTS_PATH, 'custom', browser.alias + fileName);
const png = await readPngFile(filePath);
expect(assertionHelper.hasPixel(png, pixel, 0, 0)).eql(true);
}
}
describe('Multiple windows', () => {
describe('Switch to the child window', () => {
it('Click on link', () => {
return runTests('testcafe-fixtures/switching-to-child/click-on-link.js', null, { only: 'chrome' });
});
it('Form submit', () => {
return runTests('testcafe-fixtures/switching-to-child/form-submit.js', null, { only: 'chrome' });
});
it('window.open', () => {
return runTests('testcafe-fixtures/switching-to-child/call-window-open.js', null, { only: 'chrome' });
});
it('Nested pages', () => {
return runTests('testcafe-fixtures/switching-to-child/nested-pages.js', null, { only: 'chrome' });
});
it('Cross domain', () => {
return runTests('testcafe-fixtures/switching-to-child/cross-domain.js', null, { only: 'chrome' });
});
});
describe('Switch to the parent window', () => {
it('"window.close" method call', () => {
return runTests('testcafe-fixtures/switching-to-parent/window-close-call.js', null, { only: 'chrome' });
});
});
describe('Cookie synchonization', () => {
it('cross-domain', () => {
return runTests('testcafe-fixtures/cookie-synchronization/cross-domain.js', null, { only: 'chrome' });
});
});
it('Console messages', () => {
return runTests('testcafe-fixtures/console/console-test.js', null, { only: 'chrome' });
});
it('Unhandled JavaScript errors', () => {
return runTests('testcafe-fixtures/handle-errors/handle-js-errors.js', null, { only: 'chrome', shouldFail: true })
.catch(errs => {
expect(errs[0]).to.contain('A JavaScript error occurred on "http://localhost:3000/fixtures/multiple-windows/pages/handle-errors/page-with-js-error.html"');
});
});
it('Close the window immediately after opening (GH-3762)', () => {
return runTests('testcafe-fixtures/close-window-immediately-after-opening.js', null, { only: 'chrome' });
});
it('headless', () => {
return createTestCafe('127.0.0.1', 1335, 1336)
.then(tc => {
testCafeInstance = tc;
})
.then(() => {
const fullTestPath = path.join(__dirname, './testcafe-fixtures/headless.js');
return testCafeInstance.createRunner()
.browsers(`chrome:headless`)
.src(fullTestPath)
.run();
})
.then(() => {
return testCafeInstance.close();
});
});
describe('Should not finalize some commands on driver starting (GH-4855)', () => {
it('ExecuteSelectorCommand', () => {
return runTests('testcafe-fixtures/i4855.js', 'ExecuteSelectorCommand', { only: 'chrome' });
});
it('ExecuteClientFunctionCommand', () => {
return runTests('testcafe-fixtures/i4855.js', 'ExecuteClientFunctionCommand', { only: 'chrome' });
});
});
it('Should correctly synchronize a cookie from a new same-domain window', () => {
return runTests('testcafe-fixtures/cookie-synchronization/same-domain.js', null, { only: 'chrome' });
});
if (!experimentalDebug) {
it('Should continue debugging when a child window closes', () => {
// NOTE: This test imitates the user clicks for TestCafe's debug UI.
// This scenario is not suitable for a run in compiler service due to
// the compiler service tests execute in the headless chrome
// and TestCafe's debug UI is not visible for end-user.
return runTests('testcafe-fixtures/debug-synchronization.js', null, { only: 'chrome' });
});
}
it('Should make screenshots of different windows', () => {
return runTests('testcafe-fixtures/features/screenshots.js', null, { setScreenshotPath: true })
.then(() => {
return assertScreenshotColor('0.png', RED_PIXEL);
})
.then(() => {
return assertScreenshotColor('1.png', GREEN_PIXEL);
})
.then(() => {
return assertScreenshotColor('2.png', RED_PIXEL);
})
.then(() => {
return assertScreenshotColor('3.png', GREEN_PIXEL);
})
.then(() => {
assertionHelper.removeScreenshotDir();
});
});
it('Should recreate close window watcher after new child window is opened', () => {
return runTests('testcafe-fixtures/i5857.js');
});
it('Should throw error if cannot restore child links', () => {
return runTests('testcafe-fixtures/i4760.js', null, { only: 'chrome', shouldFail: true })
.catch(errs => {
expect(errs[0]).to.contain('Failed to restore connection to window within the allocated timeout.');
});
});
it('Should switch to parent and close window if the file was downloaded in separate window', () => {
return runTests('testcafe-fixtures/i6242.js');
});
it('Should switch to child window if parent page has proxied image', () => {
return runTests('testcafe-fixtures/i6680.js');
});
it('Should not hang if switching to a window from iframe', () => {
return runTests('testcafe-fixtures/i6085.js');
});
describe('API', () => {
it('Open child window', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Open child window', { only: 'chrome' });
});
it('Open slow child window ', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Open slow child window', { only: 'chrome' });
});
it('Close current window', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Close current window', { only: 'chrome' });
});
it('Get current window', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Get current window', { only: 'chrome' });
});
it('Switch to parent window', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Switch to parent window', { only: 'chrome', speed: 0.01 });
});
it('Switch to unexisting parent window', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Switch to unexisting parent window', { only: 'chrome', shouldFail: true })
.catch(errs => {
expect(errs[0]).to.contain('Cannot find the parent window. Make sure that the tested window was opened from another window.');
});
});
it('Switch to unexisting window', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Switch to unexisting window', { only: 'chrome', shouldFail: true })
.catch(errs => {
expect(errs[0]).to.contain('Cannot find the window specified in the action parameters.');
});
});
it('Switch to child window', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Switch to child window', { only: 'chrome' });
});
it('Switch to window by url', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Switch to window by url', { only: 'chrome' });
});
it('Switch to window by title', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Switch to window by title', { only: 'chrome' });
});
it('Multiple windows are found warning', () => {
const { reporter, assertReporterWarnings, warningResult } = createWarningReporter();
return runTests('testcafe-fixtures/api/api-test.js', 'Multiple windows are found warning', { only: 'chrome', reporter })
.then(() => {
expect(warningResult.warnings.length).eql(1);
expect(warningResult.warnings[0].message).eql('The predicate function passed to the \'switchToWindow\' method matched multiple windows. The first matching window was activated.');
assertReporterWarnings('switchToWindow');
});
});
it('Switch to window by predicate with error', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Switch to window by predicate with error', { only: 'chrome', shouldFail: true })
.catch(errs => {
expect(errs[0]).to.contain('An error occurred inside the "switchToWindow" argument function. Error details: Cannot read property \'field\' of undefined');
});
});
it('Switch to previous window', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Switch to previous window', { only: 'chrome' });
});
it('Switch to previous closed window', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Switch to previous closed window', { only: 'chrome', shouldFail: true })
.catch(errs => {
expect(errs[0]).to.contain('Cannot find the previous window. Make sure that the previous window is opened.');
});
});
it('Switch to other child', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Switch to other child', { only: 'chrome' });
});
it('Switch to deep child', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Switch to deep child', { only: 'chrome' });
});
it('Close specific window from parent', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Close specific window from parent', { only: 'chrome' });
});
it('Close window and check master did not changed', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Close window and check master did not changed', { only: 'chrome' });
});
it('Close specific window from child', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Close specific window from child', { only: 'chrome', shouldFail: true })
.catch(errs => {
expect(errs[0]).to.contain('Cannot find the window specified in the action parameters.');
});
});
it('Close specific window and switch to it', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Close specific window and switch to it', { only: 'chrome', shouldFail: true })
.catch(errs => {
expect(errs[0]).to.contain('Cannot find the window specified in the action parameters.');
});
});
it('Close parent window and catch error', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Close parent window and catch error', { only: 'chrome', shouldFail: true })
.catch(errs => {
expect(errs[0]).to.contain('Cannot close a window that has an open child window.');
});
});
it('Close unexisting window', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Close unexisting window', { only: 'chrome', shouldFail: true })
.catch(errs => {
expect(errs[0]).to.contain('Cannot find the window specified in the action parameters');
});
});
it('Close unexisting child window', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Close unexisting child window', { only: 'chrome', shouldFail: true })
.catch(errs => {
expect(errs[0]).to.contain('Cannot find the window specified in the action parameters.');
});
});
it('Close closed window', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Close closed window', { only: 'chrome', shouldFail: true })
.catch(errs => {
expect(errs[0]).to.contain('Cannot find the window specified in the action parameters.');
});
});
it('Close window without parent', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Close window without parent', { only: 'chrome', shouldFail: true })
.catch(errs => {
expect(errs[0]).to.contain(
'Cannot close the window because it does not have a parent. The parent window was closed ' +
'or you are attempting to close the root browser window where tests were launched.'
);
});
});
it('Open window with `disableMultipleWindows` option', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Open window with `disableMultipleWindows` option', { only: 'chrome', disableMultipleWindows: true, shouldFail: true })
.catch(errs => {
expect(errs[0]).to.contain('Multi-window mode is disabled. To use the "openWindow" method, remove the "disableMultipleWindows" option.');
});
});
it('Refresh parent and switch to child', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Refresh parent and switch to child', { only: 'chrome' });
});
it('Refresh parent and remove child', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Refresh parent and remove child', { only: 'chrome' });
});
it('Refresh parent with multiple children', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Refresh parent with multiple children', { only: 'chrome' });
});
it('Refresh child and close', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Refresh child and close', { only: 'chrome' });
});
it('Refresh child and switch to parent', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Refresh child and switch to parent', { only: 'chrome' });
});
});
describe('iFrames', () => {
it('Should switch to child window if it is opened from iFrame', () => {
return runTests('testcafe-fixtures/iframe.js', 'Open child window from iframe');
});
it('Reload child window opened from iframe', () => {
return runTests('testcafe-fixtures/iframe.js', 'Reload child window opened from iframe');
});
});
describe('Emulation', () => {
it('Should resize window when emulating device', async () => {
return createTestCafe('127.0.0.1', 1335, 1336)
.then(tc => {
testCafeInstance = tc;
})
.then(() => {
return testCafeInstance
.createRunner()
.src(path.join(__dirname, './testcafe-fixtures/features/emulation.js'))
.browsers('chrome:emulation:device=iphone X')
.run();
})
.then(failedCount => {
expect(failedCount).eql(0);
return testCafeInstance.close();
});
});
});
describe('Resize', () => {
function runTestsResize (browser) {
return createTestCafe('127.0.0.1', 1335, 1336)
.then(tc => {
testCafeInstance = tc;
})
.then(() => {
return testCafeInstance
.createRunner()
.src(path.join(__dirname, './testcafe-fixtures/api/api-test.js'))
.filter(testName => testName === 'Resize multiple windows')
.browsers(browser)
.run();
})
.then(failedCount => {
expect(failedCount).eql(0);
return testCafeInstance.close();
});
}
it('Resize multiple windows', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Resize multiple windows');
});
it('Maximize multiple windows', () => {
return runTests('testcafe-fixtures/api/api-test.js', 'Maximize multiple windows');
});
it('Resize headless', () => {
return runTestsResize('firefox:headless').then(() => {
return runTestsResize('chrome:headless');
});
});
});
});