forked from go-rod/rod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
must.go
1074 lines (919 loc) · 25.9 KB
/
must.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
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
// This file contains the methods that panics when error return value is not nil.
// Their function names are all prefixed with Must.
// A function here is usually a wrapper for the error version with fixed default options to make it easier to use.
//
// For example the source code of `Element.Click` and `Element.MustClick`. `MustClick` has no argument.
// But `Click` has a `button` argument to decide which button to click.
// `MustClick` feels like a version of `Click` with some default behaviors.
package rod
import (
"errors"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/go-rod/rod/lib/devices"
"github.com/go-rod/rod/lib/proto"
"github.com/go-rod/rod/lib/utils"
"github.com/ysmood/gson"
)
// It must be generated by genE.
type eFunc func(args ...interface{})
// Generate a eFunc with the specified fail function.
// If the last arg of eFunc is error the fail will be called.
func genE(fail func(interface{})) eFunc {
return func(args ...interface{}) {
err, ok := args[len(args)-1].(error)
if ok {
fail(err)
}
}
}
// WithPanic returns a browser clone with the specified panic function.
// The fail must stop the current goroutine's execution immediately, such as use runtime.Goexit() or panic inside it.
func (b *Browser) WithPanic(fail func(interface{})) *Browser {
n := *b
n.e = genE(fail)
return &n
}
// MustConnect is similar to Browser.Connect
func (b *Browser) MustConnect() *Browser {
b.e(b.Connect())
return b
}
// MustClose is similar to Browser.Close
func (b *Browser) MustClose() {
_ = b.Close()
}
// MustIncognito is similar to Browser.Incognito
func (b *Browser) MustIncognito() *Browser {
b, err := b.Incognito()
b.e(err)
return b
}
// MustPage is similar to Browser.Page.
// The url list will be joined by "/".
func (b *Browser) MustPage(url ...string) *Page {
p, err := b.Page(proto.TargetCreateTarget{URL: strings.Join(url, "/")})
b.e(err)
return p
}
// MustPages is similar to Browser.Pages
func (b *Browser) MustPages() Pages {
list, err := b.Pages()
b.e(err)
return list
}
// MustPageFromTargetID is similar to Browser.PageFromTargetID
func (b *Browser) MustPageFromTargetID(targetID proto.TargetTargetID) *Page {
p, err := b.PageFromTarget(targetID)
b.e(err)
return p
}
// MustHandleAuth is similar to Browser.HandleAuth
func (b *Browser) MustHandleAuth(username, password string) (wait func()) {
w := b.HandleAuth(username, password)
return func() { b.e(w()) }
}
// MustIgnoreCertErrors is similar to Browser.IgnoreCertErrors
func (b *Browser) MustIgnoreCertErrors(enable bool) *Browser {
b.e(b.IgnoreCertErrors(enable))
return b
}
// MustGetCookies is similar Browser.GetCookies
func (b *Browser) MustGetCookies() []*proto.NetworkCookie {
nc, err := b.GetCookies()
b.e(err)
return nc
}
// MustSetCookies is similar Browser.SetCookies.
// If the len(cookies) is 0 it will clear all the cookies.
func (b *Browser) MustSetCookies(cookies ...*proto.NetworkCookie) *Browser {
if len(cookies) == 0 {
b.e(b.SetCookies(nil))
} else {
b.e(b.SetCookies(proto.CookiesToParams(cookies)))
}
return b
}
// MustWaitDownload is similar to Browser.WaitDownload.
// It will read the file into bytes then remove the file.
func (b *Browser) MustWaitDownload() func() []byte {
tmpDir := filepath.Join(os.TempDir(), "rod", "downloads")
wait := b.WaitDownload(tmpDir)
return func() []byte {
info := wait()
path := filepath.Join(tmpDir, info.GUID)
defer func() { _ = os.Remove(path) }()
data, err := ioutil.ReadFile(path)
b.e(err)
return data
}
}
// MustFind is similar to Browser.Find
func (ps Pages) MustFind(selector string) *Page {
p, err := ps.Find(selector)
if err != nil {
if len(ps) > 0 {
ps[0].e(err)
} else {
// fallback to utils.E, because we don't have enough
// context to call the scope `.e`.
utils.E(err)
}
}
return p
}
// MustFindByURL is similar to Page.FindByURL
func (ps Pages) MustFindByURL(regex string) *Page {
p, err := ps.FindByURL(regex)
if err != nil {
if len(ps) > 0 {
ps[0].e(err)
} else {
// fallback to utils.E, because we don't have enough
// context to call the scope `.e`.
utils.E(err)
}
}
return p
}
// WithPanic returns a page clone with the specified panic function.
// The fail must stop the current goroutine's execution immediately, such as use runtime.Goexit() or panic inside it.
func (p *Page) WithPanic(fail func(interface{})) *Page {
n := *p
n.e = genE(fail)
return &n
}
// MustInfo is similar to Page.Info
func (p *Page) MustInfo() *proto.TargetTargetInfo {
info, err := p.Info()
p.e(err)
return info
}
// MustHTML is similar to Page.HTML
func (p *Page) MustHTML() string {
html, err := p.HTML()
p.e(err)
return html
}
// MustCookies is similar to Page.Cookies
func (p *Page) MustCookies(urls ...string) []*proto.NetworkCookie {
cookies, err := p.Cookies(urls)
p.e(err)
return cookies
}
// MustSetCookies is similar to Page.SetCookies.
// If the len(cookies) is 0 it will clear all the cookies.
func (p *Page) MustSetCookies(cookies ...*proto.NetworkCookieParam) *Page {
if len(cookies) == 0 {
cookies = nil
}
p.e(p.SetCookies(cookies))
return p
}
// MustSetExtraHeaders is similar to Page.SetExtraHeaders
func (p *Page) MustSetExtraHeaders(dict ...string) (cleanup func()) {
cleanup, err := p.SetExtraHeaders(dict)
p.e(err)
return
}
// MustSetUserAgent is similar to Page.SetUserAgent
func (p *Page) MustSetUserAgent(req *proto.NetworkSetUserAgentOverride) *Page {
p.e(p.SetUserAgent(req))
return p
}
// MustNavigate is similar to Page.Navigate
func (p *Page) MustNavigate(url string) *Page {
p.e(p.Navigate(url))
return p
}
// MustReload is similar to Page.Reload
func (p *Page) MustReload() *Page {
p.e(p.Reload())
return p
}
// MustActivate is similar to Page.Activate
func (p *Page) MustActivate() *Page {
p.e(p.Activate())
return p
}
// MustNavigateBack is similar to Page.NavigateBack
func (p *Page) MustNavigateBack() *Page {
p.e(p.NavigateBack())
return p
}
// MustNavigateForward is similar to Page.NavigateForward
func (p *Page) MustNavigateForward() *Page {
p.e(p.NavigateForward())
return p
}
// MustGetWindow is similar to Page.GetWindow
func (p *Page) MustGetWindow() *proto.BrowserBounds {
bounds, err := p.GetWindow()
p.e(err)
return bounds
}
// MustSetWindow is similar to Page.SetWindow
func (p *Page) MustSetWindow(left, top, width, height int) *Page {
p.e(p.SetWindow(&proto.BrowserBounds{
Left: gson.Int(left),
Top: gson.Int(top),
Width: gson.Int(width),
Height: gson.Int(height),
WindowState: proto.BrowserWindowStateNormal,
}))
return p
}
// MustWindowMinimize is similar to Page.WindowMinimize
func (p *Page) MustWindowMinimize() *Page {
p.e(p.SetWindow(&proto.BrowserBounds{
WindowState: proto.BrowserWindowStateMinimized,
}))
return p
}
// MustWindowMaximize is similar to Page.WindowMaximize
func (p *Page) MustWindowMaximize() *Page {
p.e(p.SetWindow(&proto.BrowserBounds{
WindowState: proto.BrowserWindowStateMaximized,
}))
return p
}
// MustWindowFullscreen is similar to Page.WindowFullscreen
func (p *Page) MustWindowFullscreen() *Page {
p.e(p.SetWindow(&proto.BrowserBounds{
WindowState: proto.BrowserWindowStateFullscreen,
}))
return p
}
// MustWindowNormal is similar to Page.WindowNormal
func (p *Page) MustWindowNormal() *Page {
p.e(p.SetWindow(&proto.BrowserBounds{
WindowState: proto.BrowserWindowStateNormal,
}))
return p
}
// MustSetViewport is similar to Page.SetViewport
func (p *Page) MustSetViewport(width, height int, deviceScaleFactor float64, mobile bool) *Page {
p.e(p.SetViewport(&proto.EmulationSetDeviceMetricsOverride{
Width: width,
Height: height,
DeviceScaleFactor: deviceScaleFactor,
Mobile: mobile,
}))
return p
}
// MustEmulate is similar to Page.Emulate
func (p *Page) MustEmulate(device devices.Device) *Page {
p.e(p.Emulate(device))
return p
}
// MustStopLoading is similar to Page.StopLoading
func (p *Page) MustStopLoading() *Page {
p.e(p.StopLoading())
return p
}
// MustClose is similar to Page.Close
func (p *Page) MustClose() {
p.e(p.Close())
}
// MustHandleDialog is similar to Page.HandleDialog
func (p *Page) MustHandleDialog() (wait func() *proto.PageJavascriptDialogOpening, handle func(bool, string)) {
w, h := p.HandleDialog()
return w, func(accept bool, promptText string) {
p.e(h(&proto.PageHandleJavaScriptDialog{
Accept: accept,
PromptText: promptText,
}))
}
}
// MustScreenshot is similar to Screenshot.
// If the toFile is "", it Page.will save output to "tmp/screenshots" folder, time as the file name.
func (p *Page) MustScreenshot(toFile ...string) []byte {
bin, err := p.Screenshot(false, nil)
p.e(err)
p.e(saveFile(saveFileTypeScreenshot, bin, toFile))
return bin
}
// MustScreenshotFullPage is similar to ScreenshotFullPage.
// If the toFile is "", it Page.will save output to "tmp/screenshots" folder, time as the file name.
func (p *Page) MustScreenshotFullPage(toFile ...string) []byte {
bin, err := p.Screenshot(true, nil)
p.e(err)
p.e(saveFile(saveFileTypeScreenshot, bin, toFile))
return bin
}
// MustPDF is similar to PDF.
// If the toFile is "", it Page.will save output to "tmp/pdf" folder, time as the file name.
func (p *Page) MustPDF(toFile ...string) []byte {
r, err := p.PDF(&proto.PagePrintToPDF{})
p.e(err)
bin, err := ioutil.ReadAll(r)
p.e(err)
p.e(saveFile(saveFileTypePDF, bin, toFile))
return bin
}
// MustWaitOpen is similar to Page.WaitOpen
func (p *Page) MustWaitOpen() (wait func() (newPage *Page)) {
w := p.WaitOpen()
return func() *Page {
page, err := w()
p.e(err)
return page
}
}
// MustWaitNavigation is similar to Page.WaitNavigation
func (p *Page) MustWaitNavigation() func() {
return p.WaitNavigation(proto.PageLifecycleEventNameNetworkAlmostIdle)
}
// MustWaitRequestIdle is similar to Page.WaitRequestIdle
func (p *Page) MustWaitRequestIdle(excludes ...string) (wait func()) {
return p.WaitRequestIdle(300*time.Millisecond, nil, excludes)
}
// MustWaitIdle is similar to Page.WaitIdle
func (p *Page) MustWaitIdle() *Page {
p.e(p.WaitIdle(time.Minute))
return p
}
// MustWaitLoad is similar to Page.WaitLoad
func (p *Page) MustWaitLoad() *Page {
p.e(p.WaitLoad())
return p
}
// MustAddScriptTag is similar to Page.AddScriptTag
func (p *Page) MustAddScriptTag(url string) *Page {
p.e(p.AddScriptTag(url, ""))
return p
}
// MustAddStyleTag is similar to Page.AddStyleTag
func (p *Page) MustAddStyleTag(url string) *Page {
p.e(p.AddStyleTag(url, ""))
return p
}
// MustEvalOnNewDocument is similar to Page.EvalOnNewDocument
func (p *Page) MustEvalOnNewDocument(js string) {
_, err := p.EvalOnNewDocument(js)
p.e(err)
}
// MustExpose is similar to Page.Expose
func (p *Page) MustExpose(name string, fn func(gson.JSON) (interface{}, error)) (stop func()) {
s, err := p.Expose(name, fn)
p.e(err)
return func() { p.e(s()) }
}
// MustEval is similar to Page.Eval
func (p *Page) MustEval(js string, params ...interface{}) gson.JSON {
res, err := p.Eval(js, params...)
p.e(err)
return res.Value
}
// MustEvaluate is similar to Page.Evaluate
func (p *Page) MustEvaluate(opts *EvalOptions) *proto.RuntimeRemoteObject {
res, err := p.Evaluate(opts)
p.e(err)
return res
}
// MustWait is similar to Page.Wait
func (p *Page) MustWait(js string, params ...interface{}) *Page {
p.e(p.Wait(nil, js, params))
return p
}
// MustWaitElementsMoreThan is similar to Page.WaitElementsMoreThan
func (p *Page) MustWaitElementsMoreThan(selector string, num int) *Page {
p.e(p.WaitElementsMoreThan(selector, num))
return p
}
// MustObjectToJSON is similar to Page.ObjectToJSON
func (p *Page) MustObjectToJSON(obj *proto.RuntimeRemoteObject) gson.JSON {
j, err := p.ObjectToJSON(obj)
p.e(err)
return j
}
// MustObjectsToJSON is similar to Page.ObjectsToJSON
func (p *Page) MustObjectsToJSON(list []*proto.RuntimeRemoteObject) gson.JSON {
arr := []interface{}{}
for _, obj := range list {
j, err := p.ObjectToJSON(obj)
p.e(err)
arr = append(arr, j.Val())
}
return gson.New(arr)
}
// MustElementFromNode is similar to Page.ElementFromNode
func (p *Page) MustElementFromNode(node *proto.DOMNode) *Element {
el, err := p.ElementFromNode(node)
p.e(err)
return el
}
// MustElementFromPoint is similar to Page.ElementFromPoint
func (p *Page) MustElementFromPoint(left, top int) *Element {
el, err := p.ElementFromPoint(left, top)
p.e(err)
return el
}
// MustRelease is similar to Page.Release
func (p *Page) MustRelease(obj *proto.RuntimeRemoteObject) *Page {
p.e(p.Release(obj))
return p
}
// MustHas is similar to Page.Has
func (p *Page) MustHas(selector string) bool {
has, _, err := p.Has(selector)
p.e(err)
return has
}
// MustHasX is similar to Page.HasX
func (p *Page) MustHasX(selector string) bool {
has, _, err := p.HasX(selector)
p.e(err)
return has
}
// MustHasR is similar to Page.HasR
func (p *Page) MustHasR(selector, regex string) bool {
has, _, err := p.HasR(selector, regex)
p.e(err)
return has
}
// MustSearch is similar to Page.Search .
// It only returns the first element in the search result.
func (p *Page) MustSearch(query string) *Element {
res, err := p.Search(query)
p.e(err)
res.Release()
return res.First
}
// MustElement is similar to Page.Element
func (p *Page) MustElement(selector string) *Element {
el, err := p.Element(selector)
p.e(err)
return el
}
// MustElementR is similar to Page.ElementR
func (p *Page) MustElementR(selector, jsRegex string) *Element {
el, err := p.ElementR(selector, jsRegex)
p.e(err)
return el
}
// MustElementX is similar to Page.ElementX
func (p *Page) MustElementX(xPath string) *Element {
el, err := p.ElementX(xPath)
p.e(err)
return el
}
// MustElementByJS is similar to Page.ElementByJS
func (p *Page) MustElementByJS(js string, params ...interface{}) *Element {
el, err := p.ElementByJS(Eval(js, params...))
p.e(err)
return el
}
// MustElements is similar to Page.Elements
func (p *Page) MustElements(selector string) Elements {
list, err := p.Elements(selector)
p.e(err)
return list
}
// MustElementsX is similar to Page.ElementsX
func (p *Page) MustElementsX(xpath string) Elements {
list, err := p.ElementsX(xpath)
p.e(err)
return list
}
// MustElementsByJS is similar to Page.ElementsByJS
func (p *Page) MustElementsByJS(js string, params ...interface{}) Elements {
list, err := p.ElementsByJS(Eval(js, params...))
p.e(err)
return list
}
// MustElementByJS is similar to RaceContext.ElementByJS
func (rc *RaceContext) MustElementByJS(js string, params []interface{}) *RaceContext {
return rc.ElementByJS(Eval(js, params...))
}
// MustHandle is similar to RaceContext.Handle
func (rc *RaceContext) MustHandle(callback func(*Element)) *RaceContext {
return rc.Handle(func(e *Element) error {
callback(e)
return nil
})
}
// MustDo is similar to RaceContext.Do
func (rc *RaceContext) MustDo() *Element {
el, err := rc.Do()
rc.page.e(err)
return el
}
// MustMove is similar to Mouse.Move
func (m *Mouse) MustMove(x, y float64) *Mouse {
m.page.e(m.Move(x, y, 0))
return m
}
// MustScroll is similar to Mouse.Scroll
func (m *Mouse) MustScroll(x, y float64) *Mouse {
m.page.e(m.Scroll(x, y, 0))
return m
}
// MustDown is similar to Mouse.Down
func (m *Mouse) MustDown(button proto.InputMouseButton) *Mouse {
m.page.e(m.Down(button, 1))
return m
}
// MustUp is similar to Mouse.Up
func (m *Mouse) MustUp(button proto.InputMouseButton) *Mouse {
m.page.e(m.Up(button, 1))
return m
}
// MustClick is similar to Mouse.Click
func (m *Mouse) MustClick(button proto.InputMouseButton) *Mouse {
m.page.e(m.Click(button))
return m
}
// MustDown is similar to Keyboard.Down
func (k *Keyboard) MustDown(key rune) *Keyboard {
k.page.e(k.Down(key))
return k
}
// MustUp is similar to Keyboard.Up
func (k *Keyboard) MustUp(key rune) *Keyboard {
k.page.e(k.Up(key))
return k
}
// MustPress is similar to Keyboard.Press
func (k *Keyboard) MustPress(key rune) *Keyboard {
k.page.e(k.Press(key))
return k
}
// MustInsertText is similar to Keyboard.InsertText
func (k *Keyboard) MustInsertText(text string) *Keyboard {
k.page.e(k.InsertText(text))
return k
}
// MustStart is similar to Touch.Start
func (t *Touch) MustStart(points ...*proto.InputTouchPoint) *Touch {
t.page.e(t.Start(points...))
return t
}
// MustMove is similar to Touch.Move
func (t *Touch) MustMove(points ...*proto.InputTouchPoint) *Touch {
t.page.e(t.Move(points...))
return t
}
// MustEnd is similar to Touch.End
func (t *Touch) MustEnd() *Touch {
t.page.e(t.End())
return t
}
// MustCancel is similar to Touch.Cancel
func (t *Touch) MustCancel() *Touch {
t.page.e(t.Cancel())
return t
}
// MustTap is similar to Touch.Tap
func (t *Touch) MustTap(x, y float64) *Touch {
t.page.e(t.Tap(x, y))
return t
}
// WithPanic returns an element clone with the specified panic function.
// The fail must stop the current goroutine's execution immediately, such as use runtime.Goexit() or panic inside it.
func (el *Element) WithPanic(fail func(interface{})) *Element {
n := *el
n.e = genE(fail)
return &n
}
// MustDescribe is similar to Element.Describe
func (el *Element) MustDescribe() *proto.DOMNode {
node, err := el.Describe(1, false)
el.e(err)
return node
}
// MustShadowRoot is similar to Element.ShadowRoot
func (el *Element) MustShadowRoot() *Element {
node, err := el.ShadowRoot()
el.e(err)
return node
}
// MustFrame is similar to Element.Frame
func (el *Element) MustFrame() *Page {
p, err := el.Frame()
el.e(err)
return p
}
// MustFocus is similar to Element.Focus
func (el *Element) MustFocus() *Element {
el.e(el.Focus())
return el
}
// MustScrollIntoView is similar to Element.ScrollIntoView
func (el *Element) MustScrollIntoView() *Element {
el.e(el.ScrollIntoView())
return el
}
// MustHover is similar to Element.Hover
func (el *Element) MustHover() *Element {
el.e(el.Hover())
return el
}
// MustClick is similar to Element.Click
func (el *Element) MustClick() *Element {
el.e(el.Click(proto.InputMouseButtonLeft))
return el
}
// MustTap is similar to Element.Tap
func (el *Element) MustTap() *Element {
el.e(el.Tap())
return el
}
// MustInteractable is similar to Element.Interactable
func (el *Element) MustInteractable() bool {
_, err := el.Interactable()
if errors.Is(err, &ErrNotInteractable{}) {
return false
}
el.e(err)
return true
}
// MustWaitInteractable is similar to Element.WaitInteractable
func (el *Element) MustWaitInteractable() *Element {
el.e(el.WaitInteractable())
return el
}
// MustPress is similar to Element.Press
func (el *Element) MustPress(keys ...rune) *Element {
el.e(el.Press(keys...))
return el
}
// MustSelectText is similar to Element.SelectText
func (el *Element) MustSelectText(regex string) *Element {
el.e(el.SelectText(regex))
return el
}
// MustSelectAllText is similar to Element.SelectAllText
func (el *Element) MustSelectAllText() *Element {
el.e(el.SelectAllText())
return el
}
// MustInput is similar to Element.Input
func (el *Element) MustInput(text string) *Element {
el.e(el.Input(text))
return el
}
// MustInputTime is similar to Element.Input
func (el *Element) MustInputTime(t time.Time) *Element {
el.e(el.InputTime(t))
return el
}
// MustBlur is similar to Element.Blur
func (el *Element) MustBlur() *Element {
el.e(el.Blur())
return el
}
// MustSelect is similar to Element.Select
func (el *Element) MustSelect(selectors ...string) *Element {
el.e(el.Select(selectors, true, SelectorTypeText))
return el
}
// MustMatches is similar to Element.Matches
func (el *Element) MustMatches(selector string) bool {
res, err := el.Matches(selector)
el.e(err)
return res
}
// MustAttribute is similar to Element.Attribute
func (el *Element) MustAttribute(name string) *string {
attr, err := el.Attribute(name)
el.e(err)
return attr
}
// MustProperty is similar to Element.Property
func (el *Element) MustProperty(name string) gson.JSON {
prop, err := el.Property(name)
el.e(err)
return prop
}
// MustContainsElement is similar to Element.ContainsElement
func (el *Element) MustContainsElement(target *Element) bool {
contains, err := el.ContainsElement(target)
el.e(err)
return contains
}
// MustSetFiles is similar to Element.SetFiles
func (el *Element) MustSetFiles(paths ...string) *Element {
el.e(el.SetFiles(paths))
return el
}
// MustSetDocumentContent is similar to Page.SetDocumentContent
func (p *Page) MustSetDocumentContent(html string) *Page {
p.e(p.SetDocumentContent(html))
return p
}
// MustText is similar to Element.Text
func (el *Element) MustText() string {
s, err := el.Text()
el.e(err)
return s
}
// MustHTML is similar to Element.HTML
func (el *Element) MustHTML() string {
s, err := el.HTML()
el.e(err)
return s
}
// MustVisible is similar to Element.Visible
func (el *Element) MustVisible() bool {
v, err := el.Visible()
el.e(err)
return v
}
// MustWaitLoad is similar to Element.WaitLoad
func (el *Element) MustWaitLoad() *Element {
el.e(el.WaitLoad())
return el
}
// MustWaitStable is similar to Element.WaitStable
func (el *Element) MustWaitStable() *Element {
el.e(el.WaitStable(300 * time.Millisecond))
return el
}
// MustWait is similar to Element.Wait
func (el *Element) MustWait(js string, params ...interface{}) *Element {
el.e(el.Wait(Eval(js, params)))
return el
}
// MustWaitVisible is similar to Element.WaitVisible
func (el *Element) MustWaitVisible() *Element {
el.e(el.WaitVisible())
return el
}
// MustWaitInvisible is similar to Element.WaitInvisible
func (el *Element) MustWaitInvisible() *Element {
el.e(el.WaitInvisible())
return el
}
// MustWaitEnabled is similar to Element.WaitEnabled
func (el *Element) MustWaitEnabled() *Element {
el.e(el.WaitEnabled())
return el
}
// MustWaitWritable is similar to Element.WaitWritable
func (el *Element) MustWaitWritable() *Element {
el.e(el.WaitWritable())
return el
}
// MustShape is similar to Element.Shape
func (el *Element) MustShape() *proto.DOMGetContentQuadsResult {
shape, err := el.Shape()
el.e(err)
return shape
}
// MustCanvasToImage is similar to Element.CanvasToImage
func (el *Element) MustCanvasToImage() []byte {
bin, err := el.CanvasToImage("", -1)
el.e(err)
return bin
}
// MustResource is similar to Element.Resource
func (el *Element) MustResource() []byte {
bin, err := el.Resource()
el.e(err)
return bin
}
// MustBackgroundImage is similar to Element.BackgroundImage
func (el *Element) MustBackgroundImage() []byte {
bin, err := el.BackgroundImage()
el.e(err)
return bin
}
// MustScreenshot is similar to Element.Screenshot
func (el *Element) MustScreenshot(toFile ...string) []byte {
bin, err := el.Screenshot(proto.PageCaptureScreenshotFormatPng, 0)
el.e(err)
el.e(saveFile(saveFileTypeScreenshot, bin, toFile))
return bin
}
// MustRelease is similar to Element.Release
func (el *Element) MustRelease() {
el.e(el.Release())
}
// MustRemove the element from the page
func (el *Element) MustRemove() {
el.e(el.Remove())
}
// MustEval is similar to Element.Eval
func (el *Element) MustEval(js string, params ...interface{}) gson.JSON {
res, err := el.Eval(js, params...)
el.e(err)
return res.Value
}
// MustHas is similar to Element.Has
func (el *Element) MustHas(selector string) bool {
has, _, err := el.Has(selector)
el.e(err)
return has
}
// MustHasX is similar to Element.HasX
func (el *Element) MustHasX(selector string) bool {
has, _, err := el.HasX(selector)
el.e(err)
return has
}
// MustHasR is similar to Element.HasR
func (el *Element) MustHasR(selector, regex string) bool {
has, _, err := el.HasR(selector, regex)
el.e(err)
return has
}
// MustElement is similar to Element.Element
func (el *Element) MustElement(selector string) *Element {
el, err := el.Element(selector)
el.e(err)
return el
}
// MustElementX is similar to Element.ElementX
func (el *Element) MustElementX(xpath string) *Element {
el, err := el.ElementX(xpath)
el.e(err)
return el
}
// MustElementByJS is similar to Element.ElementByJS
func (el *Element) MustElementByJS(js string, params ...interface{}) *Element {
el, err := el.ElementByJS(Eval(js, params...))
el.e(err)
return el
}
// MustParent is similar to Element.Parent
func (el *Element) MustParent() *Element {
parent, err := el.Parent()
el.e(err)
return parent
}
// MustParents is similar to Element.Parents
func (el *Element) MustParents(selector string) Elements {
list, err := el.Parents(selector)
el.e(err)
return list
}
// MustNext is similar to Element.Next
func (el *Element) MustNext() *Element {