-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
cy.cy.js
483 lines (384 loc) · 12.8 KB
/
cy.cy.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
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
const { $ } = Cypress
describe('driver/src/cypress/cy', () => {
beforeEach(() => {
cy.visit('/fixtures/dom.html')
})
// https://github.com/cypress-io/cypress/issues/7731
// NOTE: this must remain the first test in the file
// or it will not properly check for the described issue
context('closing commands', () => {
beforeEach(function () {
this.logs = []
cy.on('log:added', (attrs, log) => {
this.logs.push(log)
})
return null
})
it('properly closes commands', function () {
expect(true).to.be.true
expect(this.logs.length).to.be.equal(1)
expect(this.logs[0].toJSON()).to.have.property('type', 'parent')
})
})
context('internals of custom commands', () => {
let setup
beforeEach(() => {
setup = (fn = () => {}) => {
Cypress.Commands.add('nested', () => {
cy.url()
})
cy.nested().noop().then(() => fn())
}
})
it('ensures to splice queue correctly on first custom command', () => {
Cypress.Commands.add('login', () => {
cy.get('input:first').type('foo')
})
const existing = cy.queue.names()
cy.login().noop().then(() => {
expect(cy.queue.names()).to.deep.eq(
existing.concat(['login', 'get', 'type', 'noop', 'then']),
)
})
})
it('queues in the correct order', function () {
const existing = cy.queue.names()
setup(() => {
expect(cy.queue.names()).to.deep.eq(
existing.concat(['nested', 'url', 'noop', 'then']),
)
})
})
it('nested command should reference url as next property', function () {
setup(() => {
const nested = cy.queue.find({ name: 'nested' })
expect(nested.get('next').get('name')).to.eq('url')
})
})
it('null outs nestedIndex prior to restoring', function (done) {
cy.on('command:queue:end', () => {
expect(cy.state('nestedIndex')).to.be.null
done()
})
setup()
})
it('can recursively nest', () => {
Cypress.Commands.add('nest1', () => {
cy.nest2()
})
Cypress.Commands.add('nest2', () => {
cy.noop()
})
const existing = cy.queue.names()
cy.nest1().then(() => {
expect(cy.queue.names()).to.deep.eq(
existing.concat(['nest1', 'nest2', 'noop', 'then']),
)
})
})
it('works with multiple nested commands', () => {
Cypress.Commands.add('multiple', () => {
cy.url().location().noop()
})
const existing = cy.queue.names()
cy.multiple().then(() => {
expect(cy.queue.names()).to.deep.eq(
existing.concat(['multiple', 'url', 'location', 'noop', 'then']),
)
})
})
it('stores invocation stack for first command', () => {
cy
.get('input:first')
.then(() => {
const userInvocationStack = cy.queue.find({ name: 'get' }).get('userInvocationStack')
expect(userInvocationStack).to.include('.cy.js')
})
})
it('stores invocation stack for chained command', () => {
cy
.get('div')
.find('input')
.then(() => {
const userInvocationStack = cy.queue.find({ name: 'find' }).get('userInvocationStack')
expect(userInvocationStack).to.include('.cy.js')
})
})
})
context('custom commands', () => {
beforeEach(() => {
Cypress.Commands.add('dashboard.selectRenderer', () => {
cy.get('[contenteditable]').first()
})
Cypress.Commands.add('login', { prevSubject: true }, (subject, email) => {
cy.wrap(subject.find('input:first')).type(email)
})
})
it('works with custom commands', () => {
const input = cy.$$('input:first')
cy.get('input:first').parent()
.command('login', 'brian@foo.com').then(($input) => {
expect($input.get(0)).to.eq(input.get(0))
})
})
it('works with namespaced commands', () => {
const ce = cy.$$('[contenteditable]').first()
cy.command('dashboard.selectRenderer').then(($ce) => {
expect($ce.get(0)).to.eq(ce.get(0))
})
})
describe('invocation stack', () => {
beforeEach(() => {
Cypress.Commands.add('getInput', () => cy.get('input'))
Cypress.Commands.add('findInput', { prevSubject: 'element' }, (subject) => {
subject.find('input')
})
})
it('stores invocation stack for first command', () => {
cy
.getInput()
.then(() => {
const userInvocationStack = cy.queue.find({ name: 'getInput' }).get('userInvocationStack')
expect(userInvocationStack).to.include('.cy.js')
})
})
it('stores invocation stack for chained command', () => {
cy
.get('div')
.findInput()
.then(() => {
const userInvocationStack = cy.queue.find({ name: 'findInput' }).get('userInvocationStack')
expect(userInvocationStack).to.include('.cy.js')
})
})
})
describe('parent commands', () => {
it('ignores existing subject', () => {
Cypress.Commands.add('bar', (arg1, arg2) => {
return [arg1, arg2]
})
cy.wrap('foo').bar(1, 2).then((arr) => {
expect(arr).to.deep.eq([1, 2])
})
})
})
describe('child commands', () => {
beforeEach(() => {
Cypress.Commands.add('c', { prevSubject: true }, (subject, arg) => {
cy.wrap([subject, arg])
})
Cypress.Commands.add('c2', { prevSubject: true }, (subject, arg) => {
return [subject, arg]
})
Cypress.Commands.add('winOnly', { prevSubject: 'window' }, () => {})
Cypress.Commands.add('docOnly', { prevSubject: 'document' }, () => {})
Cypress.Commands.add('elOnly', { prevSubject: 'element' }, () => {})
Cypress.Commands.add('elWinOnly', { prevSubject: ['element', 'window'] }, () => {})
})
it('is called with the correct ctx', function () {
const ctx = this
let expected = false
Cypress.Commands.add('childCtx', { prevSubject: true }, function () {
expect(this).to.equal(ctx)
expected = true
})
cy.wrap(null).childCtx().then(() => {
expect(expected).to.be.true
})
})
it('inherits subjects', () => {
cy
.wrap('foo')
.c('bar')
.then((arr) => {
expect(arr).to.deep.eq(['foo', 'bar'])
return null
})
.c('baz')
.then((arr) => {
expect(arr).to.deep.eq([null, 'baz'])
})
.wrap('foo2')
.c2('bar2')
.then((arr) => {
expect(arr).to.deep.eq(['foo2', 'bar2'])
return null
})
.c('baz2')
.then((arr) => {
expect(arr).to.deep.eq([null, 'baz2'])
})
})
it('fails when calling child command before parent', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('Oops, it looks like you are trying to call a child command before running a parent command')
expect(err.message).to.include('cy.c()')
done()
})
cy.wrap('foo')
cy.c()
})
it('fails when calling child command before parent with arguments', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('Oops, it looks like you are trying to call a child command before running a parent command')
expect(err.message).to.include('cy.c("bar")')
done()
})
cy.wrap('foo')
cy.c('bar')
})
it('fails when previous subject becomes detached', (done) => {
cy.$$('#button').click(function () {
return $(this).remove()
})
cy.on('fail', (err) => {
expect(err.message).to.include('`cy.parent()` failed because this element is detached from the DOM.')
expect(err.message).to.include('<button id="button">button</button>')
expect(err.message).to.include('> `cy.click()`')
expect(err.docsUrl).to.eq('https://on.cypress.io/element-has-detached-from-dom')
done()
})
cy.get('#button').click().parent()
})
it('fails when previous subject isnt window', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('`cy.winOnly()` failed because it requires the subject be a global `window` object.')
expect(err.message).to.include('{foo: bar}')
expect(err.message).to.include('> `cy.wrap()`')
done()
})
cy.wrap({ foo: 'bar' }).winOnly()
})
it('fails when previous subject isnt document', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('`cy.docOnly()` failed because it requires the subject be a global `document` object.')
expect(err.message).to.include('[1, 2, 3]')
expect(err.message).to.include('> `cy.wrap()`')
done()
})
cy.wrap([1, 2, 3]).docOnly()
})
it('fails when previous subject isnt an element or window', (done) => {
let firstPassed = false
cy.on('fail', (err) => {
expect(firstPassed).to.be.true
expect(err.message).to.include('`cy.elWinOnly()` failed because it requires a DOM element.')
expect(err.message).to.include('string')
expect(err.message).to.include('> `cy.wrap()`')
expect(err.message).to.include('All 2 subject validations failed')
done()
})
cy.window().elWinOnly()
.then(() => {
firstPassed = true
cy.wrap('string').elWinOnly()
})
})
})
describe('dual commands', () => {
beforeEach(() => {
Cypress.Commands.add('d', { prevSubject: 'optional' }, (subject, arg) => {
cy.wrap([subject, arg])
})
})
it('passes on subject when used as a child', () => {
cy
.wrap('foo')
.d('bar')
.then((arr) => {
expect(arr).to.deep.eq(['foo', 'bar'])
})
})
it('has an undefined subject when used as a parent', () => {
cy
.d('bar')
.then((arr) => {
expect(arr).to.deep.eq([undefined, 'bar'])
})
})
it('has an undefined subject as a parent with a previous parent', () => {
cy.wrap('foo')
cy
.d('bar')
.then((arr) => {
expect(arr).to.deep.eq([undefined, 'bar'])
})
.wrap('foo')
.d('bar')
.then((arr) => {
expect(arr).to.deep.eq(['foo', 'bar'])
return null
})
.d('baz')
.then((arr) => {
expect(arr).to.deep.eq([null, 'baz'])
})
})
})
})
context('overwrite custom commands', () => {
beforeEach(() => {
Cypress.Commands.overwrite('wrap', (orig, arg1) => {
return orig(`foo${arg1}`)
})
Cypress.Commands.overwrite('first', (orig, subject) => {
subject = $([1, 2])
return orig(subject)
})
Cypress.Commands.overwrite('noop', function (orig, fn) {
// yield the context
return fn(this)
})
Cypress.Commands.overwrite('submit', (orig, subject) => {
return orig(subject, { foo: 'foo' })
})
})
it('can modify parent commands', () => {
cy.wrap('bar').then((str) => {
expect(str).to.eq('foobar')
})
})
it('can modify child commands', () => {
cy.get('li').first().then((el) => {
expect(el[0]).to.eq(1)
})
})
it('has the current runnable ctx', function () {
const _this = this
cy.noop((ctx) => {
expect(_this === ctx).to.be.true
})
})
it('overwrites only once', () => {
Cypress.Commands.overwrite('wrap', (orig, arg1) => {
return orig(`${arg1}baz`)
})
cy.wrap('bar').should('eq', 'barbaz')
})
it('errors when command does not exist', () => {
const fn = () => {
Cypress.Commands.overwrite('foo', () => {})
}
expect(fn).to.throw().with.property('message')
.and.include('Cannot overwite command for: `foo`. An existing command does not exist by that name.')
expect(fn).to.throw().with.property('docsUrl')
.and.include('https://on.cypress.io/api')
})
it('updates state(\'current\') with modified args', () => {
cy.get('form').eq(0).submit().then(() => {
expect(cy.state('current').get('prev').get('args')[0].foo).to.equal('foo')
})
})
// https://github.com/cypress-io/cypress/issues/18892
it('passes this through to overwritten command', () => {
Cypress.Commands.add('bar', function () {
expect(this.test.title).to.exist
})
cy.bar()
Cypress.Commands.overwrite('bar', function (originalFn) {
return originalFn.call(this)
})
cy.bar()
})
})
})