forked from cypress-io/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
querying_spec.js
1831 lines (1416 loc) · 52.5 KB
/
querying_spec.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
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const { assertLogLength } = require('../../../support/utils')
const { _, $, Promise } = Cypress
describe('src/cy/commands/querying', () => {
beforeEach(() => {
cy.visit('/fixtures/dom.html')
})
context('#get', {
defaultCommandTimeout: 200,
}, () => {
it('finds by selector', () => {
const list = cy.$$('#list')
cy.get('#list').then(($list) => {
expect($list.get(0)).to.eq(list.get(0))
})
})
// NOTE: FLAKY in CI, need to investigate further
it.skip('retries finding elements until something is found', () => {
const missingEl = $('<div />', { id: 'missing-el' })
// wait until we're ALMOST about to time out before
// appending the missingEl
cy.on('command:retry', (options) => {
if ((options.total + (options._interval * 4)) > options._runnableTimeout) {
cy.$$('body').append(missingEl)
}
})
cy.get('#missing-el').then(($div) => {
expect($div).to.match(missingEl)
})
})
it('can increase the timeout', () => {
const missingEl = $('<div />', { id: 'missing-el' })
cy.on('command:retry', _.after(2, (options) => {
// make sure runnableTimeout is 10secs
expect(options._runnableTimeout).to.eq(10000)
// we shouldnt have a timer either
cy.$$('body').append(missingEl)
}))
cy.get('#missing-el', { timeout: 10000 })
})
it('does not factor in the total time the test has been running', () => {
const missingEl = $('<div />', { id: 'missing-el' })
cy.on('command:retry', _.after(2, () => {
cy.$$('body').append(missingEl)
}))
const defaultCommandTimeout = Cypress.config('defaultCommandTimeout')
// in this example our test has been running 300ms
// but the default command timeout is below this amount,
// and the test still passes because the timeout is only
// into each command and not the total overall running time
cy
.wait(defaultCommandTimeout + 100)
.get('#missing-el', { timeout: defaultCommandTimeout + 50 })
// it should reset the timeout back
// to 200 after successfully finishing 'get' method
.then(() => {
expect(cy.timeout()).to.eq(defaultCommandTimeout)
})
})
it('cancels existing promises', (done) => {
cy.stub(Cypress.runner, 'stop')
let retrys = 0
const stop = _.after(2, () => {
Cypress.stop()
})
cy.on('stop', () => {
_.delay(() => {
expect(retrys).to.eq(2)
done()
}
, 100)
})
cy.on('command:retry', () => {
retrys += 1
stop()
})
cy.get('doesNotExist')
})
describe('custom elements', () => {
// <foobarbazquux>custom element</foobarbazquux>
it('can get a custom element', () => {
cy.get('foobarbazquux').should('contain', 'custom element')
})
it('can alias a custom element', () => {
cy
.get('foobarbazquux:last').as('foo')
.get('div:first')
.get('@foo').should('contain', 'custom element')
})
it('can find a custom alias again when detached from DOM', () => {
cy
.get('foobarbazquux:last').as('foo')
.then(() => {
// remove the existing foobarbazquux
cy.$$('foobarbazquux').remove()
// and cause it to be re-rendered
cy.$$('body').append(cy.$$('<foobarbazquux>asdf</foobarbazquux>'))
}).get('@foo').should('contain', 'asdf')
})
})
describe('should(\'exist\')', {
defaultCommandTimeout: 1000,
}, () => {
it('waits until button exists', () => {
cy.on('command:retry', _.after(3, () => {
cy.$$('body').append($('<div id=\'missing-el\'>missing el</div>'))
}))
cy.get('#missing-el').should('exist')
})
})
describe('should(\'not.exist\')', () => {
it('waits until button does not exist', () => {
cy.timeout(500, true)
cy.on('command:retry', _.after(2, () => {
cy.$$('#button').remove()
}))
cy.get('#button').should('not.exist')
})
it('returns null when cannot find element', () => {
cy.get('#missing-el').should('not.exist').then(($el) => {
expect($el).to.be.null
})
})
it('retries until cannot find element', () => {
// add 500ms to the delta
cy.timeout(500, true)
const retry = _.after(3, () => {
cy.$$('#list li:last').remove()
})
cy.on('command:retry', retry)
cy.get('#list li:last').should('not.exist').then(($el) => {
expect($el).to.be.null
})
})
})
describe('visibility is unopinionated', () => {
it('finds invisible elements by default', () => {
const button = cy.$$('#button').hide()
cy.get('#button').then(($button) => {
expect($button.get(0)).to.eq(button.get(0))
})
})
})
describe('should(\'not.be.visible\')', () => {
it('returns invisible element', () => {
const button = cy.$$('#button').hide()
// cy.get("#button").then ($button) ->
// expect($button).not.to.be.visible
cy.get('#button').should('not.be.visible').then(($button) => {
expect($button.get(0)).to.eq(button.get(0))
})
})
it('retries until element is invisible', () => {
// add 500ms to the delta
cy.timeout(500, true)
let button = null
const retry = _.after(3, () => {
button = cy.$$('#button').hide()
})
cy.on('command:retry', retry)
cy.get('#button').should('not.be.visible').then(($button) => {
expect($button.get(0)).to.eq(button.get(0))
})
})
})
describe('should(\'be.visible\')', () => {
it('returns visible element', () => {
const button = cy.$$('#button')
cy.get('#button').should('be.visible').then(($button) => {
expect($button.get(0)).to.eq(button.get(0))
})
})
it('retries until element is visible', () => {
// add 500ms to the delta
cy.timeout(500, true)
const button = cy.$$('#button').hide()
const retry = _.after(3, () => {
button.show()
})
cy.on('command:retry', retry)
cy.get('#button').should('be.visible').then(($button) => {
expect($button.get(0)).to.eq(button.get(0))
})
})
})
describe('should(\'have.length\', n)', () => {
it('resolves once length equals n', () => {
const forms = cy.$$('form')
cy.get('form').should('have.length', forms.length).then(($forms) => {
expect($forms.length).to.eq(forms.length)
})
})
it('retries until length equals n', () => {
// add 500ms to the delta
cy.timeout(500, true)
let buttons = cy.$$('button')
const length = buttons.length - 2
cy.on('command:retry', _.after(2, () => {
buttons.last().remove()
buttons = cy.$$('button')
}))
// should resolving after removing 2 buttons
cy.get('button').should('have.length', length).then(($buttons) => {
expect($buttons.length).to.eq(length)
})
})
it('retries an alias when not enough elements found', () => {
// add 500ms to the delta
cy.timeout(500, true)
const buttons = cy.$$('button')
const length = buttons.length + 1
// add another button after 2 retries, once
cy.on('command:retry', _.after(2, _.once(() => {
$('<button />').appendTo(cy.$$('body'))
})))
// should eventually resolve after adding 1 button
cy
.get('button').as('btns')
.get('@btns').should('have.length', length).then(($buttons) => {
expect($buttons.length).to.eq(length)
})
})
it('retries an alias when too many elements found without replaying commands', () => {
// add 500ms to the delta
cy.timeout(500, true)
let buttons = cy.$$('button')
const length = buttons.length - 2
const replayCommandsFrom = cy.spy(cy, 'replayCommandsFrom')
cy.on('command:retry', () => {
buttons.last().remove()
buttons = cy.$$('button')
})
const existingLen = cy.queue.length
// should eventually resolve after adding 1 button
cy
.get('button').as('btns')
.get('@btns').should('have.length', length).then(($buttons) => {
expect(replayCommandsFrom).not.to.be.called
// get, as, get, should, then == 5
expect(cy.queue.length - existingLen).to.eq(5) // we should not have replayed any commands
expect($buttons.length).to.eq(length)
})
})
})
describe('assertion verification', () => {
it('automatically retries', () => {
cy.on('command:retry', _.after(2, () => {
cy.$$('button:first').attr('data-foo', 'bar')
}))
cy.get('button:first').should('have.attr', 'data-foo').and('match', /bar/)
})
it('eventually resolves an alias', () => {
cy.on('command:retry', _.after(2, () => {
cy.$$('button:first').addClass('foo-bar-baz')
}))
cy
.get('button:first').as('btn')
.get('@btn').should('have.class', 'foo-bar-baz')
})
})
describe('.log', () => {
beforeEach(function () {
this.logs = []
cy.on('log:added', (attrs, log) => {
if (attrs.name === 'get') {
this.lastLog = log
this.logs.push(log)
}
})
return null
})
it('logs elements length', () => {
let buttons = cy.$$('button')
const length = buttons.length - 2
// add 500ms to the delta
cy.timeout(500, true)
cy.on('command:retry', () => {
buttons.last().remove()
buttons = cy.$$('button')
})
// should resolving after removing 2 buttons
cy.get('button').should('have.length', length).then(function ($buttons) {
expect(this.lastLog.get('numElements')).to.eq(length)
})
})
it('logs exist: false', () => {
cy.get('#does-not-exist').should('not.exist').then(function () {
expect(this.lastLog.get('message')).to.eq('#does-not-exist')
expect(this.lastLog.get('$el').get(0)).not.to.be.ok
})
})
it('logs route aliases', () => {
cy.visit('http://localhost:3500/fixtures/jquery.html')
cy.server()
cy.route(/users/, {}).as('get.users')
cy.window().then({ timeout: 2000 }, (win) => {
win.$.get('/users')
})
cy.get('@get.users').then(function () {
expect(this.lastLog.pick('message', 'referencesAlias', 'aliasType')).to.deep.eq({
message: '@get.users',
referencesAlias: { name: 'get.users' },
aliasType: 'route',
})
})
})
it('logs primitive aliases', (done) => {
cy.on('log:added', (attrs, log) => {
if (attrs.name === 'get') {
expect(log.pick('$el', 'numRetries', 'referencesAlias', 'aliasType')).to.deep.eq({
referencesAlias: { name: 'f' },
aliasType: 'primitive',
})
done()
}
})
cy
.noop('foo').as('f')
.get('@f')
})
it('logs immediately before resolving', (done) => {
cy.on('log:added', (attrs, log) => {
if (attrs.name === 'get') {
expect(log.pick('state', 'referencesAlias', 'aliasType')).to.deep.eq({
state: 'pending',
referencesAlias: undefined,
aliasType: 'dom',
})
done()
}
})
cy.get('body')
})
it('snapshots and ends when consuming an alias', () => {
cy
.get('body').as('b')
.get('@b').then(function () {
expect(this.lastLog.get('ended')).to.be.true
expect(this.lastLog.get('state')).to.eq('passed')
expect(this.lastLog.get('snapshots').length).to.eq(1)
expect(this.lastLog.get('snapshots')[0]).to.be.an('object')
})
})
it('logs obj once complete', () => {
cy.get('body').as('b').then(function ($body) {
const obj = {
state: 'passed',
name: 'get',
message: 'body',
alias: 'b',
aliasType: 'dom',
referencesAlias: undefined,
}
expect(this.lastLog.get('$el').get(0)).to.eq($body.get(0))
_.each(obj, (value, key) => {
expect(this.lastLog.get(key)).deep.eq(value, `expected key: ${key} to eq value: ${value}`)
})
})
})
it('#consoleProps', () => {
cy.get('body').then(function ($body) {
expect(this.lastLog.invoke('consoleProps')).to.deep.eq({
Command: 'get',
Selector: 'body',
Yielded: $body.get(0),
Elements: 1,
})
})
})
it('#consoleProps with an alias', () => {
cy.get('body').as('b').get('@b').then(function ($body) {
expect(this.lastLog.invoke('consoleProps')).to.deep.eq({
Command: 'get',
Alias: '@b',
Yielded: $body.get(0),
Elements: 1,
})
})
})
it('#consoleProps with a primitive alias', () => {
cy.noop({ foo: 'foo' }).as('obj').get('@obj').then(function (obj) {
expect(this.lastLog.invoke('consoleProps')).to.deep.eq({
Command: 'get',
Alias: '@obj',
Yielded: obj,
})
})
})
it('#consoleProps with a route alias', () => {
cy
.server()
.route(/users/, {}).as('getUsers')
.visit('http://localhost:3500/fixtures/jquery.html')
.window().then({ timeout: 2000 }, (win) => {
return win.$.get('/users')
}).get('@getUsers').then(function (obj) {
expect(this.lastLog.invoke('consoleProps')).to.deep.eq({
Command: 'get',
Alias: '@getUsers',
Yielded: obj,
})
})
})
})
describe('alias references', {
defaultCommandTimeout: 100,
}, () => {
it('can get alias primitives', () => {
cy
.noop('foo').as('f')
.get('@f').then((foo) => {
expect(foo).to.eq('foo')
})
})
it('can get alias objects', () => {
cy
.noop({}).as('obj')
.get('@obj').then((obj) => {
expect(obj).to.deep.eq({})
})
})
it('re-queries for an existing alias', () => {
const body = cy.$$('body')
cy.get('body').as('b').get('@b').then(($body) => {
expect($body.get(0)).to.eq(body.get(0))
})
})
it('re-queries the dom if any element in an alias isnt in the document', () => {
cy.$$('input')
cy
.get('input').as('inputs').then(function ($inputs) {
this.length = $inputs.length
// remove the last input
$inputs.last().remove()
// return original subject
return $inputs
}).get('@inputs').then(function ($inputs) {
// we should have re-queried for these inputs
// which should have reduced their length by 1
expect($inputs).to.have.length(this.length - 1)
})
})
describe('route aliases', () => {
it('returns the xhr', () => {
cy
.server()
.route(/users/, {}).as('getUsers')
.visit('http://localhost:3500/fixtures/jquery.html')
.window().then({ timeout: 2000 }, (win) => {
return win.$.get('/users')
}).get('@getUsers').then((xhr) => {
expect(xhr.url).to.include('/users')
})
})
it('handles dots in alias name', () => {
cy.server()
cy.route(/users/, {}).as('get.users')
cy.visit('http://localhost:3500/fixtures/jquery.html')
cy.window().then({ timeout: 2000 }, (win) => {
return win.$.get('/users')
})
cy.get('@get.users').then((xhr) => {
expect(xhr.url).to.include('/users')
})
})
it('returns null if no xhr is found', () => {
cy
.server()
.route(/users/, {}).as('getUsers')
.visit('http://localhost:3500/fixtures/jquery.html')
.get('@getUsers').then((xhr) => {
expect(xhr).to.be.null
})
})
it('returns an array of xhrs', () => {
cy
.visit('http://localhost:3500/fixtures/jquery.html')
.server()
.route(/users/, {}).as('getUsers')
.window().then({ timeout: 2000 }, (win) => {
return Promise.all([
win.$.get('/users', { num: 1 }),
win.$.get('/users', { num: 2 }),
])
}).get('@getUsers.all').then((xhrs) => {
expect(xhrs).to.be.an('array')
expect(xhrs[0].url).to.include('/users?num=1')
expect(xhrs[1].url).to.include('/users?num=2')
})
})
it('returns an array of xhrs when dots in alias name', () => {
cy.visit('http://localhost:3500/fixtures/jquery.html')
cy.server()
cy.route(/users/, {}).as('get.users')
cy.window().then({ timeout: 2000 }, (win) => {
return Promise.all([
win.$.get('/users', { num: 1 }),
win.$.get('/users', { num: 2 }),
])
})
cy.get('@get.users.all').then((xhrs) => {
expect(xhrs).to.be.an('array')
expect(xhrs[0].url).to.include('/users?num=1')
expect(xhrs[1].url).to.include('/users?num=2')
})
})
it('returns the 1st xhr', () => {
cy
.visit('http://localhost:3500/fixtures/jquery.html')
.server()
.route(/users/, {}).as('getUsers')
.window().then({ timeout: 2000 }, (win) => {
return Promise.all([
win.$.get('/users', { num: 1 }),
win.$.get('/users', { num: 2 }),
])
}).get('@getUsers.1').then((xhr1) => {
expect(xhr1.url).to.include('/users?num=1')
})
})
it('returns the 2nd xhr', () => {
cy
.visit('http://localhost:3500/fixtures/jquery.html')
.server()
.route(/users/, {}).as('getUsers')
.window().then({ timeout: 2000 }, (win) => {
return Promise.all([
win.$.get('/users', { num: 1 }),
win.$.get('/users', { num: 2 }),
])
}).get('@getUsers.2').then((xhr2) => {
expect(xhr2.url).to.include('/users?num=2')
})
})
it('returns the 2nd xhr when dots in alias', () => {
cy.visit('http://localhost:3500/fixtures/jquery.html')
cy.server()
cy.route(/users/, {}).as('get.users')
cy.window().then({ timeout: 2000 }, (win) => {
return Promise.all([
win.$.get('/users', { num: 1 }),
win.$.get('/users', { num: 2 }),
])
})
cy.get('@get.users.2').then((xhr2) => {
expect(xhr2.url).to.include('/users?num=2')
})
})
it('returns the 3rd xhr as null', () => {
cy
.server()
.route(/users/, {}).as('getUsers')
.visit('http://localhost:3500/fixtures/jquery.html')
.window().then({ timeout: 2000 }, (win) => {
return Promise.all([
win.$.get('/users', { num: 1 }),
win.$.get('/users', { num: 2 }),
])
}).get('@getUsers.3').then((xhr3) => {
expect(xhr3).to.be.null
})
})
})
})
// it "re-queries the dom if any element in an alias isnt visible", ->
// inputs = cy.$$("input")
// inputs.hide()
// cy
// .get("input", {visible: false}).as("inputs").then ($inputs) ->
// @length = $inputs.length
// ## show the inputs
// $inputs.show()
// return $inputs
// .get("@inputs").then ($inputs) ->
// ## we should have re-queried for these inputs
// ## which should have increased their length by 1
// expect($inputs).to.have.length(@length)
// these other tests are for .save
// it "will resolve deferred arguments", ->
// df = $.Deferred()
// _.delay ->
// df.resolve("iphone")
// , 100
// cy.get("input:text:first").type(df).then ($input) ->
// expect($input).to.have.value("iphone")
// it "handles saving subjects", ->
// cy.noop({foo: "foo"}).assign("foo").noop(cy.get("foo")).then (subject) ->
// expect(subject).to.deep.eq {foo: "foo"}
// it "resolves falsy arguments", ->
// cy.noop(0).assign("zero").then ->
// expect(cy.get("zero")).to.eq 0
// it "returns a function when no alias was found", ->
// cy.noop().then ->
// expect(cy.get("something")).to.be.a("function")
describe('errors', {
defaultCommandTimeout: 50,
}, () => {
beforeEach(function () {
this.logs = []
cy.on('log:added', (attrs, log) => {
if (attrs.name === 'get') {
this.lastLog = log
this.logs.push(log)
}
})
return null
})
it('throws once when incorrect sizzle selector', function (done) {
cy.on('fail', (err) => {
assertLogLength(this.logs, 1)
done()
})
cy.get('.spinner\'')
})
it('throws on too many elements after timing out waiting for length', (done) => {
const buttons = cy.$$('button')
cy.on('fail', (err) => {
expect(err.message).to.include(`Too many elements found. Found '${buttons.length}', expected '${buttons.length - 1}'.`)
done()
})
cy.get('button').should('have.length', buttons.length - 1)
})
it('throws on too few elements after timing out waiting for length', (done) => {
const buttons = cy.$$('button')
cy.on('fail', (err) => {
expect(err.message).to.include(`Not enough elements found. Found '${buttons.length}', expected '${buttons.length + 1}'.`)
done()
})
cy.get('button').should('have.length', buttons.length + 1)
})
it('throws after timing out not finding element', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('Expected to find element: `#missing-el`, but never found it.')
done()
})
cy.get('#missing-el')
})
it('throws after timing out not finding element when should exist', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('Expected to find element: `#missing-el`, but never found it.')
done()
})
cy.get('#missing-el').should('exist')
})
it('throws existence error without running assertions', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('Expected to find element: `#missing-el`, but never found it.')
done()
})
cy.get('#missing-el').should('have.prop', 'foo')
})
it('throws when using an alias that does not exist')
it('throws after timing out after a .wait() alias reference', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('Expected to find element: `getJsonButton`, but never found it.')
done()
})
cy
.server()
.route(/json/, { foo: 'foo' }).as('getJSON')
.visit('http://localhost:3500/fixtures/xhr.html').then(() => {
cy.$$('#get-json').click(() => {
cy.timeout(1000)
const retry = _.after(3, _.once(() => {
cy.state('window').$.getJSON('/json')
}))
cy.on('command:retry', retry)
})
}).get('#get-json').as('getJsonButton').click()
.wait('@getJSON')
.get('getJsonButton')
})
it('throws after timing out while not trying to find an element', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('Expected <div#dom> not to exist in the DOM, but it was continuously found.')
done()
})
cy.get('div:first').should('not.exist')
})
it('throws after timing out while trying to find an invisible element', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('expected \'<div#dom>\' not to be \'visible\'')
done()
})
cy.get('div:first').should('not.be.visible')
})
it('does not include message about why element was not visible', (done) => {
cy.on('fail', (err) => {
expect(err.message).not.to.include('why this element is not visible')
done()
})
cy.get('div:first').should('not.be.visible')
})
it('throws after timing out trying to find a visible element', (done) => {
cy.$$('#button').hide()
cy.on('fail', (err) => {
expect(err.message).to.include('This element `<button#button>` is not visible because it has CSS property: `display: none`')
done()
})
cy.get('#button').should('be.visible')
})
it('includes a message about why the element was not visible', (done) => {
cy.$$('#button').hide()
cy.on('fail', (err) => {
expect(err.message).to.include('element `<button#button>` is not visible because')
done()
})
cy.get('#button').should('be.visible')
})
it('sets error command state', function (done) {
cy.on('fail', (err) => {
const { lastLog } = this
expect(lastLog.get('state')).to.eq('failed')
expect(lastLog.get('error')).to.eq(err)
done()
})
cy.get('foobar')
})
it('throws when alias property is `0`', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('`0` is not a valid alias property. Are you trying to ask for the first response? If so write `@getUsers.1`')
done()
})
cy
.server()
.route(/users/, {}).as('getUsers')
.get('@getUsers.0')
})
it('throws when alias property isnt just a digit', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('`1b` is not a valid alias property. Only `numbers` or `all` is permitted.')
done()
})
cy
.server()
.route(/users/, {}).as('getUsers')
.get('@getUsers.1b')
})
it('throws when alias property isnt a digit or `all`', (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include('`all ` is not a valid alias property. Only `numbers` or `all` is permitted.')
done()
})
cy
.server()
.route(/users/, {}).as('getUsers')
.get('@getUsers.all ')
})
_.each(['', 'foo', [], 1, null], (value) => {
it(`throws when options property is not an object. Such as: ${value}`, (done) => {
cy.on('fail', (err) => {
expect(err.message).to.include(`only accepts an options object for its second argument. You passed ${value}`)
done()
})
cy.get('foobar', value)
})
})
it('logs out $el when existing $el is found even on failure', function (done) {
const button = cy.$$('#button').hide()
cy.on('fail', (err) => {
const { lastLog } = this
expect(lastLog.get('state')).to.eq('failed')
expect(lastLog.get('error')).to.eq(err)
expect(lastLog.get('$el').get(0)).to.eq(button.get(0))
const consoleProps = lastLog.invoke('consoleProps')
expect(consoleProps.Yielded).to.eq(button.get(0))
expect(consoleProps.Elements).to.eq(button.length)
done()
})
cy.get('#button').should('be.visible')
})
})
})
context('#contains', () => {
it('is scoped to the body and will not return title elements', () => {
cy.contains('DOM Fixture').then(($el) => {
expect($el).not.to.match('title')
})
})
it('will not find script elements', () => {
cy.$$('<script>// some-script-content </script>').appendTo(cy.$$('body'))
cy.contains('some-script-content').should('not.exist')
})
it('will not find style elements', () => {
cy.$$('<style> some-style-content {} </style>').appendTo(cy.$$('body'))
cy.contains('some-style-content').should('not.exist')
})