vendor/github.com/go-rod/rod/must.go
1172 lines · 29582 bytes
1// This file contains the methods that panics when error return value is not nil.
2// Their function names are all prefixed with Must.
3// A function here is usually a wrapper for the error version with fixed default options to make it easier to use.
4//
5// For example the source code of [Element.Click] and [Element.MustClick]. MustClick has no argument.
6// But `Click` has a `button` argument to decide which button to click.
7// `MustClick` feels like a version of `Click` with some default behaviors.
8
9package rod
10
11import (
12 "errors"
13 "io"
14 "net/http"
15 "os"
16 "path/filepath"
17 "strings"
18 "time"
19
20 "github.com/go-rod/rod/lib/devices"
21 "github.com/go-rod/rod/lib/input"
22 "github.com/go-rod/rod/lib/proto"
23 "github.com/go-rod/rod/lib/utils"
24 "github.com/ysmood/gson"
25)
26
27// It must be generated by genE.
28type eFunc func(args ...interface{})
29
30// Generate a eFunc with the specified fail function.
31// If the last arg of eFunc is error the fail will be called.
32func genE(fail func(interface{})) eFunc {
33 return func(args ...interface{}) {
34 err, ok := args[len(args)-1].(error)
35 if ok {
36 fail(err)
37 }
38 }
39}
40
41// WithPanic returns a browser clone with the specified panic function.
42// The fail must stop the current goroutine's execution immediately, such as use [runtime.Goexit] or panic inside it.
43func (b *Browser) WithPanic(fail func(interface{})) *Browser {
44 n := *b
45 n.e = genE(fail)
46 return &n
47}
48
49// MustConnect is similar to [Browser.Connect].
50func (b *Browser) MustConnect() *Browser {
51 b.e(b.Connect())
52 return b
53}
54
55// MustClose is similar to [Browser.Close].
56func (b *Browser) MustClose() {
57 _ = b.Close()
58}
59
60// MustIncognito is similar to [Browser.Incognito].
61func (b *Browser) MustIncognito() *Browser {
62 p, err := b.Incognito()
63 b.e(err)
64 return p
65}
66
67// MustPage is similar to [Browser.Page].
68// The url list will be joined by "/".
69func (b *Browser) MustPage(url ...string) *Page {
70 p, err := b.Page(proto.TargetCreateTarget{URL: strings.Join(url, "/")})
71 b.e(err)
72 return p
73}
74
75// MustPages is similar to [Browser.Pages].
76func (b *Browser) MustPages() Pages {
77 list, err := b.Pages()
78 b.e(err)
79 return list
80}
81
82// MustPageFromTargetID is similar to [Browser.PageFromTargetID].
83func (b *Browser) MustPageFromTargetID(targetID proto.TargetTargetID) *Page {
84 p, err := b.PageFromTarget(targetID)
85 b.e(err)
86 return p
87}
88
89// MustHandleAuth is similar to [Browser.HandleAuth].
90func (b *Browser) MustHandleAuth(username, password string) (wait func()) {
91 w := b.HandleAuth(username, password)
92 return func() { b.e(w()) }
93}
94
95// MustIgnoreCertErrors is similar to [Browser.IgnoreCertErrors].
96func (b *Browser) MustIgnoreCertErrors(enable bool) *Browser {
97 b.e(b.IgnoreCertErrors(enable))
98 return b
99}
100
101// MustGetCookies is similar to [Browser.GetCookies].
102func (b *Browser) MustGetCookies() []*proto.NetworkCookie {
103 nc, err := b.GetCookies()
104 b.e(err)
105 return nc
106}
107
108// MustSetCookies is similar to [Browser.SetCookies].
109// If the len(cookies) is 0 it will clear all the cookies.
110func (b *Browser) MustSetCookies(cookies ...*proto.NetworkCookie) *Browser {
111 if len(cookies) == 0 {
112 b.e(b.SetCookies(nil))
113 } else {
114 b.e(b.SetCookies(proto.CookiesToParams(cookies)))
115 }
116 return b
117}
118
119// MustWaitDownload is similar to [Browser.WaitDownload].
120// It will read the file into bytes then remove the file.
121func (b *Browser) MustWaitDownload() func() []byte {
122 tmpDir := filepath.Join(os.TempDir(), "rod", "downloads")
123 wait := b.WaitDownload(tmpDir)
124
125 return func() []byte {
126 info := wait()
127 path := filepath.Join(tmpDir, info.GUID)
128 defer func() { _ = os.Remove(path) }()
129 data, err := os.ReadFile(path)
130 b.e(err)
131 return data
132 }
133}
134
135// MustVersion is similar to [Browser.Version].
136func (b *Browser) MustVersion() *proto.BrowserGetVersionResult {
137 v, err := b.Version()
138 b.e(err)
139 return v
140}
141
142// MustFind is similar to [Browser.Find].
143func (ps Pages) MustFind(selector string) *Page {
144 p, err := ps.Find(selector)
145 if err != nil {
146 if len(ps) > 0 {
147 ps[0].e(err)
148 } else {
149 // fallback to utils.E, because we don't have enough
150 // context to call the scope `.e`.
151 utils.E(err)
152 }
153 }
154 return p
155}
156
157// MustFindByURL is similar to [Page.FindByURL].
158func (ps Pages) MustFindByURL(regex string) *Page {
159 p, err := ps.FindByURL(regex)
160 if err != nil {
161 if len(ps) > 0 {
162 ps[0].e(err)
163 } else {
164 // fallback to utils.E, because we don't have enough
165 // context to call the scope `.e`.
166 utils.E(err)
167 }
168 }
169 return p
170}
171
172// WithPanic returns a page clone with the specified panic function.
173// The fail must stop the current goroutine's execution immediately, such as use [runtime.Goexit] or panic inside it.
174func (p *Page) WithPanic(fail func(interface{})) *Page {
175 n := *p
176 n.e = genE(fail)
177 return &n
178}
179
180// MustInfo is similar to [Page.Info].
181func (p *Page) MustInfo() *proto.TargetTargetInfo {
182 info, err := p.Info()
183 p.e(err)
184 return info
185}
186
187// MustHTML is similar to [Page.HTML].
188func (p *Page) MustHTML() string {
189 html, err := p.HTML()
190 p.e(err)
191 return html
192}
193
194// MustCookies is similar to [Page.Cookies].
195func (p *Page) MustCookies(urls ...string) []*proto.NetworkCookie {
196 cookies, err := p.Cookies(urls)
197 p.e(err)
198 return cookies
199}
200
201// MustSetCookies is similar to [Page.SetCookies].
202// If the len(cookies) is 0 it will clear all the cookies.
203func (p *Page) MustSetCookies(cookies ...*proto.NetworkCookieParam) *Page {
204 if len(cookies) == 0 {
205 cookies = nil
206 }
207 p.e(p.SetCookies(cookies))
208 return p
209}
210
211// MustSetExtraHeaders is similar to [Page.SetExtraHeaders].
212func (p *Page) MustSetExtraHeaders(dict ...string) (cleanup func()) {
213 cleanup, err := p.SetExtraHeaders(dict)
214 p.e(err)
215 return
216}
217
218// MustSetUserAgent is similar to [Page.SetUserAgent].
219func (p *Page) MustSetUserAgent(req *proto.NetworkSetUserAgentOverride) *Page {
220 p.e(p.SetUserAgent(req))
221 return p
222}
223
224// MustSetBlockedURLs is similar to [Page.SetBlockedURLs].
225func (p *Page) MustSetBlockedURLs(urls ...string) *Page {
226 p.e(p.SetBlockedURLs(urls))
227 return p
228}
229
230// MustNavigate is similar to [Page.Navigate].
231func (p *Page) MustNavigate(url string) *Page {
232 p.e(p.Navigate(url))
233 return p
234}
235
236// MustResetNavigationHistory is similar to [Page.ResetNavigationHistory].
237func (p *Page) MustResetNavigationHistory() *Page {
238 p.e(p.ResetNavigationHistory())
239 return p
240}
241
242// MustReload is similar to [Page.Reload].
243func (p *Page) MustReload() *Page {
244 p.e(p.Reload())
245 return p
246}
247
248// MustActivate is similar to [Page.Activate].
249func (p *Page) MustActivate() *Page {
250 p.e(p.Activate())
251 return p
252}
253
254// MustNavigateBack is similar to [Page.NavigateBack].
255func (p *Page) MustNavigateBack() *Page {
256 p.e(p.NavigateBack())
257 return p
258}
259
260// MustNavigateForward is similar to [Page.NavigateForward].
261func (p *Page) MustNavigateForward() *Page {
262 p.e(p.NavigateForward())
263 return p
264}
265
266// MustGetWindow is similar to [Page.GetWindow].
267func (p *Page) MustGetWindow() *proto.BrowserBounds {
268 bounds, err := p.GetWindow()
269 p.e(err)
270 return bounds
271}
272
273// MustSetWindow is similar to [Page.SetWindow].
274func (p *Page) MustSetWindow(left, top, width, height int) *Page {
275 p.e(p.SetWindow(&proto.BrowserBounds{
276 Left: gson.Int(left),
277 Top: gson.Int(top),
278 Width: gson.Int(width),
279 Height: gson.Int(height),
280 WindowState: proto.BrowserWindowStateNormal,
281 }))
282 return p
283}
284
285// MustWindowMinimize is similar to [Page.WindowMinimize].
286func (p *Page) MustWindowMinimize() *Page {
287 p.e(p.SetWindow(&proto.BrowserBounds{
288 WindowState: proto.BrowserWindowStateMinimized,
289 }))
290 return p
291}
292
293// MustWindowMaximize is similar to [Page.WindowMaximize].
294func (p *Page) MustWindowMaximize() *Page {
295 p.e(p.SetWindow(&proto.BrowserBounds{
296 WindowState: proto.BrowserWindowStateMaximized,
297 }))
298 return p
299}
300
301// MustWindowFullscreen is similar to [Page.WindowFullscreen].
302func (p *Page) MustWindowFullscreen() *Page {
303 p.e(p.SetWindow(&proto.BrowserBounds{
304 WindowState: proto.BrowserWindowStateFullscreen,
305 }))
306 return p
307}
308
309// MustWindowNormal is similar to [Page.WindowNormal].
310func (p *Page) MustWindowNormal() *Page {
311 p.e(p.SetWindow(&proto.BrowserBounds{
312 WindowState: proto.BrowserWindowStateNormal,
313 }))
314 return p
315}
316
317// MustSetViewport is similar to [Page.SetViewport].
318func (p *Page) MustSetViewport(width, height int, deviceScaleFactor float64, mobile bool) *Page {
319 p.e(p.SetViewport(&proto.EmulationSetDeviceMetricsOverride{
320 Width: width,
321 Height: height,
322 DeviceScaleFactor: deviceScaleFactor,
323 Mobile: mobile,
324 }))
325 return p
326}
327
328// MustEmulate is similar to [Page.Emulate].
329func (p *Page) MustEmulate(device devices.Device) *Page {
330 p.e(p.Emulate(device))
331 return p
332}
333
334// MustStopLoading is similar to [Page.StopLoading].
335func (p *Page) MustStopLoading() *Page {
336 p.e(p.StopLoading())
337 return p
338}
339
340// MustClose is similar to [Page.Close].
341func (p *Page) MustClose() {
342 p.e(p.Close())
343}
344
345// MustHandleDialog is similar to [Page.HandleDialog].
346func (p *Page) MustHandleDialog() (wait func() *proto.PageJavascriptDialogOpening, handle func(bool, string)) {
347 w, h := p.HandleDialog()
348 return w, func(accept bool, promptText string) {
349 p.e(h(&proto.PageHandleJavaScriptDialog{
350 Accept: accept,
351 PromptText: promptText,
352 }))
353 }
354}
355
356// MustHandleFileDialog is similar to [Page.HandleFileDialog].
357func (p *Page) MustHandleFileDialog() func(...string) {
358 setFiles, err := p.HandleFileDialog()
359 p.e(err)
360 return func(paths ...string) {
361 p.e(setFiles(paths))
362 }
363}
364
365// MustScreenshot is similar to [Page.Screenshot].
366// If the toFile is "", it Page.will save output to "tmp/screenshots" folder, time as the file name.
367func (p *Page) MustScreenshot(toFile ...string) []byte {
368 bin, err := p.Screenshot(false, nil)
369 p.e(err)
370 p.e(saveFile(saveFileTypeScreenshot, bin, toFile))
371 return bin
372}
373
374// MustCaptureDOMSnapshot is similar to [Page.CaptureDOMSnapshot].
375func (p *Page) MustCaptureDOMSnapshot() (domSnapshot *proto.DOMSnapshotCaptureSnapshotResult) {
376 domSnapshot, err := p.CaptureDOMSnapshot()
377 p.e(err)
378 return domSnapshot
379}
380
381// MustTriggerFavicon is similar to [PageTriggerFavicon].
382func (p *Page) MustTriggerFavicon() *Page {
383 p.e(p.TriggerFavicon())
384 return p
385}
386
387// MustScreenshotFullPage is similar to [Page.ScreenshotFullPage].
388// If the toFile is "", it Page.will save output to "tmp/screenshots" folder, time as the file name.
389func (p *Page) MustScreenshotFullPage(toFile ...string) []byte {
390 bin, err := p.Screenshot(true, nil)
391 p.e(err)
392 p.e(saveFile(saveFileTypeScreenshot, bin, toFile))
393 return bin
394}
395
396// MustScrollScreenshot is similar to [Page.ScrollScreenshot].
397// If the toFile is "", it Page.will save output to "tmp/screenshots" folder, time as the file name.
398func (p *Page) MustScrollScreenshot(toFile ...string) []byte {
399 bin, err := p.ScrollScreenshot(nil)
400 p.e(err)
401 p.e(saveFile(saveFileTypeScreenshot, bin, toFile))
402 return bin
403}
404
405// MustPDF is similar to [Page.PDF].
406// If the toFile is "", it Page.will save output to "tmp/pdf" folder, time as the file name.
407func (p *Page) MustPDF(toFile ...string) []byte {
408 r, err := p.PDF(&proto.PagePrintToPDF{})
409 p.e(err)
410 bin, err := io.ReadAll(r)
411 p.e(err)
412
413 p.e(saveFile(saveFileTypePDF, bin, toFile))
414 return bin
415}
416
417// MustWaitOpen is similar to [Page.WaitOpen].
418func (p *Page) MustWaitOpen() (wait func() (newPage *Page)) {
419 w := p.WaitOpen()
420 return func() *Page {
421 page, err := w()
422 p.e(err)
423 return page
424 }
425}
426
427// MustWaitNavigation is similar to [Page.WaitNavigation].
428func (p *Page) MustWaitNavigation() func() {
429 return p.WaitNavigation(proto.PageLifecycleEventNameNetworkAlmostIdle)
430}
431
432// MustWaitRequestIdle is similar to [Page.WaitRequestIdle].
433func (p *Page) MustWaitRequestIdle(excludes ...string) (wait func()) {
434 return p.WaitRequestIdle(300*time.Millisecond, nil, excludes, nil)
435}
436
437// MustWaitIdle is similar to [Page.WaitIdle].
438func (p *Page) MustWaitIdle() *Page {
439 p.e(p.WaitIdle(time.Minute))
440 return p
441}
442
443// MustWaitDOMStable is similar to [Page.WaitDOMStable].
444func (p *Page) MustWaitDOMStable() *Page {
445 p.e(p.WaitDOMStable(time.Second, 0))
446 return p
447}
448
449// MustWaitStable is similar to [Page.WaitStable].
450func (p *Page) MustWaitStable() *Page {
451 p.e(p.WaitStable(time.Second))
452 return p
453}
454
455// MustWaitLoad is similar to [Page.WaitLoad].
456func (p *Page) MustWaitLoad() *Page {
457 p.e(p.WaitLoad())
458 return p
459}
460
461// MustAddScriptTag is similar to [Page.AddScriptTag].
462func (p *Page) MustAddScriptTag(url string) *Page {
463 p.e(p.AddScriptTag(url, ""))
464 return p
465}
466
467// MustAddStyleTag is similar to [Page.AddStyleTag].
468func (p *Page) MustAddStyleTag(url string) *Page {
469 p.e(p.AddStyleTag(url, ""))
470 return p
471}
472
473// MustEvalOnNewDocument is similar to [Page.EvalOnNewDocument].
474func (p *Page) MustEvalOnNewDocument(js string) {
475 _, err := p.EvalOnNewDocument(js)
476 p.e(err)
477}
478
479// MustExpose is similar to [Page.Expose].
480func (p *Page) MustExpose(name string, fn func(gson.JSON) (interface{}, error)) (stop func()) {
481 s, err := p.Expose(name, fn)
482 p.e(err)
483 return func() { p.e(s()) }
484}
485
486// MustEval is similar to [Page.Eval].
487func (p *Page) MustEval(js string, params ...interface{}) gson.JSON {
488 res, err := p.Eval(js, params...)
489 p.e(err)
490 return res.Value
491}
492
493// MustEvaluate is similar to [Page.Evaluate].
494func (p *Page) MustEvaluate(opts *EvalOptions) *proto.RuntimeRemoteObject {
495 res, err := p.Evaluate(opts)
496 p.e(err)
497 return res
498}
499
500// MustWait is similar to [Page.Wait].
501func (p *Page) MustWait(js string, params ...interface{}) *Page {
502 p.e(p.Wait(Eval(js, params...)))
503 return p
504}
505
506// MustWaitElementsMoreThan is similar to [Page.WaitElementsMoreThan].
507func (p *Page) MustWaitElementsMoreThan(selector string, num int) *Page {
508 p.e(p.WaitElementsMoreThan(selector, num))
509 return p
510}
511
512// MustObjectToJSON is similar to [Page.ObjectToJSON].
513func (p *Page) MustObjectToJSON(obj *proto.RuntimeRemoteObject) gson.JSON {
514 j, err := p.ObjectToJSON(obj)
515 p.e(err)
516 return j
517}
518
519// MustObjectsToJSON is similar to [Page.ObjectsToJSON].
520func (p *Page) MustObjectsToJSON(list []*proto.RuntimeRemoteObject) gson.JSON {
521 arr := []interface{}{}
522 for _, obj := range list {
523 j, err := p.ObjectToJSON(obj)
524 p.e(err)
525 arr = append(arr, j.Val())
526 }
527 return gson.New(arr)
528}
529
530// MustElementFromNode is similar to [Page.ElementFromNode].
531func (p *Page) MustElementFromNode(node *proto.DOMNode) *Element {
532 el, err := p.ElementFromNode(node)
533 p.e(err)
534 return el
535}
536
537// MustElementFromPoint is similar to [Page.ElementFromPoint].
538func (p *Page) MustElementFromPoint(left, top int) *Element {
539 el, err := p.ElementFromPoint(left, top)
540 p.e(err)
541 return el
542}
543
544// MustRelease is similar to [Page.Release].
545func (p *Page) MustRelease(obj *proto.RuntimeRemoteObject) *Page {
546 p.e(p.Release(obj))
547 return p
548}
549
550// MustHas is similar to [Page.Has].
551func (p *Page) MustHas(selector string) bool {
552 has, _, err := p.Has(selector)
553 p.e(err)
554 return has
555}
556
557// MustHasX is similar to [Page.HasX].
558func (p *Page) MustHasX(selector string) bool {
559 has, _, err := p.HasX(selector)
560 p.e(err)
561 return has
562}
563
564// MustHasR is similar to [Page.HasR].
565func (p *Page) MustHasR(selector, regex string) bool {
566 has, _, err := p.HasR(selector, regex)
567 p.e(err)
568 return has
569}
570
571// MustSearch is similar to [Page.Search].
572// It only returns the first element in the search result.
573func (p *Page) MustSearch(query string) *Element {
574 res, err := p.Search(query)
575 p.e(err)
576 res.Release()
577 return res.First
578}
579
580// MustElement is similar to [Page.Element].
581func (p *Page) MustElement(selector string) *Element {
582 el, err := p.Element(selector)
583 p.e(err)
584 return el
585}
586
587// MustElementR is similar to [Page.ElementR].
588func (p *Page) MustElementR(selector, jsRegex string) *Element {
589 el, err := p.ElementR(selector, jsRegex)
590 p.e(err)
591 return el
592}
593
594// MustElementX is similar to [Page.ElementX].
595func (p *Page) MustElementX(xPath string) *Element {
596 el, err := p.ElementX(xPath)
597 p.e(err)
598 return el
599}
600
601// MustElementByJS is similar to [Page.ElementByJS].
602func (p *Page) MustElementByJS(js string, params ...interface{}) *Element {
603 el, err := p.ElementByJS(Eval(js, params...))
604 p.e(err)
605 return el
606}
607
608// MustElements is similar to [Page.Elements].
609func (p *Page) MustElements(selector string) Elements {
610 list, err := p.Elements(selector)
611 p.e(err)
612 return list
613}
614
615// MustElementsX is similar to [Page.ElementsX].
616func (p *Page) MustElementsX(xpath string) Elements {
617 list, err := p.ElementsX(xpath)
618 p.e(err)
619 return list
620}
621
622// MustElementsByJS is similar to [Page.ElementsByJS].
623func (p *Page) MustElementsByJS(js string, params ...interface{}) Elements {
624 list, err := p.ElementsByJS(Eval(js, params...))
625 p.e(err)
626 return list
627}
628
629// MustElementByJS is similar to [RaceContext.ElementByJS].
630func (rc *RaceContext) MustElementByJS(js string, params []interface{}) *RaceContext {
631 return rc.ElementByJS(Eval(js, params...))
632}
633
634// MustHandle is similar to [RaceContext.Handle].
635func (rc *RaceContext) MustHandle(callback func(*Element)) *RaceContext {
636 return rc.Handle(func(e *Element) error {
637 callback(e)
638 return nil
639 })
640}
641
642// MustDo is similar to [RaceContext.Do].
643func (rc *RaceContext) MustDo() *Element {
644 el, err := rc.Do()
645 rc.page.e(err)
646 return el
647}
648
649// MustMoveTo is similar to [Mouse.Move].
650func (m *Mouse) MustMoveTo(x, y float64) *Mouse {
651 m.page.e(m.MoveTo(proto.NewPoint(x, y)))
652 return m
653}
654
655// MustScroll is similar to [Mouse.Scroll].
656func (m *Mouse) MustScroll(x, y float64) *Mouse {
657 m.page.e(m.Scroll(x, y, 0))
658 return m
659}
660
661// MustDown is similar to [Mouse.Down].
662func (m *Mouse) MustDown(button proto.InputMouseButton) *Mouse {
663 m.page.e(m.Down(button, 1))
664 return m
665}
666
667// MustUp is similar to [Mouse.Up].
668func (m *Mouse) MustUp(button proto.InputMouseButton) *Mouse {
669 m.page.e(m.Up(button, 1))
670 return m
671}
672
673// MustClick is similar to [Mouse.Click].
674func (m *Mouse) MustClick(button proto.InputMouseButton) *Mouse {
675 m.page.e(m.Click(button, 1))
676 return m
677}
678
679// MustType is similar to [Keyboard.Type].
680func (k *Keyboard) MustType(key ...input.Key) *Keyboard {
681 k.page.e(k.Type(key...))
682 return k
683}
684
685// MustDo is similar to [KeyActions.Do].
686func (ka *KeyActions) MustDo() {
687 ka.keyboard.page.e(ka.Do())
688}
689
690// MustInsertText is similar to [Page.InsertText].
691func (p *Page) MustInsertText(text string) *Page {
692 p.e(p.InsertText(text))
693 return p
694}
695
696// MustStart is similar to [Touch.Start].
697func (t *Touch) MustStart(points ...*proto.InputTouchPoint) *Touch {
698 t.page.e(t.Start(points...))
699 return t
700}
701
702// MustMove is similar to [Touch.Move].
703func (t *Touch) MustMove(points ...*proto.InputTouchPoint) *Touch {
704 t.page.e(t.Move(points...))
705 return t
706}
707
708// MustEnd is similar to [Touch.End].
709func (t *Touch) MustEnd() *Touch {
710 t.page.e(t.End())
711 return t
712}
713
714// MustCancel is similar to [Touch.Cancel].
715func (t *Touch) MustCancel() *Touch {
716 t.page.e(t.Cancel())
717 return t
718}
719
720// MustTap is similar to [Touch.Tap].
721func (t *Touch) MustTap(x, y float64) *Touch {
722 t.page.e(t.Tap(x, y))
723 return t
724}
725
726// WithPanic returns an element clone with the specified panic function.
727// The fail must stop the current goroutine's execution immediately, such as use [runtime.Goexit] or panic inside it.
728func (el *Element) WithPanic(fail func(interface{})) *Element {
729 n := *el
730 n.e = genE(fail)
731 return &n
732}
733
734// MustDescribe is similar to [Element.Describe].
735func (el *Element) MustDescribe() *proto.DOMNode {
736 node, err := el.Describe(1, false)
737 el.e(err)
738 return node
739}
740
741// MustShadowRoot is similar to [Element.ShadowRoot].
742func (el *Element) MustShadowRoot() *Element {
743 node, err := el.ShadowRoot()
744 el.e(err)
745 return node
746}
747
748// MustFrame is similar to [Element.Frame].
749func (el *Element) MustFrame() *Page {
750 p, err := el.Frame()
751 el.e(err)
752 return p
753}
754
755// MustFocus is similar to [Element.Focus].
756func (el *Element) MustFocus() *Element {
757 el.e(el.Focus())
758 return el
759}
760
761// MustScrollIntoView is similar to [Element.ScrollIntoView].
762func (el *Element) MustScrollIntoView() *Element {
763 el.e(el.ScrollIntoView())
764 return el
765}
766
767// MustHover is similar to [Element.Hover].
768func (el *Element) MustHover() *Element {
769 el.e(el.Hover())
770 return el
771}
772
773// MustClick is similar to [Element.Click].
774func (el *Element) MustClick() *Element {
775 el.e(el.Click(proto.InputMouseButtonLeft, 1))
776 return el
777}
778
779// MustDoubleClick is similar to [Element.Click].
780func (el *Element) MustDoubleClick() *Element {
781 el.e(el.Click(proto.InputMouseButtonLeft, 2))
782 return el
783}
784
785// MustTap is similar to [Element.Tap].
786func (el *Element) MustTap() *Element {
787 el.e(el.Tap())
788 return el
789}
790
791// MustInteractable is similar to [Element.Interactable].
792func (el *Element) MustInteractable() bool {
793 _, err := el.Interactable()
794 if errors.Is(err, &NotInteractableError{}) {
795 return false
796 }
797 el.e(err)
798 return true
799}
800
801// MustWaitInteractable is similar to [Element.WaitInteractable].
802func (el *Element) MustWaitInteractable() *Element {
803 el.e(el.WaitInteractable())
804 return el
805}
806
807// MustType is similar to [Element.Type].
808func (el *Element) MustType(keys ...input.Key) *Element {
809 el.e(el.Type(keys...))
810 return el
811}
812
813// MustKeyActions is similar to [Element.KeyActions].
814func (el *Element) MustKeyActions() *KeyActions {
815 ka, err := el.KeyActions()
816 el.e(err)
817 return ka
818}
819
820// MustSelectText is similar to [Element.SelectText].
821func (el *Element) MustSelectText(regex string) *Element {
822 el.e(el.SelectText(regex))
823 return el
824}
825
826// MustSelectAllText is similar to [Element.SelectAllText].
827func (el *Element) MustSelectAllText() *Element {
828 el.e(el.SelectAllText())
829 return el
830}
831
832// MustInput is similar to [Element.Input].
833func (el *Element) MustInput(text string) *Element {
834 el.e(el.Input(text))
835 return el
836}
837
838// MustInputTime is similar to [Element.Input].
839func (el *Element) MustInputTime(t time.Time) *Element {
840 el.e(el.InputTime(t))
841 return el
842}
843
844// MustInputColor is similar to [Element.InputColor].
845func (el *Element) MustInputColor(color string) *Element {
846 el.e(el.InputColor(color))
847 return el
848}
849
850// MustBlur is similar to [Element.Blur].
851func (el *Element) MustBlur() *Element {
852 el.e(el.Blur())
853 return el
854}
855
856// MustSelect is similar to [Element.Select].
857func (el *Element) MustSelect(selectors ...string) *Element {
858 el.e(el.Select(selectors, true, SelectorTypeText))
859 return el
860}
861
862// MustMatches is similar to [Element.Matches].
863func (el *Element) MustMatches(selector string) bool {
864 res, err := el.Matches(selector)
865 el.e(err)
866 return res
867}
868
869// MustAttribute is similar to [Element.Attribute].
870func (el *Element) MustAttribute(name string) *string {
871 attr, err := el.Attribute(name)
872 el.e(err)
873 return attr
874}
875
876// MustProperty is similar to [Element.Property].
877func (el *Element) MustProperty(name string) gson.JSON {
878 prop, err := el.Property(name)
879 el.e(err)
880 return prop
881}
882
883// MustDisabled is similar to [Element.Disabled].
884func (el *Element) MustDisabled() bool {
885 disabled, err := el.Disabled()
886 el.e(err)
887 return disabled
888}
889
890// MustContainsElement is similar to [Element.ContainsElement].
891func (el *Element) MustContainsElement(target *Element) bool {
892 contains, err := el.ContainsElement(target)
893 el.e(err)
894 return contains
895}
896
897// MustSetFiles is similar to [Element.SetFiles].
898func (el *Element) MustSetFiles(paths ...string) *Element {
899 el.e(el.SetFiles(paths))
900 return el
901}
902
903// MustSetDocumentContent is similar to [Page.SetDocumentContent].
904func (p *Page) MustSetDocumentContent(html string) *Page {
905 p.e(p.SetDocumentContent(html))
906 return p
907}
908
909// MustText is similar to [Element.Text].
910func (el *Element) MustText() string {
911 s, err := el.Text()
912 el.e(err)
913 return s
914}
915
916// MustHTML is similar to [Element.HTML].
917func (el *Element) MustHTML() string {
918 s, err := el.HTML()
919 el.e(err)
920 return s
921}
922
923// MustVisible is similar to [Element.Visible].
924func (el *Element) MustVisible() bool {
925 v, err := el.Visible()
926 el.e(err)
927 return v
928}
929
930// MustWaitLoad is similar to [Element.WaitLoad].
931func (el *Element) MustWaitLoad() *Element {
932 el.e(el.WaitLoad())
933 return el
934}
935
936// MustWaitStable is similar to [Element.WaitStable].
937func (el *Element) MustWaitStable() *Element {
938 el.e(el.WaitStable(300 * time.Millisecond))
939 return el
940}
941
942// MustWait is similar to [Element.Wait].
943func (el *Element) MustWait(js string, params ...interface{}) *Element {
944 el.e(el.Wait(Eval(js, params...)))
945 return el
946}
947
948// MustWaitVisible is similar to [Element.WaitVisible].
949func (el *Element) MustWaitVisible() *Element {
950 el.e(el.WaitVisible())
951 return el
952}
953
954// MustWaitInvisible is similar to [Element.WaitInvisible]..
955func (el *Element) MustWaitInvisible() *Element {
956 el.e(el.WaitInvisible())
957 return el
958}
959
960// MustWaitEnabled is similar to [Element.WaitEnabled].
961func (el *Element) MustWaitEnabled() *Element {
962 el.e(el.WaitEnabled())
963 return el
964}
965
966// MustWaitWritable is similar to [Element.WaitWritable].
967func (el *Element) MustWaitWritable() *Element {
968 el.e(el.WaitWritable())
969 return el
970}
971
972// MustShape is similar to [Element.Shape].
973func (el *Element) MustShape() *proto.DOMGetContentQuadsResult {
974 shape, err := el.Shape()
975 el.e(err)
976 return shape
977}
978
979// MustCanvasToImage is similar to [Element.CanvasToImage].
980func (el *Element) MustCanvasToImage() []byte {
981 bin, err := el.CanvasToImage("", -1)
982 el.e(err)
983 return bin
984}
985
986// MustResource is similar to [Element.Resource].
987func (el *Element) MustResource() []byte {
988 bin, err := el.Resource()
989 el.e(err)
990 return bin
991}
992
993// MustBackgroundImage is similar to [Element.BackgroundImage].
994func (el *Element) MustBackgroundImage() []byte {
995 bin, err := el.BackgroundImage()
996 el.e(err)
997 return bin
998}
999
1000// MustScreenshot is similar to [Element.Screenshot].
1001func (el *Element) MustScreenshot(toFile ...string) []byte {
1002 bin, err := el.Screenshot(proto.PageCaptureScreenshotFormatPng, 0)
1003 el.e(err)
1004 el.e(saveFile(saveFileTypeScreenshot, bin, toFile))
1005 return bin
1006}
1007
1008// MustRelease is similar to [Element.Release].
1009func (el *Element) MustRelease() {
1010 el.e(el.Release())
1011}
1012
1013// MustRemove is similar to [Element.Remove].
1014func (el *Element) MustRemove() {
1015 el.e(el.Remove())
1016}
1017
1018// MustEval is similar to [Element.Eval].
1019func (el *Element) MustEval(js string, params ...interface{}) gson.JSON {
1020 res, err := el.Eval(js, params...)
1021 el.e(err)
1022 return res.Value
1023}
1024
1025// MustHas is similar to [Element.Has].
1026func (el *Element) MustHas(selector string) bool {
1027 has, _, err := el.Has(selector)
1028 el.e(err)
1029 return has
1030}
1031
1032// MustHasX is similar to [Element.HasX].
1033func (el *Element) MustHasX(selector string) bool {
1034 has, _, err := el.HasX(selector)
1035 el.e(err)
1036 return has
1037}
1038
1039// MustHasR is similar to [Element.HasR].
1040func (el *Element) MustHasR(selector, regex string) bool {
1041 has, _, err := el.HasR(selector, regex)
1042 el.e(err)
1043 return has
1044}
1045
1046// MustElement is similar to [Element.Element].
1047func (el *Element) MustElement(selector string) *Element {
1048 el, err := el.Element(selector)
1049 el.e(err)
1050 return el
1051}
1052
1053// MustElementX is similar to [Element.ElementX].
1054func (el *Element) MustElementX(xpath string) *Element {
1055 el, err := el.ElementX(xpath)
1056 el.e(err)
1057 return el
1058}
1059
1060// MustElementByJS is similar to [Element.ElementByJS].
1061func (el *Element) MustElementByJS(js string, params ...interface{}) *Element {
1062 el, err := el.ElementByJS(Eval(js, params...))
1063 el.e(err)
1064 return el
1065}
1066
1067// MustParent is similar to [Element.Parent].
1068func (el *Element) MustParent() *Element {
1069 parent, err := el.Parent()
1070 el.e(err)
1071 return parent
1072}
1073
1074// MustParents is similar to [Element.Parents].
1075func (el *Element) MustParents(selector string) Elements {
1076 list, err := el.Parents(selector)
1077 el.e(err)
1078 return list
1079}
1080
1081// MustNext is similar to [Element.Next].
1082func (el *Element) MustNext() *Element {
1083 parent, err := el.Next()
1084 el.e(err)
1085 return parent
1086}
1087
1088// MustPrevious is similar to [Element.Previous].
1089func (el *Element) MustPrevious() *Element {
1090 parent, err := el.Previous()
1091 el.e(err)
1092 return parent
1093}
1094
1095// MustElementR is similar to [Element.ElementR].
1096func (el *Element) MustElementR(selector, regex string) *Element {
1097 sub, err := el.ElementR(selector, regex)
1098 el.e(err)
1099 return sub
1100}
1101
1102// MustElements is similar to [Element.Elements].
1103func (el *Element) MustElements(selector string) Elements {
1104 list, err := el.Elements(selector)
1105 el.e(err)
1106 return list
1107}
1108
1109// MustElementsX is similar to [Element.ElementsX].
1110func (el *Element) MustElementsX(xpath string) Elements {
1111 list, err := el.ElementsX(xpath)
1112 el.e(err)
1113 return list
1114}
1115
1116// MustElementsByJS is similar to [Element.ElementsByJS].
1117func (el *Element) MustElementsByJS(js string, params ...interface{}) Elements {
1118 list, err := el.ElementsByJS(Eval(js, params...))
1119 el.e(err)
1120 return list
1121}
1122
1123// MustAdd is similar to [HijackRouter.Add].
1124func (r *HijackRouter) MustAdd(pattern string, handler func(*Hijack)) *HijackRouter {
1125 r.browser.e(r.Add(pattern, "", handler))
1126 return r
1127}
1128
1129// MustRemove is similar to [HijackRouter.Remove].
1130func (r *HijackRouter) MustRemove(pattern string) *HijackRouter {
1131 r.browser.e(r.Remove(pattern))
1132 return r
1133}
1134
1135// MustStop is similar to [HijackRouter.Stop].
1136func (r *HijackRouter) MustStop() {
1137 r.browser.e(r.Stop())
1138}
1139
1140// MustLoadResponse is similar to [Hijack.LoadResponse].
1141func (h *Hijack) MustLoadResponse() {
1142 h.browser.e(h.LoadResponse(http.DefaultClient, true))
1143}
1144
1145// MustEqual is similar to [Element.Equal].
1146func (el *Element) MustEqual(elm *Element) bool {
1147 res, err := el.Equal(elm)
1148 el.e(err)
1149 return res
1150}
1151
1152// MustMoveMouseOut is similar to [Element.MoveMouseOut].
1153func (el *Element) MustMoveMouseOut() *Element {
1154 el.e(el.MoveMouseOut())
1155 return el
1156}
1157
1158// MustGetXPath is similar to [Element.GetXPath].
1159func (el *Element) MustGetXPath(optimized bool) string {
1160 xpath, err := el.GetXPath(optimized)
1161 el.e(err)
1162 return xpath
1163}
1164
1165// MustGet an elem from the pool. Use the [Pool[T].Put] to make it reusable later.
1166func (p Pool[T]) MustGet(create func() *T) *T {
1167 elem := <-p
1168 if elem == nil {
1169 elem = create()
1170 }
1171 return elem
1172}