-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
navigation_spec.ts
529 lines (450 loc) · 23 KB
/
navigation_spec.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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
import { assertLogLength } from '../../../support/utils'
// makes logs coming from secondary origin work with `assertLogLength`
const reifyLogs = (logs) => {
return logs.map((attrs) => {
return {
get (name) {
return attrs[name]
},
}
})
}
describe('navigation events', () => {
let logs: any = []
beforeEach(() => {
logs = []
cy.on('log:added', (attrs, log) => {
logs.push(log)
})
cy.visit('/fixtures/multi-domain.html')
cy.get('a[data-cy="cross-origin-secondary-link"]').click()
})
describe('navigation:changed', () => {
it('navigation:changed via hashChange', () => {
cy.origin('http://foobar.com:3500', () => {
const afterNavigationChanged = new Promise<void>((resolve) => {
const listener = () => {
cy.location().should((loc) => {
expect(loc.host).to.equal('www.foobar.com:3500')
expect(loc.pathname).to.equal('/fixtures/multi-domain-secondary.html')
expect(loc.hash).to.equal('#hashChange')
})
resolve()
}
cy.once('navigation:changed', listener)
})
cy.get('a[data-cy="hashChange"]').click()
cy.wrap(afterNavigationChanged)
})
})
// TODO: this test should work but there seems to be a problem where the command queue ends prematurely
it.skip('navigates forward and back using history', () => {
cy.origin('http://foobar.com:3500', () => {
cy.get('a[data-cy="cross-origin-page"]').click()
.window().then((win) => {
return new Promise((resolve) => {
cy.once('navigation:changed', resolve)
win.history.back()
}).then(() => {
return new Promise((resolve) => {
cy.once('navigation:changed', resolve)
win.history.forward()
})
})
})
})
})
})
describe('window:load', () => {
it('reloads', () => {
cy.origin('http://foobar.com:3500', () => {
const logs: any[] = []
cy.on('log:added', (attrs, log) => {
logs.push(log)
})
const afterWindowLoad = new Promise<void>((resolve) => {
let times = 0
const listener = (win) => {
times++
expect(win.location.host).to.equal('www.foobar.com:3500')
expect(win.location.pathname).to.equal('/fixtures/multi-domain-secondary.html')
if (times === 2) {
cy.removeListener('window:load', listener)
resolve()
}
}
cy.on('window:load', listener)
})
cy.get('button[data-cy="reload"]').click()
cy.wrap(afterWindowLoad).then(() => {
return logs.map((log) => ({ name: log.get('name') }))
})
})
.then(reifyLogs)
.then((secondaryLogs) => {
assertLogLength(secondaryLogs, 9)
expect(secondaryLogs[5].get('name')).to.eq('page load')
})
})
it('navigates to a new page', () => {
cy.origin('http://foobar.com:3500', () => {
const logs: any[] = []
cy.on('log:added', (attrs, log) => {
logs.push(log)
})
const afterWindowLoad = new Promise<void>((resolve) => {
let times = 0
const listener = (win) => {
times++
if (times === 1) {
expect(win.location.host).to.equal('www.foobar.com:3500')
expect(win.location.pathname).to.equal('/fixtures/multi-domain-secondary.html')
}
if (times === 2) {
cy.removeListener('window:load', listener)
expect(win.location.host).to.equal('www.foobar.com:3500')
expect(win.location.pathname).to.equal('/fixtures/multi-domain.html')
resolve()
}
}
cy.on('window:load', listener)
})
cy.get('a[data-cy="cross-origin-page"]').click()
cy.get('a[data-cy="cross-origin-secondary-link').invoke('text').should('equal', 'http://www.foobar.com:3500/fixtures/multi-domain-secondary.html')
cy.wrap(afterWindowLoad).then(() => {
return logs.map((log) => ({ name: log.get('name'), message: log.get('message') }))
})
})
.then(reifyLogs)
.then((secondaryLogs) => {
assertLogLength(secondaryLogs, 13)
expect(secondaryLogs[5].get('name')).to.eq('page load')
expect(secondaryLogs[6].get('name')).to.eq('new url')
expect(secondaryLogs[6].get('message')).to.eq('http://www.foobar.com:3500/fixtures/multi-domain.html')
})
})
})
describe('url:changed', () => {
it('reloads', () => {
cy.origin('http://foobar.com:3500', () => {
const afterUrlChanged = new Promise<void>((resolve) => {
cy.once('url:changed', (url) => {
expect(url).to.equal('http://www.foobar.com:3500/fixtures/multi-domain-secondary.html')
resolve()
})
})
cy.get('button[data-cy="reload"]').click()
cy.wrap(afterUrlChanged)
})
})
it('navigates to a new page', () => {
cy.origin('http://foobar.com:3500', () => {
const afterUrlChanged = new Promise<void>((resolve) => {
let times = 0
const listener = (url) => {
times++
if (times === 1) {
expect(url).to.equal('http://www.foobar.com:3500/fixtures/multi-domain-secondary.html')
}
if (times === 2) {
cy.removeListener('url:changed', listener)
expect(url).to.equal('http://www.foobar.com:3500/fixtures/multi-domain.html')
resolve()
}
}
cy.on('url:changed', listener)
})
cy.get('a[data-cy="cross-origin-page"]').click()
cy.wrap(afterUrlChanged)
})
})
// TODO: this test should re revisited with the cypress in cypress tests available in 10.0
// https://github.com/cypress-io/cypress/issues/20973
it.skip('the runner url updates appropriately', () => {
cy.origin('http://foobar.com:3500', () => {
cy.get('a[data-cy="cross-origin-page"]').click()
})
})
})
})
// @ts-ignore / session support is needed for visiting about:blank between tests
describe('event timing', () => {
it('does not timeout when receiving a delaying:html event after cy.origin has started, but before the spec bridge is ready', () => {
cy.visit('/fixtures/multi-domain.html')
cy.get('a[data-cy="cross-origin-secondary-link"]').click()
cy.origin('http://foobar.com:3500', () => {
cy.log('inside cy.origin foobar')
})
// This command is run from localhost against the cross origin aut. Updating href is one of the few allowed commands. See https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy#location
cy.window().then((win) => {
win.location.href = 'http://www.idp.com:3500/fixtures/multi-domain.html'
})
cy.origin('http://idp.com:3500', () => {
cy.log('inside cy.origin idp')
})
})
})
// @ts-ignore / session support is needed for visiting about:blank between tests
describe('delayed navigation', { defaultCommandTimeout: 2000 }, () => {
it('localhost -> localhost', () => {
cy.visit('/fixtures/auth/delayedNavigate.html')
cy.get('[data-cy="to-localhost"]').click()
cy.get('[data-cy="login-idp"]')
})
it('localhost -> foobar, delay in', () => {
cy.visit('/fixtures/auth/delayedNavigate.html')
cy.get('[data-cy="to-foobar"]').click()
cy.origin('http://foobar.com:3500', () => {
cy.get('[data-cy="login-idp"]')
})
})
it('foobar -> localhost, delay out', () => {
cy.visit('/fixtures/auth/index.html')
cy.origin('http://foobar.com:3500', () => {
cy.visit('http://www.foobar.com:3500/fixtures/auth/delayedNavigate.html')
cy.get('[data-cy="to-localhost"]').click()
})
cy.get('[data-cy="login-idp"]')
})
it('foobar -> idp, delay out', () => {
cy.visit('/fixtures/auth/index.html')
cy.origin('http://foobar.com:3500', () => {
cy.visit('http://www.foobar.com:3500/fixtures/auth/delayedNavigate.html')
cy.get('[data-cy="to-idp"]').click()
})
cy.origin('http://idp.com:3500', () => {
cy.get('[data-cy="login-idp"]')
})
})
})
// @ts-ignore / session support is needed for visiting about:blank between tests
describe('errors', () => {
it('never calls cy.origin', { pageLoadTimeout: 5000 }, (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include(`Timed out after waiting \`5000ms\` for your remote page to load on origin(s):`)
expect(err.message).to.include(`\n- \`http://localhost:3500\`\n`)
expect(err.message).to.include(`Your page did not fire its \`load\` event within \`5000ms\`.`)
expect(err.message).to.include(`A cross origin request for \`http://www.foobar.com:3500/fixtures/auth/idp.html?redirect=http%3A%2F%2Flocalhost%3A3500%2Ffixtures%2Fauth%2Findex.html\` was detected.`)
expect(err.message).to.include(`A command that triggers cross origin navigation must be immediately followed by a \`cy.origin()\` command:`)
expect(err.message).to.include(`\`\ncy.origin(\'http://foobar.com:3500\', () => {\n <commands targeting http://www.foobar.com:3500 go here>\n})\n\``)
expect(err.message).to.include(`If the cross origin request was an intermediary state, you can try increasing the \`pageLoadTimeout\` value in \`cypress.json\` to wait longer`)
expect(err.message).not.to.include(`The following error originated from your test code, not from Cypress`)
expect(err.message).not.to.include(`The following error originated from your application code, not from Cypress`)
done()
})
cy.visit('/fixtures/auth/index.html')
cy.get('[data-cy="login-foobar"]').click() // Timeout on page load here, we never reach the expected origin
cy.get('[data-cy="login-foobar"]')
})
it('never redirects to the cross origin', { defaultCommandTimeout: 50 }, (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include(`Timed out retrying after 50ms: Expected to find element: \`[data-cy="username"]\`, but never found it`)
expect(err.message).to.include(`The command was expected to run against origin: \`http://idp.com:3500\` but the application is at origin: \`http://localhost:3500\`.`)
// make sure that the secondary origin failures do NOT show up as spec failures or AUT failures
expect(err.message).not.to.include(`The following error originated from your test code, not from Cypress`)
expect(err.message).not.to.include(`The following error originated from your application code, not from Cypress`)
done()
})
cy.visit('/fixtures/auth/index.html')
cy.get('[data-cy="login-idp"]')
cy.origin('http://idp.com:3500', () => {
cy.get('[data-cy="username"]').type('BJohnson') // Timeout here on command, cannot find element
cy.get('[data-cy="login"]').click()
})
cy.get('[data-cy="welcome"]')
.invoke('text')
.should('equal', 'Welcome BJohnson')
})
it('redirects to the wrong cross origin', { pageLoadTimeout: 5000 }, (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include(`Timed out after waiting \`5000ms\` for your remote page to load on origin(s):`)
expect(err.message).to.include(`\n- \`http://idp.com:3500\`\n`)
expect(err.message).to.include(`A cross origin request for \`http://www.foobar.com:3500/fixtures/auth/idp.html?redirect=http%3A%2F%2Flocalhost%3A3500%2Ffixtures%2Fauth%2Findex.html\` was detected.`)
expect(err.message).to.include(`\`\ncy.origin(\'http://foobar.com:3500\', () => {\n <commands targeting http://www.foobar.com:3500 go here>\n})\n\``)
expect(err.message).not.to.include(`The following error originated from your test code, not from Cypress`)
expect(err.message).not.to.include(`The following error originated from your application code, not from Cypress`)
done()
})
cy.visit('/fixtures/auth/index.html')
cy.get('[data-cy="login-foobar"]').click() // Timeout on page load here, we never reach the expected origin
cy.origin('http://idp.com:3500', () => {
cy.get('[data-cy="username"]').type('BJohnson')
cy.get('[data-cy="login"]').click()
})
// Verify that the user has logged in on localhost
cy.get('[data-cy="welcome"]')
.invoke('text')
.should('equal', 'Welcome BJohnson')
})
it('never returns to the primary origin', { defaultCommandTimeout: 50 }, (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include(`Timed out retrying after 50ms: Expected to find element: \`[data-cy="welcome"]\`, but never found it`)
expect(err.message).to.include(`The command was expected to run against origin: \`http://localhost:3500\` but the application is at origin: \`http://idp.com:3500\`.`)
// make sure that the secondary origin failures do NOT show up as spec failures or AUT failures
expect(err.message).not.to.include(`The following error originated from your test code, not from Cypress`)
expect(err.message).not.to.include(`The following error originated from your application code, not from Cypress`)
done()
})
cy.visit('/fixtures/auth/index.html')
cy.get('[data-cy="login-idp"]').click()
cy.origin('http://idp.com:3500', () => {
cy.get('[data-cy="username"]').type('BJohnson')
}) // cy.origin is stable so the command exits
// Verify that the user has logged in on localhost
cy.get('[data-cy="welcome"]') // Timeout here on command, cannot find element
.invoke('text')
.should('equal', 'Welcome BJohnson')
})
it('redirects to an unexpected cross origin', { pageLoadTimeout: 5000 }, (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include(`Timed out after waiting \`5000ms\` for your remote page to load on origin(s):`)
expect(err.message).to.include(`\n- \`http://localhost:3500\`\n`)
expect(err.message).to.include(`A cross origin request for \`http://www.foobar.com:3500/fixtures/auth/index.html\` was detected.`)
expect(err.message).to.include(`\`\ncy.origin(\'http://foobar.com:3500\', () => {\n <commands targeting http://www.foobar.com:3500 go here>\n})\n\``)
expect(err.message).not.to.include(`The following error originated from your test code, not from Cypress`)
expect(err.message).not.to.include(`The following error originated from your application code, not from Cypress`)
done()
})
cy.visit('/fixtures/auth/index.html')
cy.get('[data-cy="login-idp"]').click()
cy.origin('http://idp.com:3500', () => {
cy.get('[data-cy="username"]').type('BJohnson')
cy.window().then((win) => {
win.location.href = 'http://www.foobar.com:3500/fixtures/auth/index.html'
})
})
// Verify that the user has logged in on localhost
cy.get('[data-cy="welcome"]') // Stability is false, this command is prevented from running until stability is achieved.
.invoke('text')
.should('equal', 'Welcome BJohnson')
})
it('redirects to an unexpected cross origin and calls another command in the cy.origin command', { pageLoadTimeout: 5000 }, (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include(`Timed out after waiting \`5000ms\` for your remote page to load on origin(s):`)
expect(err.message).to.include(`\n- \`http://idp.com:3500\`\n`)
expect(err.message).to.include(`A cross origin request for \`http://www.foobar.com:3500/fixtures/auth/index.html\` was detected.`)
expect(err.message).to.include(`\`\ncy.origin(\'http://foobar.com:3500\', () => {\n <commands targeting http://www.foobar.com:3500 go here>\n})\n\``)
expect(err.message).not.to.include(`The following error originated from your test code, not from Cypress`)
expect(err.message).not.to.include(`The following error originated from your application code, not from Cypress`)
done()
})
cy.visit('/fixtures/auth/index.html')
cy.get('[data-cy="login-idp"]').click()
cy.origin('http://idp.com:3500', () => {
cy.get('[data-cy="username"]').type('BJohnson')
cy.window().then((win) => {
win.location.href = 'http://www.foobar.com:3500/fixtures/auth/index.html'
})
cy.get('[data-cy="welcome"]') // Timeout here on command, cannot find element
})
// Verify that the user has logged in on localhost
cy.get('[data-cy="welcome"]')
.invoke('text')
.should('equal', 'Welcome BJohnson')
})
it('fails in cy.origin when a command is run after we return to localhost', { defaultCommandTimeout: 50 }, (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include(`Timed out retrying after 50ms: Expected to find element: \`[data-cy="cannot_find"]\`, but never found it`)
expect(err.message).to.include(`The command was expected to run against origin: \`http://idp.com:3500\` but the application is at origin: \`http://localhost:3500\`.`)
// make sure that the secondary origin failures do NOT show up as spec failures or AUT failures
expect(err.message).not.to.include(`The following error originated from your test code, not from Cypress`)
expect(err.message).not.to.include(`The following error originated from your application code, not from Cypress`)
done()
})
cy.visit('/fixtures/auth/index.html') // Establishes primary origin
cy.get('[data-cy="login-idp"]').click() // Takes you to idp.com
cy.origin('http://idp.com:3500', () => {
cy.get('[data-cy="username"]').type('BJohnson')
cy.get('[data-cy="login"]').click()
cy.get('[data-cy="cannot_find"]') // Timeout here on command stability achieved by primary origin, this command times out.
})
// Verify that the user has logged in on localhost
cy.get('[data-cy="welcome"]')
.invoke('text')
.should('equal', 'Welcome BJohnson')
})
it('fails with a normal timeout', { defaultCommandTimeout: 50 }, (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include(`Timed out retrying after 50ms: Expected to find element: \`[data-cy="cannot_find"]\`, but never found it`)
// make sure that the secondary origin failures do NOT show up as spec failures or AUT failures
expect(err.message).not.to.include(`The following error originated from your test code, not from Cypress`)
expect(err.message).not.to.include(`The following error originated from your application code, not from Cypress`)
expect(err.message).not.to.include(`The command was expected to run against origin:`)
done()
})
cy.visit('/fixtures/auth/index.html') // Establishes primary origin
cy.get('[data-cy="login-idp"]').click() // Takes you to idp.com
cy.origin('http://idp.com:3500', () => {
cy.get('[data-cy="cannot_find"]') // Timeout here on command stability achieved by primary origin, this command times out.
})
})
describe('Pre established spec bridge', () => {
// These next three tests test and edge case where we want to prevent a load event from an established spec bridge that is not part of the test.
// This test removes the foobar spec bridge, navigates to idp, then navigates to foobar and attempts to access selectors on localhost.
it('times out in cy.origin with foobar spec bridge undefined', { pageLoadTimeout: 5000 }, (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include(`Timed out after waiting \`5000ms\` for your remote page to load on origin(s):`)
expect(err.message).to.include(`\n- \`http://localhost:3500\`\n`)
expect(err.message).to.include(`A cross origin request for \`http://www.foobar.com:3500/fixtures/auth/index.html\` was detected.`)
expect(err.message).to.include(`\`\ncy.origin(\'http://foobar.com:3500\', () => {\n <commands targeting http://www.foobar.com:3500 go here>\n})\n\``)
expect(err.message).not.to.include(`The following error originated from your test code, not from Cypress`)
expect(err.message).not.to.include(`The following error originated from your application code, not from Cypress`)
done()
})
cy.visit('/fixtures/auth/index.html') // Establishes primary origin
cy.window().then(() => {
// Force remove the spec bridge
window?.top?.document.getElementById('Spec Bridge: foobar.com')?.remove()
})
cy.get('[data-cy="login-idp"]').click() // Takes you to idp.com
cy.origin('http://idp.com:3500', () => {
cy.get('[data-cy="username"]').type('BJohnson')
cy.window().then((win) => {
win.location.href = 'http://www.foobar.com:3500/fixtures/auth/index.html'
})//
})
// Verify that the user has logged in on localhost
cy.get('[data-cy="welcome"]') // Stability is false, this command is prevented from running until stability is achieved.
.invoke('text')
.should('equal', 'Welcome BJohnson')
})
// this test just needs to establish the foobar spec bridge.
it('establishes foobar spec bridge', () => {
cy.visit('/fixtures/auth/index.html') // Establishes primary origin
cy.get('[data-cy="login-foobar"]').click() // Takes you to idp.com
cy.origin('http://foobar.com:3500', () => {
cy.get('[data-cy="username"]').type('BJohnson')
cy.get('[data-cy="login"]').click()
})
// Verify that the user has logged in on localhost
cy.get('[data-cy="welcome"]')
.invoke('text')
.should('equal', 'Welcome BJohnson')
})
// This test is the same as the first test but the foobar spec bridge has been established and we expect it to behave the same as the first test.
// The primary origin should ignore the load event from the foobar spec bridge and load should timeout in the idp cy.origin command..
it('times out in cy.origin with foobar spec bridge defined', { pageLoadTimeout: 5000 }, (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include(`Timed out after waiting \`5000ms\` for your remote page to load on origin(s):`)
expect(err.message).to.include(`\n- \`http://localhost:3500\`\n`)
expect(err.message).to.include(`A cross origin request for \`http://www.foobar.com:3500/fixtures/auth/index.html\` was detected.`)
expect(err.message).to.include(`\`\ncy.origin(\'http://foobar.com:3500\', () => {\n <commands targeting http://www.foobar.com:3500 go here>\n})\n\``)
expect(err.message).not.to.include(`The following error originated from your test code, not from Cypress`)
expect(err.message).not.to.include(`The following error originated from your application code, not from Cypress`)
done()
})
cy.visit('/fixtures/auth/index.html') // Establishes primary origin
cy.get('[data-cy="login-idp"]').click() // Takes you to idp.com
cy.origin('http://idp.com:3500', () => {
cy.get('[data-cy="username"]').type('BJohnson')
cy.window().then((win) => {
win.location.href = 'http://www.foobar.com:3500/fixtures/auth/index.html'
})
})
// Verify that the user has logged in on localhost
cy.get('[data-cy="welcome"]') // Stability is false, this command is prevented from running until stability is achieved.
.invoke('text')
.should('equal', 'Welcome BJohnson')
})
})
})