/
multi_domain_navigation.spec.ts
499 lines (399 loc) · 19.2 KB
/
multi_domain_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
const { stripIndent } = require('common-tags')
context('cy.origin navigation', () => {
it('.go()', () => {
cy.visit('/fixtures/multi-domain.html')
cy.get('a[data-cy="cross-origin-secondary-link"]').click()
cy.origin('http://foobar.com:3500', () => {
cy.visit('http://www.foobar.com:3500/fixtures/dom.html')
cy.go('back')
cy.location('pathname').should('include', 'multi-domain-secondary.html')
cy.go('forward')
cy.location('pathname').should('include', 'dom.html')
})
})
it('.reload()', () => {
cy.visit('/fixtures/multi-domain.html')
cy.get('a[data-cy="dom-link"]').click()
cy.origin('http://foobar.com:3500', () => {
cy.get(':checkbox[name="colors"][value="blue"]').check().should('be.checked')
cy.reload()
cy.get(':checkbox[name="colors"][value="blue"]').should('not.be.checked')
})
})
context('.visit()', () => {
it('calls the correct load handlers', () => {
const primaryCyBeforeLoadSpy = cy.spy()
const primaryCyLoadSpy = cy.spy()
const primaryVisitBeforeLoadSpy = cy.spy()
const primaryVisitLoadSpy = cy.spy()
cy.on('window:before:load', primaryCyBeforeLoadSpy)
cy.on('window:load', primaryCyLoadSpy)
cy.visit('/fixtures/multi-domain.html', {
onBeforeLoad: primaryVisitBeforeLoadSpy,
onLoad: primaryVisitLoadSpy,
}).then(() => {
expect(primaryCyBeforeLoadSpy).to.be.calledOnce
expect(primaryCyLoadSpy).to.be.calledTwice // twice because it's also called for 'about:blank'
expect(primaryVisitBeforeLoadSpy).to.be.calledOnce
expect(primaryVisitLoadSpy).to.be.calledOnce
})
cy.origin('http://foobar.com:3500', () => {
const secondaryCyBeforeLoadSpy = cy.spy()
const secondaryCyLoadSpy = cy.spy()
const secondaryVisitBeforeLoadSpy = cy.spy()
const secondaryVisitLoadSpy = cy.spy()
cy.on('window:before:load', secondaryCyBeforeLoadSpy)
cy.on('window:load', secondaryCyLoadSpy)
cy.visit('http://www.foobar.com:3500/fixtures/dom.html', {
onBeforeLoad: secondaryVisitBeforeLoadSpy,
onLoad: secondaryVisitLoadSpy,
}).then(() => {
expect(secondaryCyBeforeLoadSpy).to.be.calledOnce
expect(secondaryCyLoadSpy).to.be.calledOnce
expect(secondaryVisitBeforeLoadSpy).to.be.calledOnce
expect(secondaryVisitLoadSpy).to.be.calledOnce
})
}).then(() => {
expect(primaryCyBeforeLoadSpy).to.be.calledOnce
expect(primaryCyLoadSpy).to.be.calledTwice
expect(primaryVisitBeforeLoadSpy).to.be.calledOnce
expect(primaryVisitLoadSpy).to.be.calledOnce
})
})
it('supports visiting primary first', () => {
cy.visit('/fixtures/multi-domain.html')
cy.origin('http://foobar.com:3500', () => {
cy.visit('http://www.foobar.com:3500/fixtures/multi-domain-secondary.html')
cy.get('[data-cy="dom-check"]').should('have.text', 'From a secondary origin')
})
})
it('supports skipping visiting primary first', () => {
cy.origin('http://foobar.com:3500', () => {
cy.visit('http://www.foobar.com:3500/fixtures/multi-domain-secondary.html')
cy.get('[data-cy="dom-check"]').should('have.text', 'From a secondary origin')
})
})
// TODO: we don't support nested cy.origin yet...
it.skip('supports nesting a third origin', () => {
cy.visit('/fixtures/multi-domain.html')
cy.origin('http://foobar.com:3500', () => {
cy.visit('http://www.foobar.com:3500/fixtures/multi-domain-secondary.html')
cy.get('[data-cy="dom-check"]').should('have.text', 'From a secondary origin')
cy.origin('http://idp.com:3500', () => {
cy.visit('http://www.idp.com:3500/fixtures/dom.html')
})
})
})
it('supports navigating to secondary through button and then visiting', () => {
cy.visit('/fixtures/multi-domain.html')
cy.get('a[data-cy="cross-origin-secondary-link"]').click()
cy.origin('http://foobar.com:3500', () => {
cy.get('[data-cy="dom-check"]').should('have.text', 'From a secondary origin')
cy.visit('http://www.foobar.com:3500/fixtures/dom.html')
cy.location('href').should('equal', 'http://www.foobar.com:3500/fixtures/dom.html')
})
})
it('supports relative urls within secondary', () => {
cy.origin('http://www.foobar.com:3500', () => {
cy.visit('/fixtures/dom.html')
cy.location('href').should('equal', 'http://www.foobar.com:3500/fixtures/dom.html')
})
})
it('supports relative urls with path within secondary', () => {
cy.origin('http://www.foobar.com:3500/fixtures', () => {
cy.visit('/dom.html')
cy.location('href').should('equal', 'http://www.foobar.com:3500/fixtures/dom.html')
})
})
it('supports relative urls with hash within secondary', () => {
cy.origin('http://www.foobar.com:3500/#hash', () => {
cy.visit('/more-hash')
cy.location('href').should('equal', 'http://www.foobar.com:3500/#hash/more-hash')
})
})
it('supports relative urls with path and hash within secondary', () => {
cy.origin('http://www.foobar.com:3500/welcome/#hash', () => {
cy.visit('/more-hash')
cy.location('href').should('equal', 'http://www.foobar.com:3500/welcome/#hash/more-hash')
})
})
it('supports hash change within secondary', () => {
cy.visit('/fixtures/multi-domain.html')
cy.get('a[data-cy="cross-origin-secondary-link"]').click()
cy.origin('http://foobar.com:3500', () => {
cy.visit('http://www.foobar.com:3500/fixtures/multi-domain-secondary.html#hashchange')
cy.location('hash').should('equal', '#hashchange')
})
})
it('navigates back to primary', () => {
cy.origin('http://foobar.com:3500', () => {
cy.visit('http://www.foobar.com:3500/fixtures/multi-domain-secondary.html')
cy.get('[data-cy="dom-check"]').should('have.text', 'From a secondary origin')
})
cy.visit('/fixtures/multi-domain.html')
cy.get('a[data-cy="cross-origin-secondary-link"]').should('have.text', 'http://www.foobar.com:3500/fixtures/multi-domain-secondary.html')
cy.location('href').should('equal', 'http://localhost:3500/fixtures/multi-domain.html')
})
it('errors when visiting a new origin within origin', (done) => {
cy.on('fail', (e) => {
expect(e.message).to.equal(stripIndent`\
\`cy.visit()\` failed because you are attempting to visit a URL that is of a different origin.\n
You likely forgot to use \`cy.origin()\`:\n
\`cy.origin('http://foobar.com:3500', () => {\`
\` cy.visit('http://www.foobar.com:3500/fixtures/multi-domain-secondary.html')\`
\` <commands targeting http://www.foobar.com:3500 go here>\`
\`})\`\n
\`cy.origin('http://idp.com:3500', () => {\`
\` cy.visit('http://www.idp.com:3500/fixtures/dom.html')\`
\` <commands targeting http://www.idp.com:3500 go here>\`
\`})\`\n
The new URL is considered a different origin because the following parts of the URL are different:\n
> superdomain\n
You may only \`cy.visit()\` same-origin URLs within \`cy.origin()\`.\n
The previous URL you visited was:\n
> 'http://www.foobar.com:3500'\n
You're attempting to visit this URL:\n
> 'http://www.idp.com:3500'`)
done()
})
cy.visit('/fixtures/multi-domain.html')
cy.get('a[data-cy="cross-origin-secondary-link"]').click()
cy.origin('http://foobar.com:3500', () => {
// this call should error since we can't visit a cross-origin
cy.visit('http://www.idp.com:3500/fixtures/dom.html')
})
})
// @ts-ignore
it('informs user to use cy.origin with experimental flag off', { experimentalSessionAndOrigin: false }, (done) => {
cy.on('fail', (e) => {
expect(e.message).to.equal(stripIndent`\
\`cy.visit()\` failed because you are attempting to visit a URL that is of a different origin.\n
In order to visit a different origin, you can enable the \`experimentalSessionAndOrigin\` flag and use \`cy.origin()\`:\n
\`cy.visit('http://localhost:3500/fixtures/multi-domain.html')\`
\`<commands targeting http://localhost:3500 go here>\`\n
\`cy.origin('http://foobar.com:3500', () => {\`
\` cy.visit('http://www.foobar.com:3500/fixtures/dom.html')\`
\` <commands targeting http://www.foobar.com:3500 go here>\`
\`})\`\n
The new URL is considered a different origin because the following parts of the URL are different:\n
> superdomain\n
You may only \`cy.visit()\` same-origin URLs within a single test.\n
The previous URL you visited was:\n
> 'http://localhost:3500'\n
You're attempting to visit this URL:\n
> 'http://www.foobar.com:3500'`)
done()
})
cy.visit('/fixtures/multi-domain.html')
// this call should error since we can't visit a cross-origin
cy.visit('http://www.foobar.com:3500/fixtures/dom.html')
})
it('supports the query string option', () => {
cy.visit('/fixtures/multi-domain.html')
cy.origin('http://foobar.com:3500', () => {
cy.visit('http://www.foobar.com:3500/fixtures/multi-domain-secondary.html', { qs: { foo: 'bar' } })
cy.location('search').should('equal', '?foo=bar')
})
})
it('can send a POST request', () => {
cy.origin('http://foobar.com:3500', () => {
cy.visit('http://www.foobar.com:3500/post-only', {
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
bar: 'baz',
}),
})
cy.contains('it worked!').contains('{"bar":"baz"}')
})
})
it('succeeds when the AUT window in the secondary is undefined', () => {
// manually remove the spec bridge iframe to ensure Cypress.state('window') is not already set
window.top?.document.getElementById('Spec\ Bridge:\ foobar.com')?.remove()
cy.visit('/fixtures/multi-domain.html')
cy.origin('http://foobar.com:3500', () => {
cy.visit('http://www.foobar.com:3500/fixtures/multi-domain-secondary.html')
cy.get('[data-cy="dom-check"]').should('have.text', 'From a secondary origin')
})
})
it('succeeds when the secondary is already defined but the AUT is still on the primary', () => {
// setup the secondary to be on the secondary origin
cy.origin('http://foobar.com:3500', () => {
cy.visit('http://www.foobar.com:3500/fixtures/multi-domain-secondary.html')
cy.get('[data-cy="dom-check"]').should('have.text', 'From a secondary origin')
})
// update the AUT to be on the primary origin
cy.visit('/fixtures/multi-domain.html')
// verify there aren't any issues when the AUT is on primary but the spec bridge is on secondary (cross-origin)
cy.origin('http://foobar.com:3500', () => {
cy.visit('http://www.foobar.com:3500/fixtures/multi-domain-secondary.html')
cy.get('[data-cy="dom-check"]').should('have.text', 'From a secondary origin')
})
})
it('does not navigate to about:blank in secondary', () => {
const primaryCyLoadSpy = cy.spy()
cy.on('window:load', primaryCyLoadSpy)
cy.origin('http://foobar.com:3500', () => {
const secondaryCyLoadSpy = cy.spy()
cy.on('window:load', secondaryCyLoadSpy)
cy.visit('http://www.foobar.com:3500/fixtures/multi-domain-secondary.html').then(() => {
expect(secondaryCyLoadSpy).to.have.been.calledOnce
expect(secondaryCyLoadSpy.args[0][0].location.href).to.equal('http://www.foobar.com:3500/fixtures/multi-domain-secondary.html')
})
})
cy.then(() => {
expect(primaryCyLoadSpy).to.not.have.been.called
})
})
it('supports redirecting from primary to secondary in cy.origin', () => {
cy.visit('/fixtures/multi-domain.html')
cy.origin('http://www.foobar.com:3500', () => {
cy.visit('http://localhost:3500/redirect?href=http://www.foobar.com:3500/fixtures/multi-domain-secondary.html')
cy.get('[data-cy="dom-check"]').should('have.text', 'From a secondary origin')
})
})
it('supports redirecting from secondary to primary outside of cy.origin', () => {
cy.visit('/fixtures/multi-domain.html')
cy.visit('http://www.foobar.com:3500/redirect?href=http://localhost:3500/fixtures/generic.html')
})
it('errors when trying to redirect from secondary to primary in cy.origin', (done) => {
cy.on('fail', (e) => {
expect(e.message).to.equal(stripIndent`
\`cy.visit()\` failed because you are attempting to visit a URL from a previous origin inside of \`cy.origin()\`.\n
Instead of placing the \`cy.visit()\` inside of \`cy.origin()\`, the \`cy.visit()\` should be placed outside of the \`cy.origin()\` block.\n
\`<commands targeting http://localhost:3500 go here>\`\n
\`cy.origin('http://foobar.com:3500', () => {\`
\` <commands targeting http://foobar.com:3500 go here>\`
\`})\`\n
\`cy.visit('http://www.foobar.com:3500/redirect?href=http://localhost:3500/fixtures/generic.html')\``)
done()
})
cy.visit('http://localhost:3500/fixtures/multi-domain.html')
cy.origin('http://www.foobar.com:3500', () => {
cy.visit('/redirect?href=http://localhost:3500/fixtures/generic.html')
})
})
it('errors when trying to visit primary in cy.origin', (done) => {
cy.on('fail', (e) => {
expect(e.message).to.equal(stripIndent`
\`cy.visit()\` failed because you are attempting to visit a URL from a previous origin inside of \`cy.origin()\`.\n
Instead of placing the \`cy.visit()\` inside of \`cy.origin()\`, the \`cy.visit()\` should be placed outside of the \`cy.origin()\` block.\n
\`<commands targeting http://localhost:3500 go here>\`\n
\`cy.origin('http://foobar.com:3500', () => {\`
\` <commands targeting http://foobar.com:3500 go here>\`
\`})\`\n
\`cy.visit('http://localhost:3500/fixtures/generic.html')\``)
done()
})
cy.visit('http://localhost:3500/fixtures/multi-domain.html')
cy.origin('http://www.foobar.com:3500', () => {
cy.visit('http://localhost:3500/fixtures/generic.html')
})
})
it('errors when trying to redirect from primary to secondary outside of cy.origin', (done) => {
cy.on('fail', (e) => {
expect(e.message).to.equal(stripIndent`\
\`cy.visit()\` failed because you are attempting to visit a URL that is of a different origin.\n
You likely forgot to use \`cy.origin()\`:\n
\`cy.visit('http://localhost:3500/fixtures/multi-domain.html')\`
\`<commands targeting http://localhost:3500 go here>\`\n
\`cy.origin('http://foobar.com:3500', () => {\`
\` cy.visit('http://localhost:3500/redirect?href=http://www.foobar.com:3500/fixtures/generic.html')\`
\` <commands targeting http://www.foobar.com:3500 go here>\`
\`})\`\n
The new URL is considered a different origin because the following parts of the URL are different:\n
> superdomain\n
You may only \`cy.visit()\` same-origin URLs within a single test.\n
The previous URL you visited was:\n
> 'http://localhost:3500'\n
You're attempting to visit this URL:\n
> 'http://www.foobar.com:3500'`)
done()
})
cy.visit('/fixtures/multi-domain.html')
cy.visit('http://localhost:3500/redirect?href=http://www.foobar.com:3500/fixtures/generic.html')
})
it('supports auth options and adding auth to subsequent requests', () => {
cy.origin('http://foobar.com:3500', () => {
cy.visit('http://www.foobar.com:3500/basic_auth', {
auth: {
username: 'cypress',
password: 'password123',
},
})
cy.get('body').should('have.text', 'basic auth worked')
cy.window().then((win) => {
win.location.href = 'http://www.foobar.com:3500/basic_auth'
})
cy.get('body').should('have.text', 'basic auth worked')
})
// attaches the auth options for the foobar origin even from another origin
cy.origin('http://www.idp.com:3500', () => {
cy.visit('/fixtures/multi-domain.html')
cy.window().then((win) => {
win.location.href = 'http://www.foobar.com:3500/basic_auth'
})
})
cy.origin('http://foobar.com:3500', () => {
cy.get('body').should('have.text', 'basic auth worked')
})
cy.visit('/fixtures/multi-domain.html')
// attaches the auth options for the foobar origin from the top-level
cy.window().then((win) => {
win.location.href = 'http://www.foobar.com:3500/basic_auth'
})
cy.origin('http://foobar.com:3500', () => {
cy.get('body').should('have.text', 'basic auth worked')
})
})
it('does not propagate the auth options across tests', (done) => {
cy.intercept('/basic_auth', (req) => {
req.on('response', (res) => {
// clear the www-authenticate header so the browser doesn't prompt for username/password
res.headers['www-authenticate'] = ''
expect(res.statusCode).to.equal(401)
done()
})
})
cy.window().then((win) => {
win.location.href = 'http://www.foobar.com:3500/fixtures/multi-domain.html'
})
cy.origin('http://foobar.com:3500', () => {
cy.window().then((win) => {
win.location.href = 'http://www.foobar.com:3500/basic_auth'
})
})
})
it('succeeds when visiting local file server first', { baseUrl: undefined }, () => {
cy.visit('cypress/fixtures/multi-domain.html')
cy.origin('http://www.foobar.com:3500', () => {
cy.visit('/fixtures/multi-domain-secondary.html')
cy.get('[data-cy="dom-check"]').should('have.text', 'From a secondary origin')
})
})
it('handles visit failures', { baseUrl: undefined }, (done) => {
cy.on('fail', (e) => {
expect(e.message).to.include('failed trying to load:\n\nhttp://www.foobar.com:3500/fixtures/multi-domain-secondary.html')
expect(e.message).to.include('500: Internal Server Error')
done()
})
cy.intercept('*/multi-domain-secondary.html', { statusCode: 500 })
cy.visit('cypress/fixtures/multi-domain.html')
cy.origin('http://www.foobar.com:3500', () => {
cy.visit('fixtures/multi-domain-secondary.html')
})
})
})
it('supports navigating through changing the window.location.href', () => {
cy.visit('/fixtures/multi-domain.html')
cy.get('a[data-cy="cross-origin-secondary-link"]').click()
cy.origin('http://foobar.com:3500', () => {
cy.window().then((win) => {
win.location.href = 'http://www.foobar.com:3500/fixtures/dom.html'
})
cy.location('pathname').should('equal', '/fixtures/dom.html')
})
})
})