-
Notifications
You must be signed in to change notification settings - Fork 11
/
tab_test.go
837 lines (699 loc) · 20.8 KB
/
tab_test.go
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
package autogcd
import (
"bytes"
"crypto/md5"
"encoding/hex"
"fmt"
"os"
"strings"
"sync"
"testing"
"time"
"github.com/wirepair/gcd/gcdapi"
)
func TestTabNavigate(t *testing.T) {
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
tab, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
if _, errorText, err := tab.Navigate(testServerAddr + "index.html"); err != nil {
t.Fatalf("Error navigating: %s %s\n", errorText, err)
}
}
func TestTabGetCurrentUrl(t *testing.T) {
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
tab, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
if _, errorText, err := tab.Navigate(testServerAddr + "console.html?x=1"); err != nil {
t.Fatalf("Error navigating: %s %s\n", errorText, err)
}
tab.WaitStable()
url, err := tab.GetCurrentUrl()
if err != nil {
t.Fatalf("error getting url: %s\n", err)
}
if url != testServerAddr+"console.html?x=1" {
t.Fatalf("expected url is different, got: %s\n", url)
}
}
func TestTabGetTitle(t *testing.T) {
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
tab, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
tab.DebugEvents(true)
if _, errorText, err := tab.Navigate(testServerAddr + "index.html"); err != nil {
t.Fatalf("Error navigating: %s %s\n", errorText, err)
}
title, err := tab.GetTitle()
if err != nil {
t.Fatalf("error getting url: %s\n", err)
}
if title != "autogcd test" {
t.Fatalf("expected title is different, got: %s\n", title)
}
}
func TestTabGetConsoleMessage(t *testing.T) {
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
timeout := time.NewTimer(5 * time.Second)
done := make(chan struct{})
tab, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
msgHandler := func(callerTab *Tab, message *gcdapi.ConsoleConsoleMessage) {
if message.Text == "this is a console message" {
done <- struct{}{}
}
}
tab.GetConsoleMessages(msgHandler)
if _, errorText, err := tab.Navigate(testServerAddr + "console_log.html"); err != nil {
t.Fatalf("Error navigating: %s %s\n", errorText, err)
}
select {
case <-done:
return
case <-timeout.C:
t.Fatalf("error waiting for console message")
}
}
func TestTabGetPageSource(t *testing.T) {
//var src string
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
tab, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
if _, errorText, err := tab.Navigate(testServerAddr + "inner.html"); err != nil {
t.Fatalf("Error navigating: %s %s\n", errorText, err)
}
tab.WaitStable()
ele, err := tab.GetDocument()
if err != nil {
t.Fatalf("error getting tab document")
}
ele.WaitForReady()
_, err = tab.GetPageSource(ele.id)
if err != nil {
t.Fatalf("Error getting page source: %s\n", err)
}
//t.Logf("source: %s\n", src)
}
func TestTabGetBodySize(t *testing.T) {
var err error
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
tab, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
//tab.Debug(true)
if _, errorText, err := tab.Navigate(testServerAddr + "big_body.html"); err != nil {
t.Fatalf("Error navigating: %s %s\n", errorText, err)
}
tab.WaitStable()
bodies, err := tab.GetElementsBySelector("body")
if err != nil {
t.Fatalf("error getting body")
}
for _, body := range bodies {
size_f, _ := body.Dimensions()
t.Logf("body %#v\n", size_f)
}
doc, _ := tab.GetDocument()
size_d, _ := doc.Dimensions()
t.Logf("doc: %#v\n", size_d)
}
func TestTabFrameGetPageSource(t *testing.T) {
var src string
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
tab, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
//tab.Debug(true)
if _, errorText, err := tab.Navigate(testServerAddr + "iframe.html"); err != nil {
t.Fatalf("Error navigating: %s %s\n", errorText, err)
}
err = tab.WaitFor(testWaitRate, testWaitTimeout, ElementByIdReady(tab, "innerfr"))
if err != nil {
t.Fatalf("error waiting for ready state of innerfr: %s\n", err)
}
ele, _, err := tab.GetElementById("innerfr")
if err != nil {
t.Fatalf("error getting inner frame element")
}
if err := ele.WaitForReady(); err != nil {
t.Fatalf("error waiting for innerfr to be ready: %s\n", err)
}
id, err := ele.GetFrameDocumentNodeId()
if err != nil {
t.Fatalf("error getting iframe document node id: %s\n", err)
}
frameEle, _ := tab.GetElementByNodeId(id)
src, err = tab.GetPageSource(frameEle.NodeId())
if err != nil {
t.Fatalf("Error getting page source: %s\n", err)
}
if !strings.Contains(src, "<div>HELLL") {
t.Fatalf("error finding dynamically inserted element in source: %s\n", src)
}
//t.Logf("source: %s\n", src)
}
func TestTabGetFrameResources(t *testing.T) {
var resourceMap map[string]string
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
tab, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
if _, errorText, err := tab.Navigate(testServerAddr + "iframe.html"); err != nil {
t.Fatalf("Error navigating: %s %s\n", errorText, err)
}
resourceMap, err = tab.GetFrameResources()
if err != nil {
t.Fatalf("Error getting page source: %s\n", err)
}
for k, v := range resourceMap {
//t.Logf("id: %s url: %s\n", k, v)
src, wasBase64, err := tab.GetFrameSource(k, v)
if err != nil {
t.Fatalf("error getting frame source: %s\n", err)
}
t.Logf("wasBase64: %v src: %s\n", wasBase64, src)
}
}
func TestTabPromptHandler(t *testing.T) {
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
timeout := time.NewTimer(5 * time.Second)
done := make(chan struct{})
tab, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
promptHandlerFn := func(theTab *Tab, message, promptType string) {
// promptType is only set in Chrome and not chromium? Works in Windows Chrome, not in linux Chromium.
// if promptType == "prompt" {
theTab.Page.HandleJavaScriptDialog(true, "someinput")
//}
}
tab.SetJavaScriptPromptHandler(promptHandlerFn)
msgHandler := func(callerTab *Tab, message *gcdapi.ConsoleConsoleMessage) {
if message.Text == "someinput" {
done <- struct{}{}
}
}
tab.GetConsoleMessages(msgHandler)
if _, errorText, err := tab.Navigate(testServerAddr + "prompt.html"); err != nil {
t.Fatalf("Error navigating: %s %s\n", errorText, err)
}
select {
case <-done:
return
case <-timeout.C:
t.Fatalf("error waiting for console message")
}
}
// prompts will block navigation from returning
func TestTabNavigationTimeout(t *testing.T) {
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
tab, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
tab.SetNavigationTimeout(10)
if _, errorText, err := tab.Navigate(testServerAddr + "prompt.html"); err == nil {
t.Fatalf("did not get an error navigating: %s %s\n", errorText, err)
}
}
func TestTabInjectScript(t *testing.T) {
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
wg := &sync.WaitGroup{}
wg.Add(4) // should be called 2x, one for main page, one for script_inner.html
tab, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
msgHandler := func(callerTab *Tab, message *gcdapi.ConsoleConsoleMessage) {
if strings.Contains(message.Text, "inject") {
//t.Logf("got message: %s\n", message.Text)
wg.Done()
}
}
tab.GetConsoleMessages(msgHandler)
tab.InjectScriptOnLoad("console.log('inject ' + location.href);")
tab.InjectScriptOnLoad("console.log('inject 2' + location.href);")
if _, errorText, err := tab.Navigate(testServerAddr + "script.html"); err != nil {
t.Fatalf("Error navigating: %s %s\n", errorText, err)
}
wg.Wait()
}
func TestTabEvaluateScript(t *testing.T) {
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
wg := &sync.WaitGroup{}
wg.Add(1) // should be called 2x, one for main page, one for script_inner.html
tab, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
msgHandler := func(callerTab *Tab, message *gcdapi.ConsoleConsoleMessage) {
if strings.Contains(message.Text, "inject") {
//t.Logf("got message: %s\n", message.Text)
wg.Done()
}
}
tab.GetConsoleMessages(msgHandler)
if _, errorText, err := tab.Navigate(testServerAddr + "button.html"); err != nil {
t.Fatalf("Error navigating: %s %s\n", errorText, err)
}
_, errEval := tab.EvaluateScript("JSON.stringify(document)")
if errEval != nil {
t.Fatalf("error evaluating script: %s\n", errEval)
}
// trigger completion
_, errEval = tab.EvaluateScript("console.log('inject ' + location.href);")
if errEval != nil {
t.Fatalf("error evaluating trigger script: %s\n", errEval)
}
wg.Wait()
//t.Logf("res: %#v\n", res)
}
func TestTabTwoTabCookies(t *testing.T) {
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
tab1, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
tab2, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
if _, errorText, err := tab1.Navigate(testServerAddr + "cookie1.html"); err != nil {
t.Fatalf("Error navigating: %s %s\n", errorText, err)
}
_, err = tab1.GetCookies()
if err != nil {
t.Fatalf("Error getting first tab cookies: %s\n", err)
}
if _, errorText, err := tab2.Navigate(testServerAddr + "cookie2.html"); err != nil {
t.Fatalf("Error navigating: %s %s\n", errorText, err)
}
_, err = tab2.GetCookies()
if err != nil {
t.Fatalf("Error getting second tab cookies: %s\n", err)
}
_, err = tab1.GetCookies()
if err != nil {
t.Fatalf("Error getting tab1 cookies again: %s\n", err)
}
}
func TestTabNetworkTraffic(t *testing.T) {
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
tab1, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
requestHandlerFn := func(callerTab *Tab, request *NetworkRequest) {
t.Logf("Got network request: %#v\n", request)
}
responseHandlerFn := func(callerTab *Tab, response *NetworkResponse) {
t.Logf("got a network response: %#v\n", response)
}
if err := tab1.GetNetworkTraffic(requestHandlerFn, responseHandlerFn, nil); err != nil {
t.Fatalf("Error listening to network traffic: %s\n", err)
}
if _, errorText, err := tab1.Navigate(testServerAddr + "button.html"); err != nil {
t.Fatalf("Error navigating: %s %s\n", errorText, err)
}
tab2, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
if _, errorText, err := tab2.Navigate(testServerAddr + "console_log.html"); err != nil {
t.Fatalf("Error navigating: %s %s\n", errorText, err)
}
}
func TestTabWindows(t *testing.T) {
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
tab, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
if _, errorText, err := tab.Navigate(testServerAddr + "window_main.html"); err != nil {
t.Fatalf("error opening first window: %s %s", errorText, err)
}
tab.WaitStable()
t.Logf("# of elements: %d\n", len(tab.elements))
ele, _, err := tab.GetElementById("mainwindow")
if err != nil {
t.Fatalf("error getting mainwindow element")
}
ele.WaitForReady()
tabs := testAuto.GetAllTabs()
if err != nil {
t.Fatalf("error getting tabs")
}
newTabs, err := testAuto.RefreshTabList()
if err != nil {
t.Fatalf("Error getting new tabs")
}
t.Logf("%d unknown tabs found!", len(newTabs)-len(tabs))
}
func TestTabAfterRedirect(t *testing.T) {
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
tab, err := testAuto.NewTab()
//tab.Debug(true)
if err != nil {
t.Fatalf("error getting tab")
}
if _, errorText, err := tab.Navigate(testServerAddr + "redirect.html"); err != nil {
t.Fatalf("error opening first window: %s %s", errorText, err)
}
tab.WaitStable()
url, err := tab.GetCurrentUrl()
if err != nil {
t.Fatalf("error getting url: %s\n", err)
}
if url != testServerAddr+"redirect.html" {
t.Fatalf("url does not match redirect.html got: %s\n", url)
}
oldDoc, err := tab.GetDocument()
if err != nil {
t.Fatalf("error getting document")
}
ele, _, err := tab.GetElementById("child")
if err != nil {
t.Fatalf("error getting child element: %s\n", err)
}
err = ele.WaitForReady()
if err != nil {
t.Fatalf("timed out waiting for element")
}
if ele.IsInvalid() {
t.Fatalf("element is invalid before redirect occurred")
}
time.Sleep(4 * time.Second)
err = tab.WaitFor(testWaitRate, testWaitTimeout, ElementByIdReady(tab, "parent_redirect"))
if err != nil {
t.Fatalf("error waiting for ready state of parent_redirect: %s\n", err)
}
newEle, _, err := tab.GetElementById("parent_redirect")
if err != nil {
t.Fatalf("error getting child element: %s\n", err)
}
err = newEle.WaitForReady()
if err != nil {
t.Fatalf("error new element is not ready")
}
newUrl, err := tab.GetCurrentUrl()
if err != nil {
t.Fatalf("error getting url after redirect: %s\n", err)
}
if newUrl != testServerAddr+"redirect_target.html" {
t.Fatalf("url does not match redirect_target.html got: %s\n", newUrl)
}
if !ele.IsInvalid() {
t.Fatalf("error element was not invalidated after redirect!")
}
if !oldDoc.IsInvalid() {
t.Fatalf("error document was not invalidated after redirect!")
}
}
func TestTabGetElementAtLocation(t *testing.T) {
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
tab, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
if _, errorText, err := tab.Navigate(testServerAddr + "button.html"); err != nil {
t.Fatalf("error opening first window: %s %s", errorText, err)
}
tab.WaitStable()
ele, err := tab.GetElementByLocation(0, 0)
if err != nil {
t.Fatalf("error getting element at location 0,0: %s\n", err)
}
ele.WaitForReady()
name, err := ele.GetTagName()
if err != nil {
t.Fatalf("error getting tag name of element: %s\n", err)
}
if name != "html" {
t.Fatalf("Weird, we didn't get html for the element at 0,0")
}
}
func TestTabFrameRedirect(t *testing.T) {
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
tab, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
//tab.Debug(true)
if _, errorText, err := tab.Navigate(testServerAddr + "frame_top.html"); err != nil {
t.Fatalf("error opening first window: %s %s", errorText, err)
}
tab.WaitStable()
ifr, ready, err := tab.GetElementById("frameredirect")
if err != nil {
t.Fatalf("error finding frame element")
}
if !ready {
ifr.WaitForReady()
}
if ifr.IsInvalid() {
t.Fatalf("error frame was invalidated before redirect")
}
// get reference to the frames document before redirect
ifrDocNodeId, err := ifr.GetFrameDocumentNodeId()
if err != nil {
t.Fatalf("error getting doc node of invalidated iframe")
}
ifrDoc, _ := tab.GetElementByNodeId(ifrDocNodeId)
ifrDoc.WaitForReady()
// wait for setTimeout redirect
time.Sleep(6 * time.Second)
if !ifr.IsInvalid() {
t.Fatalf("error iframe was not invalidated after redirect: %d\n", ifr.NodeId())
}
if !ifrDoc.IsInvalid() {
t.Log("frameredirect:")
t.Log(ifr.String())
t.Log("frameredirect doc:")
t.Log(ifrDoc.String())
t.Fatalf("error the iframe elements document was not invalidated after redirect")
}
}
func TestTabMultiTab(t *testing.T) {
numTabs := 5
tabs := make([]*Tab, numTabs)
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
for i := 0; i < numTabs; i++ {
tab, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error opening tab %d %s\n", i, err)
}
tabs[i] = tab
}
wg := &sync.WaitGroup{}
for _, tab := range tabs {
wg.Add(1)
testMultiNavigateSendKeys(t, wg, tab)
}
wg.Wait()
}
// This only works in chromium
func TestTabNavigationError(t *testing.T) {
if !strings.Contains(testPath, "chromium") {
return
}
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
tab, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
// test invalid domain name
if _, errorText, err := tab.Navigate("http://asdfasdf"); err != nil {
t.Fatalf("error opening first window: %s %s", errorText, err)
}
tab.WaitStable()
ret, failText := tab.DidNavigationFail()
if ret == false {
t.Fatalf("navigation should have failed but got false back\n")
} else {
t.Logf("nav error: %s\n", failText)
}
// test unopen port
if _, errorText, err := tab.Navigate("http://127.0.0.1:19145"); err != nil {
t.Fatalf("error opening window: %s %s", errorText, err)
}
tab.WaitStable()
ret, failText = tab.DidNavigationFail()
if ret == false {
t.Fatalf("navigation should have failed but got false back\n")
} else {
t.Logf("nav error: %s\n", failText)
}
// test valid site
if _, errorText, err := tab.Navigate(testServerAddr); err != nil {
t.Fatalf("error opening window: %s %s", errorText, err)
}
tab.WaitStable()
ret, failText = tab.DidNavigationFail()
if ret == true {
t.Fatalf("navigation should have succeeded but got error back: %s\n", failText)
}
}
func TestTabSslError(t *testing.T) {
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
tab, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
//tab.Debug(true)
// Test expired SSL certificate
// "--test-type", "--ignore-certificate-errors", should not return any errors
if _, errorText, err := tab.Navigate("https://expired.identrustssl.com/"); err != nil {
t.Fatalf("error opening first window: %s %s", errorText, err)
}
ret, failText := tab.DidNavigationFail()
t.Logf("ret: %t failText: %s\n", ret, failText)
if ret == true {
t.Fatalf("navigation should have succeeded but got failed back: %s\n", failText)
}
// Test invalid CN name
// example.com ip https://93.184.216.34/
// "--test-type", "--ignore-certificate-errors", should not return any errors
if _, errorText, err := tab.Navigate("https://93.184.216.34/"); err != nil {
t.Fatalf("error opening invalid cn host: %s %s\n", errorText, err)
}
ret, failText = tab.DidNavigationFail()
t.Logf("ret: %t failText: %s\n", ret, failText)
if ret == true {
t.Fatalf("navigation should have succeeded but got failed back: %s\n", failText)
}
}
func TestTabChromeCrash(t *testing.T) {
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
tab, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
timeout := time.NewTimer(time.Second * 10)
doneCh := make(chan struct{})
handlerFn := func(tab *Tab, reason string) {
doneCh <- struct{}{}
}
tab.Debug(true)
tab.DebugEvents(true)
go func() {
<-timeout.C
t.Fatalf("timed out waiting for termination event")
}()
tab.SetDisconnectedHandler(handlerFn)
// apparently chrome://crash does not disconnect any more? but this will. id: 4/22/2018
if _, _, err := tab.Navigate("chrome://inducebrowsercrashforrealz/"); err == nil {
t.Fatalf("crash window did not cause error\n")
}
<-doneCh
}
func testMultiNavigateSendKeys(t *testing.T, wg *sync.WaitGroup, tab *Tab) {
var err error
var ele *Element
msgHandler := func(callerTab *Tab, message *gcdapi.ConsoleConsoleMessage) {
if message.Text == "zomgs Test!" {
callerTab.StopConsoleMessages(true)
wg.Done()
}
}
tab.GetConsoleMessages(msgHandler)
if _, errorText, err := tab.Navigate(testServerAddr + "input.html"); err != nil {
t.Fatalf("Error navigating: %s %s\n", errorText, err)
}
tab.WaitStable()
ele, _, err = tab.GetElementById("attr")
if err != nil {
t.Fatalf("error finding input attr: %s\n", err)
}
err = ele.SendKeys("zomgs Test!\n")
if err != nil {
t.Fatalf("error sending keys: %s\n", err)
}
}
func TestTabSearchBySelector(t *testing.T) {
var err error
testAuto := testDefaultStartup(t)
defer testAuto.Shutdown()
tab, err := testAuto.NewTab()
if err != nil {
t.Fatalf("error getting tab")
}
//tab.Debug(true)
if _, errorText, err := tab.Navigate(testServerAddr + "table.html"); err != nil {
t.Fatalf("Error navigating: %s %s\n", errorText, err)
}
tab.WaitStable()
cells, err := tab.GetElementsBySearch(`//table[@id="t1"]//tr/td[1]/text()`, false)
if err != nil {
t.Fatalf("error getting cells")
}
for _, cell := range cells {
t.Logf("cell %#v\n", cell.characterData)
}
}
func TestTabFullPageScreenShot(t *testing.T) {
originalHash, err := hex.DecodeString("5c16850da3d7a40fd18313f0432e15df")
if err != nil {
t.Fatalf("invalid original hash value: %s\n", err)
}
testAuto := testHeadlessStartup(t)
defer testAuto.Shutdown()
tab, err := testAuto.NewTab()
defer testAuto.CloseTab(tab)
if err != nil {
t.Fatalf("error getting tab")
}
if _, errorText, err := tab.Navigate(testServerAddr + "background.html"); err != nil {
t.Fatalf("error navigating: %s %s\n", errorText, err)
}
tab.WaitStable()
data, err := tab.GetFullPageScreenShot()
if err != nil {
t.Fatalf("error making full page screenshot: %s\n", err)
}
f, errFile := os.Create("test.png")
defer f.Close()
if errFile != nil {
fmt.Printf("error creating image file: %s\n", errFile)
return
}
f.Write(data)
newHash := md5.Sum(data)
if !bytes.Equal(newHash[:], originalHash) {
t.Fatalf("error, MD5 for full page screenshot %x does not match predefined value: %x\n", newHash[:], originalHash)
}
}