-
-
Notifications
You must be signed in to change notification settings - Fork 737
/
Copy pathwebapi.js
1816 lines (1565 loc) · 57.9 KB
/
webapi.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
let expect
let assert
import('chai').then((chai) => {
expect = chai.expect
assert = chai.assert
})
const path = require('path')
const dataFile = path.join(__dirname, '/../data/app/db')
const formContents = require('../../lib/utils').test.submittedData(dataFile)
const fileExists = require('../../lib/utils').fileExists
const secret = require('../../lib/secret').secret
const Locator = require('../../lib/locator')
const customLocators = require('../../lib/plugin/customLocator')
let originalLocators
let I
let data
let siteUrl
module.exports.init = function (testData) {
data = testData
}
module.exports.tests = function () {
const isHelper = (helperName) => I.constructor.name === helperName
beforeEach(() => {
I = data.I
siteUrl = data.siteUrl
if (fileExists(dataFile)) require('fs').unlinkSync(dataFile)
})
describe('#saveElementScreenshot', () => {
beforeEach(() => {
global.output_dir = path.join(global.codecept_dir, 'output')
})
it('should create a screenshot file in output dir of element', async () => {
await I.amOnPage('/form/field')
await I.seeElement("input[name='name']")
const sec = new Date().getUTCMilliseconds()
await I.saveElementScreenshot("input[name='name']", `element_screenshot_${sec}.png`)
assert.ok(fileExists(path.join(global.output_dir, `element_screenshot_${sec}.png`)), null, 'file does not exists')
})
})
describe('current url : #seeInCurrentUrl, #seeCurrentUrlEquals, #grabCurrentUrl, ...', () => {
it('should check for url fragment', async () => {
await I.amOnPage('/form/checkbox')
await I.seeInCurrentUrl('/form')
await I.dontSeeInCurrentUrl('/user')
})
it('should check for equality', async () => {
await I.amOnPage('/info')
await I.seeCurrentUrlEquals('/info')
await I.dontSeeCurrentUrlEquals('form')
})
it('should check for equality in absolute urls', async () => {
await I.amOnPage('/info')
await I.seeCurrentUrlEquals(`${siteUrl}/info`)
await I.dontSeeCurrentUrlEquals(`${siteUrl}/form`)
})
it('should grab browser url', async () => {
await I.amOnPage('/info')
const url = await I.grabCurrentUrl()
assert.equal(url, `${siteUrl}/info`)
})
})
describe('#waitInUrl, #waitUrlEquals', () => {
it('should wait part of the URL to match the expected', async () => {
try {
await I.amOnPage('/info')
await I.waitInUrl('/info')
await I.waitInUrl('/info2', 0.1)
} catch (e) {
assert.include(e.message, `expected url to include /info2, but found ${siteUrl}/info`)
}
})
it('should wait for the entire URL to match the expected', async () => {
try {
await I.amOnPage('/info')
await I.waitUrlEquals('/info')
await I.waitUrlEquals(`${siteUrl}/info`)
await I.waitUrlEquals('/info2', 0.1)
} catch (e) {
assert.include(e.message, `expected url to be ${siteUrl}/info2, but found ${siteUrl}/info`)
}
})
})
describe('see text : #see', () => {
it('should check text on site', async () => {
await I.amOnPage('/')
await I.see('Welcome to test app!')
await I.see('A wise man said: "debug!"')
await I.dontSee('Info')
})
it('should check text inside element', async () => {
await I.amOnPage('/')
await I.see('Welcome to test app!', 'h1')
await I.amOnPage('/info')
await I.see('valuable', { css: 'p' })
await I.see('valuable', '//p')
await I.dontSee('valuable', 'h1')
})
it('should verify non-latin chars', async () => {
await I.amOnPage('/info')
await I.see('на')
await I.see("Don't do that at home!", 'h3')
await I.see('Текст', 'p')
})
it('should verify text with  ', async () => {
if (isHelper('TestCafe') || isHelper('WebDriver')) return
await I.amOnPage('/')
await I.see('With special space chars')
})
})
describe('see element : #seeElement, #seeElementInDOM, #dontSeeElement', () => {
it('should check visible elements on page', async () => {
await I.amOnPage('/form/field')
await I.seeElement('input[name=name]')
await I.seeElement({ name: 'name' })
await I.seeElement('//input[@id="name"]')
await I.dontSeeElement('#something-beyond')
await I.dontSeeElement('//input[@id="something-beyond"]')
await I.dontSeeElement({ name: 'noname' })
await I.dontSeeElement('#noid')
})
it('should check elements are in the DOM', async () => {
await I.amOnPage('/form/field')
await I.seeElementInDOM('input[name=name]')
await I.seeElementInDOM('//input[@id="name"]')
await I.seeElementInDOM({ name: 'noname' })
await I.seeElementInDOM('#noid')
await I.dontSeeElementInDOM('#something-beyond')
await I.dontSeeElementInDOM('//input[@id="something-beyond"]')
})
it('should check elements are visible on the page', async () => {
await I.amOnPage('/form/field')
await I.seeElementInDOM('input[name=email]')
await I.dontSeeElement('input[name=email]')
await I.dontSeeElement('#something-beyond')
})
})
describe('#seeNumberOfVisibleElements', () => {
it('should check number of visible elements for given locator', async () => {
await I.amOnPage('/info')
await I.seeNumberOfVisibleElements('//div[@id = "grab-multiple"]//a', 3)
})
})
describe('#grabNumberOfVisibleElements', () => {
it('should grab number of visible elements for given locator', async () => {
await I.amOnPage('/info')
const num = await I.grabNumberOfVisibleElements('//div[@id = "grab-multiple"]//a')
assert.equal(num, 3)
})
it('should support locators like {xpath:"//div"}', async () => {
await I.amOnPage('/info')
const num = await I.grabNumberOfVisibleElements({
xpath: '//div[@id = "grab-multiple"]//a',
})
assert.equal(num, 3)
})
it('should grab number of visible elements for given css locator', async () => {
await I.amOnPage('/info')
const num = await I.grabNumberOfVisibleElements('[id=grab-multiple] a')
assert.equal(num, 3)
})
it('should return 0 for non-existing elements', async () => {
await I.amOnPage('/info')
const num = await I.grabNumberOfVisibleElements('button[type=submit]')
assert.equal(num, 0)
})
it('should honor visibility hidden style', async () => {
await I.amOnPage('/info')
const num = await I.grabNumberOfVisibleElements('.issue2928')
assert.equal(num, 1)
})
})
describe('#seeInSource, #dontSeeInSource', () => {
it('should check meta of a page', async () => {
await I.amOnPage('/info')
await I.seeInSource('<body>')
await I.dontSeeInSource('<meta>')
await I.seeInSource('Invisible text')
await I.seeInSource('content="text/html; charset=utf-8"')
})
})
describe('#click', () => {
it('should click by inner text', async () => {
await I.amOnPage('/')
await I.click('More info')
await I.seeInCurrentUrl('/info')
})
it('should click by css', async () => {
await I.amOnPage('/')
await I.click('#link')
await I.seeInCurrentUrl('/info')
})
it('should click by xpath', async () => {
await I.amOnPage('/')
await I.click('//a[@id="link"]')
await I.seeInCurrentUrl('/info')
})
it('should click by name', async () => {
await I.amOnPage('/form/button')
await I.click('btn0')
assert.equal(formContents('text'), 'val')
})
it('should click on context', async () => {
await I.amOnPage('/')
await I.click('More info', 'body>p')
await I.seeInCurrentUrl('/info')
})
it('should not click wrong context', async () => {
let err = false
await I.amOnPage('/')
try {
await I.click('More info', '#area1')
} catch (e) {
err = true
}
assert.ok(err)
})
it('should should click by aria-label', async () => {
await I.amOnPage('/form/aria')
await I.click('get info')
await I.seeInCurrentUrl('/info')
})
it('should click link with inner span', async () => {
await I.amOnPage('/form/example7')
await I.click('Buy Chocolate Bar')
await I.seeCurrentUrlEquals('/')
})
it('should click link with xpath locator', async () => {
await I.amOnPage('/form/example7')
await I.click({
xpath: '(//*[@title = "Chocolate Bar"])[1]',
})
await I.seeCurrentUrlEquals('/')
})
})
describe('#forceClick', () => {
beforeEach(function () {
if (isHelper('TestCafe')) this.skip()
})
it('should forceClick by inner text', async () => {
await I.amOnPage('/')
await I.forceClick('More info')
if (isHelper('Puppeteer')) await I.waitForNavigation()
await I.seeInCurrentUrl('/info')
})
it('should forceClick by css', async () => {
await I.amOnPage('/')
await I.forceClick('#link')
if (isHelper('Puppeteer')) await I.waitForNavigation()
await I.seeInCurrentUrl('/info')
})
it('should forceClick by xpath', async () => {
await I.amOnPage('/')
await I.forceClick('//a[@id="link"]')
if (isHelper('Puppeteer')) await I.waitForNavigation()
await I.seeInCurrentUrl('/info')
})
it('should forceClick on context', async () => {
await I.amOnPage('/')
await I.forceClick('More info', 'body>p')
if (isHelper('Puppeteer')) await I.waitForNavigation()
await I.seeInCurrentUrl('/info')
})
})
// Could not get double click to work
describe('#doubleClick', () => {
it('it should doubleClick', async () => {
await I.amOnPage('/form/doubleclick')
await I.dontSee('Done')
await I.doubleClick('#block')
await I.see('Done')
})
})
// rightClick does not seem to work either
describe('#rightClick', () => {
it('it should rightClick', async () => {
await I.amOnPage('/form/rightclick')
await I.dontSee('right clicked')
await I.rightClick('Lorem Ipsum')
await I.see('right clicked')
})
it('it should rightClick by locator', async () => {
await I.amOnPage('/form/rightclick')
await I.dontSee('right clicked')
await I.rightClick('.context a')
await I.see('right clicked')
})
it('it should rightClick by locator and context', async () => {
await I.amOnPage('/form/rightclick')
await I.dontSee('right clicked')
await I.rightClick('Lorem Ipsum', '.context')
await I.see('right clicked')
})
})
describe('#checkOption', () => {
it('should check option by css', async () => {
await I.amOnPage('/form/checkbox')
await I.checkOption('#checkin')
await I.click('Submit')
await I.wait(1)
assert.equal(formContents('terms'), 'agree')
})
it('should check option by strict locator', async () => {
await I.amOnPage('/form/checkbox')
await I.checkOption({
id: 'checkin',
})
await I.click('Submit')
assert.equal(formContents('terms'), 'agree')
})
it('should check option by name', async () => {
await I.amOnPage('/form/checkbox')
await I.checkOption('terms')
await I.click('Submit')
assert.equal(formContents('terms'), 'agree')
})
it('should check option by label', async () => {
await I.amOnPage('/form/checkbox')
await I.checkOption('I Agree')
await I.click('Submit')
assert.equal(formContents('terms'), 'agree')
})
// TODO Having problems with functional style selectors in testcafe
// cannot do Selector(css).find(elementByXPath(xpath))
// testcafe always says "xpath is not defined"
// const el = Selector(context).find(elementByXPath(Locator.checkable.byText(xpathLocator.literal(field))).with({ boundTestRun: this.t })).with({ boundTestRun: this.t });
it.skip('should check option by context', async () => {
if (isHelper('TestCafe')) this.skip()
await I.amOnPage('/form/example1')
await I.checkOption('Remember me next time', '.rememberMe')
await I.click('Login')
assert.equal(formContents('LoginForm').rememberMe, 1)
})
})
describe('#uncheckOption', () => {
it('should uncheck option that is currently checked', async () => {
await I.amOnPage('/info')
await I.uncheckOption('interesting')
await I.dontSeeCheckboxIsChecked('interesting')
})
})
describe('#selectOption', () => {
it('should select option by css', async () => {
await I.amOnPage('/form/select')
await I.selectOption('form select[name=age]', 'adult')
await I.click('Submit')
assert.equal(formContents('age'), 'adult')
})
it('should select option by name', async () => {
await I.amOnPage('/form/select')
await I.selectOption('age', 'adult')
await I.click('Submit')
assert.equal(formContents('age'), 'adult')
})
it('should select option by label', async () => {
await I.amOnPage('/form/select')
await I.selectOption('Select your age', 'dead')
await I.click('Submit')
assert.equal(formContents('age'), 'dead')
})
it('should select option by label and option text', async () => {
await I.amOnPage('/form/select')
await I.selectOption('Select your age', '21-60')
await I.click('Submit')
assert.equal(formContents('age'), 'adult')
})
it('should select option by label and option text - with an onchange callback', async () => {
await I.amOnPage('/form/select_onchange')
await I.selectOption('Select a value', 'Option 2')
await I.click('Submit')
assert.equal(formContents('select'), 'option2')
})
// Could not get multiselect to work with testcafe
it('should select multiple options', async function () {
if (isHelper('TestCafe')) this.skip()
await I.amOnPage('/form/select_multiple')
await I.selectOption('What do you like the most?', ['Play Video Games', 'Have Sex'])
await I.click('Submit')
assert.deepEqual(formContents('like'), ['play', 'adult'])
})
it('should select option by label and option text with additional spaces', async () => {
await I.amOnPage('/form/select_additional_spaces')
await I.selectOption('Select your age', '21-60')
await I.click('Submit')
assert.equal(formContents('age'), 'adult')
})
})
describe('#executeScript', () => {
it('should execute synchronous script', async () => {
await I.amOnPage('/')
await I.executeScript(() => {
document.getElementById('link').innerHTML = 'Appended'
})
await I.see('Appended', 'a')
})
it('should return value from sync script', async () => {
await I.amOnPage('/')
const val = await I.executeScript((a) => a + 5, 5)
assert.equal(val, 10)
})
it('should return value from sync script in iframe', async function () {
// TODO Not yet implemented
if (isHelper('TestCafe')) this.skip() // TODO Not yet implemented
await I.amOnPage('/iframe')
await I.switchTo({ css: 'iframe' })
const val = await I.executeScript(() => document.getElementsByTagName('h1')[0].innerText)
assert.equal(val, 'Information')
})
it('should execute async script', async function () {
if (isHelper('TestCafe')) this.skip() // TODO Not yet implemented
if (isHelper('Playwright')) return // It won't be implemented
await I.amOnPage('/')
const val = await I.executeAsyncScript((val, done) => {
setTimeout(() => {
document.getElementById('link').innerHTML = val
done(5)
}, 100)
}, 'Timeout')
assert.equal(val, 5)
await I.see('Timeout', 'a')
})
})
describe('#fillField, #appendField', () => {
it('should fill input fields', async () => {
await I.amOnPage('/form/field')
await I.fillField('Name', 'Nothing special')
await I.click('Submit')
assert.equal(formContents('name'), 'Nothing special')
})
it('should fill input fields with secrets', async () => {
await I.amOnPage('/form/field')
await I.fillField('Name', secret('Something special'))
await I.click('Submit')
assert.equal(formContents('name'), 'Something special')
})
it('should fill field by css', async () => {
await I.amOnPage('/form/field')
await I.fillField('#name', 'Nothing special')
await I.click('Submit')
assert.equal(formContents('name'), 'Nothing special')
})
it('should fill field by strict locator', async () => {
await I.amOnPage('/form/field')
await I.fillField(
{
id: 'name',
},
'Nothing special',
)
await I.click('Submit')
assert.equal(formContents('name'), 'Nothing special')
})
it('should fill field by name', async () => {
await I.amOnPage('/form/example1')
await I.fillField('LoginForm[username]', 'davert')
await I.fillField('LoginForm[password]', '123456')
await I.click('Login')
assert.equal(formContents('LoginForm').username, 'davert')
assert.equal(formContents('LoginForm').password, '123456')
})
it('should fill textarea by css', async () => {
await I.amOnPage('/form/textarea')
await I.fillField('textarea', 'Nothing special')
await I.click('Submit')
assert.equal(formContents('description'), 'Nothing special')
})
it('should fill textarea by label', async () => {
await I.amOnPage('/form/textarea')
await I.fillField('Description', 'Nothing special')
await I.click('Submit')
assert.equal(formContents('description'), 'Nothing special')
})
it('should fill input by aria-label and aria-labelledby', async () => {
await I.amOnPage('/form/aria')
await I.fillField('My Address', 'Home Sweet Home')
await I.fillField('Phone', '123456')
await I.click('Submit')
assert.equal(formContents('my-form-phone'), '123456')
assert.equal(formContents('my-form-address'), 'Home Sweet Home')
})
it('should fill textarea by overwritting the existing value', async () => {
await I.amOnPage('/form/textarea')
await I.fillField('Description', 'Nothing special')
await I.fillField('Description', 'Some other text')
await I.click('Submit')
assert.equal(formContents('description'), 'Some other text')
})
it('should append field value', async () => {
await I.amOnPage('/form/field')
await I.appendField('Name', '_AND_NEW')
await I.click('Submit')
assert.equal(formContents('name'), 'OLD_VALUE_AND_NEW')
})
it.skip('should not fill invisible fields', async () => {
if (isHelper('Playwright')) return // It won't be implemented
await I.amOnPage('/form/field')
try {
I.fillField('email', 'test@1234')
} catch (e) {
await assert.equal(e.message, 'Error: Field "email" was not found by text|CSS|XPath')
}
})
})
describe('#clearField', () => {
it('should clear a given element', async () => {
await I.amOnPage('/form/field')
await I.fillField('#name', 'Nothing special')
await I.seeInField('#name', 'Nothing special')
await I.clearField('#name')
await I.dontSeeInField('#name', 'Nothing special')
})
it('should clear field by name', async () => {
await I.amOnPage('/form/example1')
await I.clearField('LoginForm[username]')
await I.click('Login')
assert.equal(formContents('LoginForm').username, '')
})
it('should clear field by locator', async () => {
await I.amOnPage('/form/example1')
await I.clearField('#LoginForm_username')
await I.click('Login')
assert.equal(formContents('LoginForm').username, '')
})
})
describe('#type', () => {
it('should type into a field', async function () {
if (isHelper('TestCafe')) this.skip()
await I.amOnPage('/form/field')
await I.click('Name')
await I.type('Type Test')
await I.seeInField('Name', 'Type Test')
await I.fillField('Name', '')
await I.type(['T', 'y', 'p', 'e', '2'])
await I.seeInField('Name', 'Type2')
})
it('should use delay to slow down typing', async function () {
if (isHelper('TestCafe')) this.skip()
await I.amOnPage('/form/field')
await I.fillField('Name', '')
const time = Date.now()
await I.type('12345', 100)
await I.seeInField('Name', '12345')
assert(Date.now() - time > 500)
})
})
describe('check fields: #seeInField, #seeCheckboxIsChecked, ...', () => {
it('should check for empty field', async () => {
await I.amOnPage('/form/empty')
await I.seeInField('#empty_input', '')
})
it('should check for empty textarea', async () => {
await I.amOnPage('/form/empty')
await I.seeInField('#empty_textarea', '')
})
it('should check field equals', async () => {
await I.amOnPage('/form/field')
await I.seeInField('Name', 'OLD_VALUE')
await I.seeInField('name', 'OLD_VALUE')
await I.seeInField('//input[@id="name"]', 'OLD_VALUE')
await I.dontSeeInField('//input[@id="name"]', 'NOtVALUE')
})
it('should check textarea equals', async () => {
await I.amOnPage('/form/textarea')
await I.seeInField('Description', 'sunrise')
await I.seeInField('textarea', 'sunrise')
await I.seeInField('//textarea[@id="description"]', 'sunrise')
await I.dontSeeInField('//textarea[@id="description"]', 'sunset')
})
it('should check checkbox is checked :)', async () => {
await I.amOnPage('/info')
await I.seeCheckboxIsChecked('input[type=checkbox]')
})
it('should check checkbox is not checked', async () => {
await I.amOnPage('/form/checkbox')
await I.dontSeeCheckboxIsChecked('#checkin')
})
it('should match fields with the same name', async () => {
await I.amOnPage('/form/example20')
await I.seeInField("//input[@name='txtName'][2]", 'emma')
await I.seeInField("input[name='txtName']:nth-child(2)", 'emma')
})
})
describe('#grabTextFromAll, #grabHTMLFromAll, #grabValueFromAll, #grabAttributeFromAll', () => {
it('should grab multiple texts from page', async () => {
await I.amOnPage('/info')
let vals = await I.grabTextFromAll('#grab-multiple a')
assert.equal(vals[0], 'First')
assert.equal(vals[1], 'Second')
assert.equal(vals[2], 'Third')
await I.amOnPage('/info')
vals = await I.grabTextFromAll('#invalid-id a')
assert.equal(vals.length, 0)
})
it('should grab multiple html from page', async function () {
if (isHelper('TestCafe')) this.skip()
await I.amOnPage('/info')
let vals = await I.grabHTMLFromAll('#grab-multiple a')
assert.equal(vals[0], 'First')
assert.equal(vals[1], 'Second')
assert.equal(vals[2], 'Third')
await I.amOnPage('/info')
vals = await I.grabHTMLFromAll('#invalid-id a')
assert.equal(vals.length, 0)
})
it('should grab multiple attribute from element', async () => {
await I.amOnPage('/form/empty')
const vals = await I.grabAttributeFromAll(
{
css: 'input',
},
'name',
)
assert.equal(vals[0], 'text')
assert.equal(vals[1], 'empty_input')
})
it('Should return empty array if no attribute found', async () => {
await I.amOnPage('/form/empty')
const vals = await I.grabAttributeFromAll(
{
css: 'div',
},
'test',
)
assert.equal(vals.length, 0)
})
it('should grab values if multiple field matches', async () => {
await I.amOnPage('/form/hidden')
let vals = await I.grabValueFromAll('//form/input')
assert.equal(vals[0], 'kill_people')
assert.equal(vals[1], 'Submit')
vals = await I.grabValueFromAll("//form/input[@name='action']")
assert.equal(vals[0], 'kill_people')
})
it('Should return empty array if no value found', async () => {
await I.amOnPage('/')
const vals = await I.grabValueFromAll('//form/input')
assert.equal(vals.length, 0)
})
})
describe('#grabTextFrom, #grabHTMLFrom, #grabValueFrom, #grabAttributeFrom', () => {
it('should grab text from page', async () => {
await I.amOnPage('/')
let val = await I.grabTextFrom('h1')
assert.equal(val, 'Welcome to test app!')
val = await I.grabTextFrom('//h1')
assert.equal(val, 'Welcome to test app!')
})
it('should grab html from page', async function () {
if (isHelper('TestCafe')) this.skip()
await I.amOnPage('/info')
const val = await I.grabHTMLFrom('#grab-multiple')
assert.equal(
`
<a id="first-link">First</a>
<a id="second-link">Second</a>
<a id="third-link">Third</a>
`,
val,
)
})
it('should grab value from field', async () => {
await I.amOnPage('/form/hidden')
let val = await I.grabValueFrom('#action')
assert.equal(val, 'kill_people')
val = await I.grabValueFrom("//form/input[@name='action']")
assert.equal(val, 'kill_people')
await I.amOnPage('/form/textarea')
val = await I.grabValueFrom('#description')
assert.equal(val, 'sunrise')
await I.amOnPage('/form/select')
val = await I.grabValueFrom('#age')
assert.equal(val, 'oldfag')
})
it('should grab attribute from element', async () => {
await I.amOnPage('/search')
const val = await I.grabAttributeFrom(
{
css: 'form',
},
'method',
)
assert.equal(val, 'get')
})
it('should grab custom attribute from element', async () => {
await I.amOnPage('/form/example4')
const val = await I.grabAttributeFrom(
{
css: '.navbar-toggle',
},
'data-toggle',
)
assert.equal(val, 'collapse')
})
})
describe('page title : #seeTitle, #dontSeeTitle, #grabTitle', () => {
it('should check page title', async function () {
if (isHelper('TestCafe')) this.skip()
await I.amOnPage('/')
await I.seeInTitle('TestEd Beta 2.0')
await I.dontSeeInTitle('Welcome to test app')
await I.amOnPage('/info')
await I.dontSeeInTitle('TestEd Beta 2.0')
})
it('should grab page title', async function () {
if (isHelper('TestCafe')) this.skip()
await I.amOnPage('/')
const val = await I.grabTitle()
assert.equal(val, 'TestEd Beta 2.0')
})
})
describe('#attachFile', () => {
it('should upload file located by CSS', async () => {
await I.amOnPage('/form/file')
await I.attachFile('#avatar', 'app/avatar.jpg')
await I.click('Submit')
await I.see('Thank you')
formContents().files.should.have.key('avatar')
formContents().files.avatar.name.should.eql('avatar.jpg')
formContents().files.avatar.type.should.eql('image/jpeg')
})
it('should upload file located by label', async () => {
await I.amOnPage('/form/file')
await I.attachFile('Avatar', 'app/avatar.jpg')
await I.click('Submit')
await I.see('Thank you')
formContents().files.should.have.key('avatar')
formContents().files.avatar.name.should.eql('avatar.jpg')
formContents().files.avatar.type.should.eql('image/jpeg')
})
})
describe('#saveScreenshot', () => {
beforeEach(() => {
global.output_dir = path.join(global.codecept_dir, 'output')
})
it('should create a screenshot file in output dir', async () => {
const sec = new Date().getUTCMilliseconds()
await I.amOnPage('/')
await I.saveScreenshot(`screenshot_${sec}.png`)
assert.ok(fileExists(path.join(global.output_dir, `screenshot_${sec}.png`)), null, 'file does not exists')
})
it('should create a full page screenshot file in output dir', async () => {
const sec = new Date().getUTCMilliseconds()
await I.amOnPage('/')
await I.saveScreenshot(`screenshot_full_${+sec}.png`, true)
assert.ok(fileExists(path.join(global.output_dir, `screenshot_full_${+sec}.png`)), null, 'file does not exists')
})
})
describe('cookies : #setCookie, #clearCookies, #seeCookie, #waitForCookie', () => {
it('should do all cookie stuff', async () => {
await I.amOnPage('/')
await I.setCookie({
name: 'auth',
value: '123456',
url: 'http://localhost',
})
await I.seeCookie('auth')
await I.dontSeeCookie('auuth')
const cookie = await I.grabCookie('auth')
assert.equal(cookie.value, '123456')
await I.clearCookie('auth')
await I.dontSeeCookie('auth')
})
it('should grab all cookies', async () => {
await I.amOnPage('/')
await I.setCookie({
name: 'auth',
value: '123456',
url: 'http://localhost',
})
await I.setCookie({
name: 'user',
value: 'davert',
url: 'http://localhost',
})
const cookies = await I.grabCookie()
assert.equal(cookies.length, 2)
assert(cookies[0].name)
assert(cookies[0].value)
})
it('should clear all cookies', async () => {
await I.amOnPage('/')
await I.setCookie({
name: 'auth',
value: '123456',
url: 'http://localhost',
})
await I.clearCookie()
await I.dontSeeCookie('auth')
})
it('should wait for cookie and throw error when cookie not found', async () => {
if (isHelper('TestCafe')) return
await I.amOnPage('https://google.com')
try {
await I.waitForCookie('auth', 2)
} catch (e) {
assert.equal(e.message, 'Cookie auth is not found after 2s')
}
})
it('should wait for cookie', async () => {
if (isHelper('TestCafe')) return
await I.amOnPage('/')
await I.setCookie({
name: 'auth',
value: '123456',
url: 'http://localhost',
})
await I.waitForCookie('auth')
})
})
describe('#waitForText', () => {
it('should wait for text', async () => {
await I.amOnPage('/dynamic')
await I.dontSee('Dynamic text')
await I.waitForText('Dynamic text', 2)
await I.see('Dynamic text')
})
it('should wait for text in context', async () => {
await I.amOnPage('/dynamic')
await I.dontSee('Dynamic text')
await I.waitForText('Dynamic text', 2, '#text')
await I.see('Dynamic text')
})
it('should fail if no context', async function () {
if (isHelper('TestCafe')) this.skip()
let failed = false
await I.amOnPage('/dynamic')
await I.dontSee('Dynamic text')
try {
await I.waitForText('Dynamic text', 1, '#fext')
} catch (e) {
failed = true
}
assert.ok(failed)
})
it("should fail if text doesn't contain", async function () {
if (isHelper('TestCafe')) this.skip()
let failed = false
await I.amOnPage('/dynamic')
try {
await I.waitForText('Other text', 1)
} catch (e) {
failed = true
}
assert.ok(failed)
})
it('should fail if text is not in element', async function () {
if (isHelper('TestCafe')) this.skip()
let failed = false
await I.amOnPage('/dynamic')
try {
await I.waitForText('Other text', 1, '#text')
} catch (e) {
failed = true
}
assert.ok(failed)
})
it('should wait for text after timeout', async () => {
await I.amOnPage('/timeout')
await I.dontSee('Timeout text')
await I.waitForText('Timeout text', 31, '#text')
await I.see('Timeout text')
})