-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
index.js
2694 lines (2339 loc) · 76 KB
/
index.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
/* global it, describe, before, beforeEach, after, afterEach */
/**
* Module dependencies.
*/
require('mocha-generators').install()
var Nightmare = require('..')
var IPC = require('../lib/ipc')
var chai = require('chai')
var url = require('url')
var server = require('./server')
var https = require('https')
var fs = require('fs')
var mkdirp = require('mkdirp')
var path = require('path')
var rimraf = require('rimraf')
var child_process = require('child_process')
var PNG = require('pngjs').PNG
var should = chai.should()
var split = require('split')
var asPromised = require('chai-as-promised')
var assert = require('assert')
chai.use(asPromised)
/**
* Temporary directory
*/
var tmp_dir = path.join(__dirname, 'tmp')
/**
* Get rid of a warning.
*/
process.setMaxListeners(0)
/**
* Locals.
*/
var base = 'http://localhost:7500/'
describe('Nightmare', function() {
before(function(done) {
server.listen(7500, done)
Nightmare = withDeprecationTracking(Nightmare)
})
after(function() {
Nightmare.assertNoDeprecations()
})
it('should be constructable', function*() {
var nightmare = Nightmare()
nightmare.should.be.ok
yield nightmare.end()
})
it('should have version information', function*() {
var nightmare = Nightmare()
var versions = yield nightmare.engineVersions()
nightmare.engineVersions.electron.should.be.ok
nightmare.engineVersions.chrome.should.be.ok
versions.electron.should.be.ok
versions.chrome.should.be.ok
Nightmare.version.should.be.ok
yield nightmare.end()
})
it('should kill its electron process when it is killed', function(done) {
var child = child_process.fork(
path.join(__dirname, 'files', 'nightmare-unended.js')
)
child.once('message', function(electronPid) {
child.once('exit', function() {
try {
electronPid.should.not.be.a.process
} catch (error) {
// if the test failed, clean up the still-running process
process.kill(electronPid, 'SIGINT')
throw error
}
done()
})
child.kill()
})
})
it('should gracefully handle electron being killed', function(done) {
var child = child_process.fork(
path.join(__dirname, 'files', 'nightmare-unended.js')
)
child.once('message', function(electronPid) {
process.kill(electronPid, 'SIGINT')
child.once('exit', function() {
electronPid.should.not.be.a.process
done()
})
})
})
it('should end gracefully if the chain has not been started', function(done) {
var child = child_process.fork(
path.join(__dirname, 'files', 'nightmare-created.js')
)
child.once('message', function() {
child.once('exit', function(code) {
code.should.equal(0)
done()
})
child.kill()
})
})
it('should exit with a non-zero code on uncaughtExecption', function(done) {
var child = child_process.fork(
path.join(__dirname, 'files', 'nightmare-error.js'),
[],
{ silent: true }
)
child.once('exit', function(code) {
code.should.not.equal(0)
done()
})
})
it('should provide a .catch function', function(done) {
var nightmare = Nightmare()
nightmare
.goto('about:blank')
.evaluate(function() {
throw new Error('Test')
})
.catch(function(err) {
assert.equal(err instanceof Error, true)
assert.equal(err.message, 'Test')
done()
})
})
it('should allow ending more than once', function(done) {
var nightmare = Nightmare()
nightmare
.goto(fixture('navigation'))
.end()
.then(() => nightmare.end())
.then(() => done())
})
it('should allow end with a callback', function(done) {
var nightmare = Nightmare()
nightmare.goto(fixture('navigation')).end(() => done())
})
it('should allow end with a callback to be thenable', function(done) {
var nightmare = Nightmare()
nightmare
.goto(fixture('navigation'))
.end(() => 'nightmare')
.then(str => {
str.should.equal('nightmare')
done()
})
})
it('should kill electron process when halted', function() {
var nightmare = Nightmare()
const check1 = nightmare
.goto(fixture('navigation'))
.wait(1000)
.wait(500)
.end()
.then(() => {})
.should.be.rejectedWith('Nightmare Halted')
const electronPid = nightmare.proc.pid
const check2 = new Promise((resolve, _reject) => {
nightmare.halt('Nightmare Halted', () => {
electronPid.should.not.be.a.process
resolve()
})
})
return Promise.all([check1, check2])
})
it('should successfully end on pages setting onunload or onbeforeunload', function(done) {
var nightmare = Nightmare()
nightmare
.goto(fixture('unload'))
.end()
.then(() => done())
})
it('should successfully end on pages binding unload or beforeunload', function(done) {
var nightmare = Nightmare()
nightmare
.goto(fixture('unload/add-event-listener.html'))
.end()
.then(() => done())
})
it('should provide useful errors for .click', function(done) {
var nightmare = Nightmare()
nightmare
.goto('about:blank')
.click('a.not-here')
.catch(function(err) {
assert.equal(err instanceof Error, true)
err.message.should.include('a.not-here')
done()
})
})
it('should provide useful errors for .mousedown', function(done) {
var nightmare = Nightmare()
nightmare
.goto('about:blank')
.mousedown('a.not-here')
.catch(function(err) {
assert.equal(err instanceof Error, true)
err.message.should.include('a.not-here')
done()
})
})
it('should provide useful errors for .mouseup', function(done) {
var nightmare = Nightmare()
nightmare
.goto('about:blank')
.mouseup('a.not-here')
.catch(function(err) {
assert.equal(err instanceof Error, true)
err.message.should.include('a.not-here')
done()
})
})
it('should provide useful errors for .mouseover', function(done) {
var nightmare = Nightmare()
nightmare
.goto('about:blank')
.mouseover('a.not-here')
.catch(function(err) {
assert.equal(err instanceof Error, true)
err.message.should.include('a.not-here')
done()
})
})
describe('navigation', function() {
var nightmare
beforeEach(function() {
nightmare = Nightmare({
webPreferences: { partition: 'test-partition' + Math.random() },
loadTimeout: 45 * 1000,
waitTimeout: 5 * 1000
})
})
afterEach(function*() {
yield nightmare.end()
Nightmare.resetActions()
})
it('should return data about the response', function*() {
var data = yield nightmare.goto(fixture('navigation'))
data.should.contain.keys('url', 'code', 'method', 'referrer', 'headers')
})
it('should reject with a useful message when no URL', function() {
return nightmare.goto(undefined).then(
function() {
throw new Error('goto(undefined) didn’t cause an error')
},
function(err) {
assert.equal(err instanceof Error, true)
err.message.should.include('url')
}
)
})
it('should reject with a useful message for an empty URL', function() {
return nightmare.goto('').then(
function() {
throw new Error('goto(undefined) didn’t cause an error')
},
function(err) {
assert.equal(err instanceof Error, true)
err.message.should.include('url')
}
)
})
it('should click on a link and then go back', function*() {
var title = yield nightmare
.goto(fixture('navigation'))
.click('a')
.title()
title.should.equal('A')
title = yield nightmare.back().title()
title.should.equal('Navigation')
})
it('should work for links that dont go anywhere', function*() {
var title = yield nightmare
.goto(fixture('navigation'))
.click('a')
.title()
title.should.equal('A')
title = yield nightmare.click('.d').title()
title.should.equal('A')
})
it('should click on a link, go back, and then go forward', function*() {
yield nightmare
.goto(fixture('navigation'))
.click('a')
.back()
.forward()
})
it('should refresh the page', function*() {
yield nightmare.goto(fixture('navigation')).refresh()
})
it('should wait until element is present', function*() {
yield nightmare.goto(fixture('navigation')).wait('a')
})
it('should soft timeout if element does not appear', function*() {
yield nightmare.goto(fixture('navigation')).wait('ul', 150)
})
it('should wait until element is present with a modified poll interval', function*() {
nightmare = Nightmare({
pollInterval: 50
})
yield nightmare.goto(fixture('navigation')).wait('a')
})
it('should escape the css selector correctly when waiting for an element', function*() {
yield nightmare.goto(fixture('navigation')).wait('#escaping\\:test')
})
it('should wait until the evaluate fn returns true', function*() {
yield nightmare.goto(fixture('navigation')).wait(function() {
var text = document.querySelector('a').textContent
return text === 'A'
})
})
it('should wait until the evaluate fn with arguments returns true', function*() {
yield nightmare.goto(fixture('navigation')).wait(
function(expectedA, expectedB) {
var textA = document.querySelector('a.a').textContent
var textB = document.querySelector('a.b').textContent
return expectedA === textA && expectedB === textB
},
'A',
'B'
)
})
describe('asynchronous wait', function() {
it('should wait until the evaluate fn with arguments returns true with a callback', function*() {
yield nightmare.goto(fixture('navigation')).wait(
function(expectedA, expectedB, done) {
setTimeout(() => {
var textA = document.querySelector('a.a').textContent
var textB = document.querySelector('a.b').textContent
done(null, expectedA === textA && expectedB === textB)
}, 2000)
},
'A',
'B'
)
})
it('should wait until the evaluate fn with arguments returns true with a promise', function*() {
yield nightmare.goto(fixture('navigation')).wait(
function(expectedA, expectedB) {
return new Promise(function(resolve) {
setTimeout(() => {
var textA = document.querySelector('a.a').textContent
var textB = document.querySelector('a.b').textContent
resolve(expectedA === textA && expectedB === textB)
}, 2000)
})
},
'A',
'B'
)
})
it('should reject timeout on wait', function*() {
yield nightmare.goto(fixture('navigation')).wait(function(_done) {
//never call done
}).should.be.rejected
})
it('should reject timeout on wait with selector', function*() {
yield nightmare
.goto(fixture('navigation'))
.wait('#non-existent')
.should.be.rejected.then(function(error) {
error.message.should.include('#non-existent')
})
})
it('should run multiple times before timeout on wait', function*() {
yield nightmare.goto(fixture('navigation')).wait(function(done) {
setTimeout(() => done(null, false), 500)
}).should.be.rejected
})
})
it('should fail if navigation target is invalid', function() {
return nightmare.goto('http://this-is-not-a-real-domain.tld').then(
function() {
throw new Error('Navigation to an invalid domain succeeded')
},
function(err) {
assert.equal(err instanceof Error, true)
assert.equal(err.message, 'navigation error')
assert.equal(err.details, 'ERR_NAME_NOT_RESOLVED')
assert.equal(err.code, -105)
assert.equal(err.url, 'http://this-is-not-a-real-domain.tld/')
}
)
})
it('should fail if navigation target is a malformed URL', function(done) {
nightmare
.goto('somewhere out there')
.then(function() {
done(new Error('Navigation to an invalid domain succeeded'))
})
.catch(function(_error) {
done()
})
})
it('should fail if navigating to an unknown protocol', function(done) {
nightmare
.goto('fake-protocol://blahblahblah')
.then(function() {
done(new Error('Navigation to an invalid protocol succeeded'))
})
.catch(function(_error) {
done()
})
})
it('should not fail if the URL loads but a resource fails', function() {
return nightmare.goto(fixture('navigation/invalid-image'))
})
it('should not fail if a child frame fails', function() {
return nightmare.goto(fixture('navigation/invalid-frame'))
})
it('should return correct data when child frames are present', function*() {
var data = yield nightmare.goto(fixture('navigation/valid-frame'))
data.should.have.property('url')
data.url.should.equal(fixture('navigation/valid-frame'))
})
it('should not fail if response was a valid error (e.g. 404)', function() {
return nightmare.goto(fixture('navigation/not-a-real-page'))
})
it('should fail if the response dies in flight', function(done) {
nightmare
.goto(fixture('do-not-respond'))
.then(function() {
done(new Error('Navigation succeeded but server connection died'))
})
.catch(function(_error) {
done()
})
})
it('should not fail for a redirect', function() {
return nightmare.goto(fixture('redirect?url=%2Fnavigation'))
})
it('should fail for a redirect to an invalid URL', function(done) {
nightmare
.goto(
fixture('redirect?url=http%3A%2F%2Fthis-is-not-a-real-domain.tld')
)
.then(function() {
done(new Error('Navigation succeeded with redirect to bad location'))
})
.catch(function(_error) {
done()
})
})
it('should succeed properly if request handler is present', function() {
Nightmare.action(
'monitorRequest',
function(name, options, parent, win, renderer, done) {
win.webContents.session.webRequest.onBeforeRequest(
['*://localhost:*'],
function(details, callback) {
callback({ cancel: false })
}
)
done()
},
function(done) {
done()
return this
}
)
return Nightmare({ webPreferences: { partition: 'test-partition' } })
.goto(fixture('navigation'))
.end()
})
it('should fail properly if request handler is present', function(done) {
Nightmare.action(
'monitorRequest',
function(name, options, parent, win, renderer, done) {
win.webContents.session.webRequest.onBeforeRequest(
['*://localhost:*'],
function(details, callback) {
callback({ cancel: false })
}
)
done()
},
function(done) {
done()
return this
}
)
Nightmare({ webPreferences: { partition: 'test-partition' } })
.goto('http://this-is-not-a-real-domain.tld')
.then(function() {
done(new Error('Navigation to an invalid domain succeeded'))
})
.catch(function(_error) {
done()
})
})
it('should support javascript URLs', function*() {
var gotoResult = yield nightmare
.goto(fixture('navigation'))
.goto(
'javascript:void(document.querySelector(".a").textContent="LINK");'
)
gotoResult.should.be.an('object')
var linkText = yield nightmare.evaluate(function() {
return document.querySelector('.a').textContent
})
linkText.should.equal('LINK')
})
it('should support javascript URLs that load pages', function*() {
var data = yield nightmare
.goto(fixture('navigation'))
.goto(`javascript:window.location='${fixture('navigation/a.html')}'`)
data.should.contain.keys('url', 'code', 'method', 'referrer', 'headers')
data.url.should.equal(fixture('navigation/a.html'))
var linkText = yield nightmare.evaluate(function() {
return document.querySelector('.d').textContent
})
linkText.should.equal('D')
})
it('should fail immediately/not time out for 304 statuses', function() {
return Nightmare({ gotoTimeout: 500 })
.goto(fixture('not-modified'))
.end()
.then(
function() {
throw new Error('Navigating to a 304 should return an error')
},
function(error) {
if (error.code === -7) {
throw new Error('Navigating to a 304 should not time out')
}
}
)
})
it('should not time out for aborted loads', function() {
Nightmare.action(
'abortRequests',
function(name, options, parent, win, renderer, done) {
win.webContents.session.webRequest.onBeforeRequest(
['*://localhost:*'],
function(details, callback) {
setTimeout(() => win.webContents.stop(), 0)
callback({ cancel: false })
}
)
done()
},
function() {}
)
return Nightmare({ gotoTimeout: 500 })
.goto(fixture('navigation'))
.end()
.then(
function() {
throw new Error('An aborted page load should return an error')
},
function(error) {
if (error.code === -7) {
throw new Error('Aborting a page load should not time out')
}
}
)
})
describe('timeouts', function() {
it('should time out after 30 seconds of loading', function() {
// allow this test to go particularly long
this.timeout(40000)
return nightmare
.goto(fixture('wait'))
.should.be.rejected.then(function(error) {
error.code.should.equal(-7)
})
})
it('should allow custom goto timeout on the constructor', function() {
var startTime = Date.now()
return Nightmare({ gotoTimeout: 1000 })
.goto(fixture('wait'))
.end()
.should.be.rejected.then(function(_error) {
// allow a few extra seconds for browser startup
;(startTime - Date.now()).should.be.below(3000)
})
})
it('should allow a timeout to succeed if DOM loaded', function() {
return Nightmare({ gotoTimeout: 1000 })
.goto(fixture('navigation/hanging-resources.html'))
.end()
.then(function(data) {
data.details.should.include('1000 ms')
})
})
it('should allow actions on a hanging page', function() {
return Nightmare({ gotoTimeout: 500 })
.goto(fixture('navigation/hanging-resources.html'))
.evaluate(() => document.title)
.end()
.then(function(title) {
title.should.equal('Hanging resource load')
})
})
it('should allow loading a new page after timing out', function() {
nightmare.end().then()
nightmare = Nightmare({ gotoTimeout: 1000 })
return nightmare
.goto(fixture('wait'))
.should.be.rejected.then(function() {
return nightmare.goto(fixture('navigation'))
})
})
it('should allow for timeouts for non-goto loads', function*() {
// ###
this.timeout(40000)
var nightmare = Nightmare({ loadTimeout: 30000 })
yield nightmare.goto(fixture('navigation')).click('#never-ends')
yield nightmare.end()
})
})
})
describe('evaluation', function() {
var nightmare
beforeEach(function() {
nightmare = Nightmare()
})
afterEach(function*() {
yield nightmare.end()
})
it('should get the title', function*() {
var title = yield nightmare.goto(fixture('evaluation')).title()
title.should.eql('Evaluation')
})
it('should get the url', function*() {
var url = yield nightmare.goto(fixture('evaluation')).url()
url.should.have.string(fixture('evaluation'))
})
it('should get the path', function*() {
var path = yield nightmare.goto(fixture('evaluation')).path()
var formalUrl = fixture('evaluation') + '/'
formalUrl.should.have.string(path)
})
it('should check if the selector exists', function*() {
// existent element
var exists = yield nightmare
.goto(fixture('evaluation'))
.exists('h1.title')
exists.should.be.true
// non-existent element
exists = yield nightmare.exists('a.blahblahblah')
exists.should.be.false
})
it('should check if an element is visible', function*() {
// visible element
var visible = yield nightmare
.goto(fixture('evaluation'))
.visible('h1.title')
visible.should.be.true
// hidden element
visible = yield nightmare.visible('.hidden')
visible.should.be.false
// non-existent element
visible = yield nightmare.visible('#asdfasdfasdf')
visible.should.be.false
})
it('should evaluate javascript on the page, with parameters', function*() {
var title = yield nightmare
.goto(fixture('evaluation'))
.evaluate(function(parameter) {
return document.title + ' -- ' + parameter
}, 'testparameter')
title.should.equal('Evaluation -- testparameter')
})
it('should capture invalid evaluate fn', function() {
return nightmare.goto(fixture('evaluation')).evaluate('not_a_function')
.should.be.rejected
})
describe('asynchronous', function() {
it('should allow for asynchronous evaluation with a callback', function*() {
var asyncValue = yield nightmare
.goto(fixture('evaluation'))
.evaluate(function(done) {
setTimeout(() => done(null, 'nightmare'), 1000)
})
asyncValue.should.equal('nightmare')
})
it('should allow for arguments with asynchronous evaluation with a callback', function*() {
var asyncValue = yield nightmare
.goto(fixture('evaluation'))
.evaluate(function(str, done) {
setTimeout(() => done(null, str), 1000)
}, 'nightmare')
asyncValue.should.equal('nightmare')
})
it('should allow for errors in asynchronous evaluation with a callback', function*() {
yield nightmare.goto(fixture('evaluation')).evaluate(function(done) {
setTimeout(() => done(new Error('nightmare')), 1000)
}).should.be.rejected
})
it('should allow for timeouts in asynchronous evaluation with a callback', function*() {
this.timeout(40000)
yield nightmare.goto(fixture('evaluation')).evaluate(function(_done) {
//don't call done
}).should.be.rejected
})
it('should allow for asynchronous evaluation with a promise', function*() {
var asyncValue = yield nightmare
.goto(fixture('evaluation'))
.evaluate(function() {
return new Promise(resolve => {
setTimeout(() => resolve('nightmare'), 1000)
})
})
asyncValue.should.equal('nightmare')
})
it('should allow for arguments with asynchronous evaluation with a promise', function*() {
var asyncValue = yield nightmare
.goto(fixture('evaluation'))
.evaluate(function(str) {
return new Promise(resolve => {
setTimeout(() => resolve(str), 1000)
})
}, 'nightmare')
asyncValue.should.equal('nightmare')
})
it('should allow for errors in asynchronous evaluation with a promise', function*() {
yield nightmare.goto(fixture('evaluation')).evaluate(function() {
return new Promise((resolve, reject) => {
setTimeout(() => reject(new Error('nightmare')), 1000)
})
}).should.be.rejected
})
it('should allow for timeouts in asynchronous evaluation with a promise', function*() {
this.timeout(40000)
yield nightmare.goto(fixture('evaluation')).evaluate(function() {
return new Promise((_resolve, _reject) => {
return 'nightmare'
})
}).should.be.rejected
})
})
})
describe('manipulation', function() {
var nightmare
beforeEach(function() {
nightmare = Nightmare()
})
afterEach(function*() {
yield nightmare.end()
})
it('should inject javascript onto the page', function*() {
var globalNumber = yield nightmare
.goto(fixture('manipulation'))
.inject('js', 'test/files/globals.js')
.evaluate(function() {
return globalNumber
})
globalNumber.should.equal(7)
var numAnchors = yield nightmare
.goto(fixture('manipulation'))
.inject('js', 'test/files/jquery-2.1.1.min.js')
.evaluate(function() {
return window.$('h1').length
})
numAnchors.should.equal(1)
})
it('should inject javascript onto the page ending with a comment', function*() {
var globalNumber = yield nightmare
.goto(fixture('manipulation'))
.inject('js', 'test/files/globals.js')
.evaluate(function() {
return globalNumber
})
globalNumber.should.equal(7)
var numAnchors = yield nightmare
.goto(fixture('manipulation'))
.inject('js', 'test/files/jquery-1.9.0.min.js')
.evaluate(function() {
return window.$('h1').length
})
numAnchors.should.equal(1)
})
it('should inject css onto the page', function*() {
var color = yield nightmare
.goto(fixture('manipulation'))
.inject('js', 'test/files/jquery-2.1.1.min.js')
.inject('css', 'test/files/test.css')
.evaluate(function() {
return window.$('body').css('background-color')
})
color.should.equal('rgb(255, 0, 0)')
})
it('should not inject unsupported types onto the page', function*() {
var color = yield nightmare
.goto(fixture('manipulation'))
.inject('js', 'test/files/jquery-2.1.1.min.js')
.inject('pdf', 'test/files/test.css')
.evaluate(function() {
return window.$('body').css('background-color')
})
color.should.not.equal('rgb(255, 0, 0)')
})
it('should type', function*() {
var input = 'nightmare'
var events = input.length * 3
var value = yield nightmare
.on('console', function(type, _input, _message) {
if (type === 'log') events--
})
.goto(fixture('manipulation'))
.type('input[type=search]', input)
.evaluate(function() {
return document.querySelector('input[type=search]').value
})
value.should.equal('nightmare')
events.should.equal(0)
})
it('should type integer', function*() {
var input = 10
var events = input.toString().length * 3
var value = yield nightmare
.on('console', function(type, _input, _message) {
if (type === 'log') events--
})
.goto(fixture('manipulation'))
.type('input[type=search]', input)
.evaluate(function() {
return document.querySelector('input[type=search]').value
})
value.should.equal('10')
events.should.equal(0)
})
it('should type object', function*() {
var input = {
foo: 'bar'
}
var events = input.toString().length * 3
var value = yield nightmare
.on('console', function(type, _input, _message) {
if (type === 'log') events--
})
.goto(fixture('manipulation'))
.type('input[type=search]', input)
.evaluate(function() {
return document.querySelector('input[type=search]').value
})
value.should.equal('[object Object]')
events.should.equal(0)
})
it('should clear inputs', function*() {
var input = 'nightmare'
var events = input.length * 3
var value = yield nightmare
.on('console', function(type, _input, _message) {
if (type === 'log') events--
})
.goto(fixture('manipulation'))
.type('input[type=search]', input)
.type('input[type=search]')
.evaluate(function() {
return document.querySelector('input[type=search]').value
})
value.should.equal('')
events.should.equal(0)
})