-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
Copy pathorigin.spec.ts
262 lines (224 loc) · 9.79 KB
/
origin.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
describe('cy.origin', () => {
afterEach(() => {
// FIXME: Tests that end with a cy.origin command and enqueue no further cy
// commands may have origin's unload event bleed into subsequent tests
// and prevent stability from being reached, causing those tests to hang.
// We enqueue another cy command after each test to ensure stability
// is reached for the next test. This additional command can be removed with the
// completion of: https://github.com/cypress-io/cypress/issues/21300
cy.then(() => { /* ensuring stability */ })
})
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://foobar.com:3500', { args: expectedViewport }, (expectedViewport) => {
const secondaryViewport = [cy.state('viewportWidth'), cy.state('viewportHeight')]
expect(secondaryViewport).to.deep.equal(expectedViewport)
})
})
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://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://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://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://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://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://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://foobar.com:3500', { args: 'foo' }, (foo) => {
expect(foo).to.equal('foo')
})
})
it('passes number to callback function', () => {
cy.origin('http://foobar.com:3500', { args: 1 }, (num) => {
expect(num).to.equal(1)
})
})
it('passes boolean to callback function', () => {
cy.origin('http://foobar.com:3500', { args: true }, (bool) => {
expect(bool).to.be.true
})
})
it('passes mixed types to callback function', () => {
cy.origin('http://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) => {
expect(err.message).to.include('variable is not defined')
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://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://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://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://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://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://foobar.com:3500', { args: variable }, (variable) => {
variable()
})
})
})
})
})