/
origin.cy.ts
373 lines (316 loc) · 14.2 KB
/
origin.cy.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
describe('cy.origin', { browser: '!webkit' }, () => {
it('successfully visits after creating 30 spec bridges', () => {
// Make ~30 spec bridges
for (let index = 0; index < 30; index++) {
cy.origin(`http://www.${index}.com:3500`, () => undefined)
}
cy.origin('http://www.app.foobar.com:3500', () => {
cy.visit('/fixtures/primary-origin.html')
})
})
it('creates and injects into google subdomains', () => {
// Intercept google to keep our tests independent from google.
cy.intercept('https://www.google.com', {
body: '<html><head><title></title></head><body><p>google.com</p></body></html>',
})
cy.intercept('https://accounts.google.com', {
body: '<html><head><title></title></head><body><p>accounts.google.com</p></body></html>',
})
cy.visit('https://www.google.com')
cy.visit('https://accounts.google.com')
cy.origin('https://accounts.google.com', () => {
cy.window().then((win) => {
expect(win.Cypress).to.not.be.undefined
})
})
})
it('creates and injects into google subdomains when visiting in an origin block', () => {
// Intercept google to keep our tests independent from google.
cy.intercept('https://www.google.com', {
body: '<html><head><title></title></head><body><p>google.com</p></body></html>',
})
cy.intercept('https://accounts.google.com', {
body: '<html><head><title></title></head><body><p>accounts.google.com</p></body></html>',
})
cy.visit('https://www.google.com')
cy.origin('https://accounts.google.com', () => {
cy.visit('https://accounts.google.com')
cy.window().then((win) => {
expect(win.Cypress).to.not.be.undefined
})
})
})
it('passes viewportWidth/Height state to the secondary origin', () => {
const expectedViewport = [320, 480]
cy.viewport(320, 480).then(() => {
const primaryViewport = [cy.state('viewportWidth'), cy.state('viewportHeight')]
expect(primaryViewport).to.deep.equal(expectedViewport)
})
cy.visit('/fixtures/primary-origin.html')
cy.get('a[data-cy="cross-origin-secondary-link"]').click()
cy.origin('http://www.foobar.com:3500', { args: expectedViewport }, (expectedViewport) => {
const secondaryViewport = [cy.state('viewportWidth'), cy.state('viewportHeight')]
expect(secondaryViewport).to.deep.equal(expectedViewport)
})
})
describe('ensure that visiting a cross origin page with a cy.origin command with no cy commands may cause subsequent tests to hang.', () => {
it('executes quickly', () => {
cy.visit('/fixtures/primary-origin.html')
cy.get('a[data-cy="cross-origin-secondary-link"]').click()
cy.origin('http://www.foobar.com:3500', () => {
expect(true).to.equal(true)
})
})
it('does not hang', () => {
cy.log('I log correctly')
})
})
describe('async attach', () => {
it('attaches to an origin at any time', () => {
cy.visit('/fixtures/auth/index.html')
// Visit a cross origin page
cy.visit('http://www.idp.com:3500/fixtures/auth/index.html')
// execute a command on the primary origin while the AUT is cross origin
cy.log('This command runs in the primary origin while the AUT is cross origin.')
// attach to the cross origin aut.
cy.origin('http://www.idp.com:3500', () => {
cy.get('[data-cy="login-idp"]')
})
})
it('errors if you try to use onload when visiting a cross origin page', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include(`\`cy.visit()\` was called to visit a cross origin site with an \`onLoad\` callback. \`onLoad\` callbacks can only be used with same origin sites.
If you wish to specify an \`onLoad\` callback please use the \`cy.origin\` command to setup a \`window:load\` event prior to visiting the cross origin site.`)
expect(err.message).to.include(`\`cy.origin('http://www.idp.com:3500', () => {\``)
expect(err.message).to.include(`\` cy.on('window:load', () => {\``)
expect(err.message).to.include(` \` <onLoad callback goes here>\``)
expect(err.message).to.include(` \`cy.visit('http://www.idp.com:3500/fixtures/auth/index.html')\``)
done()
})
cy.visit('/fixtures/auth/index.html')
cy.visit('http://www.idp.com:3500/fixtures/auth/index.html', {
onLoad: () => {
cy.log('onLoad')
},
})
})
it('errors if you try to use onBeforeLoad when visiting a cross origin page', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include(`\`cy.visit()\` was called to visit a cross origin site with an \`onBeforeLoad\` callback. \`onBeforeLoad\` callbacks can only be used with same origin sites.
If you wish to specify an \`onBeforeLoad\` callback please use the \`cy.origin\` command to setup a \`window:before:load\` event prior to visiting the cross origin site.`)
expect(err.message).to.include(`\`cy.origin('http://www.idp.com:3500', () => {\``)
expect(err.message).to.include(`\` cy.on('window:before:load', () => {\``)
expect(err.message).to.include(` \` <onBeforeLoad callback goes here>\``)
expect(err.message).to.include(` \`cy.visit('http://www.idp.com:3500/fixtures/auth/index.html')\``)
done()
})
cy.visit('/fixtures/auth/index.html')
cy.visit('http://www.idp.com:3500/fixtures/auth/index.html', {
onBeforeLoad: () => {
cy.log('onBeforeLoad')
},
})
})
})
context('withBeforeEach', () => {
beforeEach(() => {
cy.visit('/fixtures/primary-origin.html')
cy.get('a[data-cy="cross-origin-secondary-link"]').click()
})
it('runs commands in secondary origin', () => {
cy.origin('http://www.foobar.com:3500', () => {
cy
.get('[data-cy="dom-check"]')
.invoke('text')
.should('equal', 'From a secondary origin')
})
cy.log('after cy.origin')
})
it('passes runnable state to the secondary origin', () => {
const runnable = cy.state('runnable')
const expectedRunnable = {
clearTimeout: null,
isPending: null,
resetTimeout: null,
timeout: null,
id: runnable.id,
_currentRetry: runnable._currentRetry,
_timeout: 4000,
type: 'test',
title: 'passes runnable state to the secondary origin',
titlePath: [
'cy.origin',
'withBeforeEach',
'passes runnable state to the secondary origin',
],
parent: {
id: runnable.parent.id,
type: 'suite',
title: 'withBeforeEach',
titlePath: [
'withBeforeEach',
],
parent: {
id: runnable.parent.parent.id,
type: 'suite',
title: '',
titlePath: undefined,
ctx: {},
},
ctx: {},
},
ctx: {},
}
cy.origin('http://www.foobar.com:3500', { args: expectedRunnable }, (expectedRunnable) => {
const actualRunnable = cy.state('runnable')
expect(actualRunnable.titlePath()).to.deep.equal(expectedRunnable.titlePath)
expectedRunnable.titlePath = actualRunnable.titlePath
expect(actualRunnable.title).to.equal(expectedRunnable.title)
expect(actualRunnable.id).to.equal(expectedRunnable.id)
expect(actualRunnable.ctx).to.deep.equal(expectedRunnable.ctx)
expect(actualRunnable._timeout).to.equal(expectedRunnable._timeout)
expect(actualRunnable.type).to.equal(expectedRunnable.type)
expect(actualRunnable.callback).to.exist
expect(actualRunnable.timeout).to.exist
expect(actualRunnable.parent.title).to.equal(expectedRunnable.parent.title)
expect(actualRunnable.parent.type).to.equal(expectedRunnable.parent.type)
})
})
it('handles querying nested elements', () => {
cy.origin('http://www.foobar.com:3500', () => {
cy
.get('form button')
.invoke('text')
.should('equal', 'Submit')
})
cy.log('after cy.origin')
})
it('sets up window.Cypress in secondary origin', () => {
cy.origin('http://www.foobar.com:3500', () => {
cy
.get('[data-cy="cypress-check"]')
.invoke('text')
.should('equal', 'Has window.Cypress')
})
})
describe('data argument', () => {
it('passes object to callback function', () => {
cy.origin('http://www.foobar.com:3500', { args: { foo: 'foo', bar: 'bar' } }, ({ foo, bar }) => {
expect(foo).to.equal('foo')
expect(bar).to.equal('bar')
})
})
it('passes array to callback function', () => {
cy.origin('http://www.foobar.com:3500', { args: ['foo', 'bar'] }, ([foo, bar]) => {
expect(foo).to.equal('foo')
expect(bar).to.equal('bar')
})
})
it('passes string to callback function', () => {
cy.origin('http://www.foobar.com:3500', { args: 'foo' }, (foo) => {
expect(foo).to.equal('foo')
})
})
it('passes number to callback function', () => {
cy.origin('http://www.foobar.com:3500', { args: 1 }, (num) => {
expect(num).to.equal(1)
})
})
it('passes boolean to callback function', () => {
cy.origin('http://www.foobar.com:3500', { args: true }, (bool) => {
expect(bool).to.be.true
})
})
it('passes mixed types to callback function', () => {
cy.origin('http://www.foobar.com:3500', { args: { foo: 'foo', num: 1, bool: true } }, ({ foo, num, bool }) => {
expect(foo).to.equal('foo')
expect(num).to.equal(1)
expect(bool).to.be.true
})
})
})
describe('errors', () => {
it('propagates secondary origin errors to the primary that occur within the test', (done) => {
cy.on('fail', (err) => {
const undefinedMessage = Cypress.isBrowser('webkit') ? 'Can\'t find variable: variable' : 'variable is not defined'
expect(err.message).to.include(undefinedMessage)
expect(err.message).to.include(`Variables must either be defined within the \`cy.origin()\` command or passed in using the args option.`)
expect(err.stack).to.include(`Variables must either be defined within the \`cy.origin()\` command or passed in using the args option.`)
// 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.codeFrame).to.exist
expect(err.codeFrame!.frame).to.include('cy.origin')
done()
})
const variable = 'string'
cy.origin('http://www.foobar.com:3500', () => {
cy.log(variable)
})
})
it('propagates thrown errors in the secondary origin back to the primary w/ done', (done) => {
cy.on('fail', (e) => {
expect(e.message).to.equal('oops')
done()
})
cy.origin('http://www.foobar.com:3500', () => {
throw 'oops'
})
})
it('propagates thrown errors in the secondary origin back to the primary w/o done', () => {
return new Promise((resolve) => {
cy.on('fail', (e) => {
expect(e.message).to.equal('oops')
resolve(undefined)
})
cy.origin('http://www.foobar.com:3500', () => {
throw 'oops'
})
})
})
it('receives command failures from the secondary origin', (done) => {
const timeout = 50
cy.on('fail', (err) => {
expect(err.message).to.include(`Timed out retrying after ${timeout}ms: Expected to find element: \`#doesnt-exist\`, 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`)
done()
})
cy.origin('http://www.foobar.com:3500', { args: timeout }, (timeout) => {
cy.get('#doesnt-exist', {
timeout,
})
})
})
it('receives command failures from the secondary origin with the default timeout', { defaultCommandTimeout: 50 }, (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include(`Timed out retrying after 50ms: Expected to find element: \`#doesnt-exist\`, 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`)
done()
})
cy.origin('http://www.foobar.com:3500', () => {
cy.get('#doesnt-exist')
})
})
it('has non serializable arguments', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include(`This is likely because the arguments specified are not serializable. Note that functions and DOM objects cannot be serialized.`)
expect(err.stack).to.include(`This is likely because the arguments specified are not serializable. Note that functions and DOM objects cannot be serialized.`)
// 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.codeFrame).to.exist
expect(err.codeFrame!.frame).to.include('cy.origin')
done()
})
const variable = () => {}
cy.origin('http://www.foobar.com:3500', { args: variable }, (variable) => {
variable()
})
})
})
})
})