Best Rod code snippet using rod.MustClose
examples_test.go
Source:examples_test.go
...21func Example() {22 // Launch a new browser with default options, and connect to it.23 browser := rod.New().MustConnect()24 // Even you forget to close, rod will close it after main process ends.25 defer browser.MustClose()26 // Create a new page27 page := browser.MustPage("https://github.com")28 // We use css selector to get the search input element and input "git"29 page.MustElement("input").MustInput("git").MustType(input.Enter)30 // Wait until css selector get the element then get the text content of it.31 text := page.MustElement(".codesearch-results p").MustText()32 fmt.Println(text)33 // Get all input elements. Rod supports query elements by css selector, xpath, and regex.34 // For more detailed usage, check the query_test.go file.35 fmt.Println("Found", len(page.MustElements("input")), "input elements")36 // Eval js on the page37 page.MustEval(`() => console.log("hello world")`)38 // Pass parameters as json objects to the js function. This MustEval will result 339 fmt.Println("1 + 2 =", page.MustEval(`(a, b) => a + b`, 1, 2).Int())40 // When eval on an element, "this" in the js is the current DOM element.41 fmt.Println(page.MustElement("title").MustEval(`() => this.innerText`).String())42 // Output:43 // Git is the most widely used version control system.44 // Found 5 input elements45 // 1 + 2 = 346 // Search · git · GitHub47}48// Shows how to disable headless mode and debug.49// Rod provides a lot of debug options, you can set them with setter methods or use environment variables.50// Doc for environment variables: https://pkg.go.dev/github.com/moredure/xrod/lib/defaults51func Example_disable_headless_to_debug() {52 // Headless runs the browser on foreground, you can also use flag "-rod=show"53 // Devtools opens the tab in each new tab opened automatically54 l := launcher.New().55 Headless(false).56 Devtools(true)57 defer l.Cleanup() // remove launcher.FlagUserDataDir58 url := l.MustLaunch()59 // Trace shows verbose debug information for each action executed60 // Slowmotion is a debug related function that waits 2 seconds between61 // each action, making it easier to inspect what your code is doing.62 browser := rod.New().63 ControlURL(url).64 Trace(true).65 SlowMotion(2 * time.Second).66 MustConnect()67 // ServeMonitor plays screenshots of each tab. This feature is extremely68 // useful when debugging with headless mode.69 // You can also enable it with flag "-rod=monitor"70 launcher.Open(browser.ServeMonitor(""))71 defer browser.MustClose()72 page := browser.MustPage("https://github.com/")73 page.MustElement("input").MustInput("git").MustType(input.Enter)74 text := page.MustElement(".codesearch-results p").MustText()75 fmt.Println(text)76 utils.Pause() // pause goroutine77}78// Rod use https://golang.org/pkg/context to handle cancelations for IO blocking operations, most times it's timeout.79// Context will be recursively passed to all sub-methods.80// For example, methods like Page.Context(ctx) will return a clone of the page with the ctx,81// all the methods of the returned page will use the ctx if they have IO blocking operations.82// Page.Timeout or Page.WithCancel is just a shortcut for Page.Context.83// Of course, Browser or Element works the same way.84func Example_context_and_timeout() {85 page := rod.New().MustConnect().MustPage("https://github.com")86 page.87 // Set a 5-second timeout for all chained methods88 Timeout(5 * time.Second).89 // The total time for MustWaitLoad and MustElement must be less than 5 seconds90 MustWaitLoad().91 MustElement("title").92 // Methods after CancelTimeout won't be affected by the 5-second timeout93 CancelTimeout().94 // Set a 10-second timeout for all chained methods95 Timeout(10 * time.Second).96 // Panics if it takes more than 10 seconds97 MustText()98 // The two code blocks below are basically the same:99 {100 page.Timeout(5 * time.Second).MustElement("a").CancelTimeout()101 }102 {103 // Use this way you can customize your own way to cancel long-running task104 page, cancel := page.WithCancel()105 go func() {106 time.Sleep(time.Duration(rand.Int())) // cancel after randomly time107 cancel()108 }()109 page.MustElement("a")110 }111}112// We use "Must" prefixed functions to write example code. But in production you may want to use113// the no-prefix version of them.114// About why we use "Must" as the prefix, it's similar to https://golang.org/pkg/regexp/#MustCompile115func Example_error_handling() {116 page := rod.New().MustConnect().MustPage("https://mdn.dev")117 // We use Go's standard way to check error types, no magic.118 check := func(err error) {119 var evalErr *rod.ErrEval120 if errors.Is(err, context.DeadlineExceeded) { // timeout error121 fmt.Println("timeout err")122 } else if errors.As(err, &evalErr) { // eval error123 fmt.Println(evalErr.LineNumber)124 } else if err != nil {125 fmt.Println("can't handle", err)126 }127 }128 // The two code blocks below are doing the same thing in two styles:129 // The block below is better for debugging or quick scripting. We use panic to short-circuit logics.130 // So that we can take advantage of fluent interface (https://en.wikipedia.org/wiki/Fluent_interface)131 // and fail-fast (https://en.wikipedia.org/wiki/Fail-fast).132 // This style will reduce code, but it may also catch extra errors (less consistent and precise).133 {134 err := rod.Try(func() {135 fmt.Println(page.MustElement("a").MustHTML()) // use "Must" prefixed functions136 })137 check(err)138 }139 // The block below is better for production code. It's the standard way to handle errors.140 // Usually, this style is more consistent and precise.141 {142 el, err := page.Element("a")143 if err != nil {144 check(err)145 return146 }147 html, err := el.HTML()148 if err != nil {149 check(err)150 return151 }152 fmt.Println(html)153 }154}155// Example_search shows how to use Search to get element inside nested iframes or shadow DOMs.156// It works the same as https://developers.google.com/web/tools/chrome-devtools/dom#search157func Example_search() {158 browser := rod.New().MustConnect()159 defer browser.MustClose()160 page := browser.MustPage("https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe")161 // Click the zoom-in button of the OpenStreetMap162 page.MustSearch(".leaflet-control-zoom-in").MustClick()163 fmt.Println("done")164 // Output: done165}166func Example_page_screenshot() {167 page := rod.New().MustConnect().MustPage("https://github.com").MustWaitLoad()168 // simple version169 page.MustScreenshot("my.png")170 // customization version171 img, _ := page.Screenshot(true, &proto.PageCaptureScreenshot{172 Format: proto.PageCaptureScreenshotFormatJpeg,173 Quality: gson.Int(90),174 Clip: &proto.PageViewport{175 X: 0,176 Y: 0,177 Width: 300,178 Height: 200,179 Scale: 1,180 },181 FromSurface: true,182 })183 _ = utils.OutputFile("my.jpg", img)184}185func Example_page_pdf() {186 page := rod.New().MustConnect().MustPage("https://github.com").MustWaitLoad()187 // simple version188 page.MustPDF("my.pdf")189 // customized version190 pdf, _ := page.PDF(&proto.PagePrintToPDF{191 PaperWidth: gson.Num(8.5),192 PaperHeight: gson.Num(11),193 PageRanges: "1-3",194 })195 _ = utils.OutputFile("my.pdf", pdf)196}197// Show how to handle multiple results of an action.198// Such as when you login a page, the result can be success or wrong password.199func Example_race_selectors() {200 const username = ""201 const password = ""202 browser := rod.New().MustConnect()203 page := browser.MustPage("https://leetcode.com/accounts/login/")204 page.MustElement("#id_login").MustInput(username)205 page.MustElement("#id_password").MustInput(password).MustType(input.Enter)206 // It will keep retrying until one selector has found a match207 elm := page.Race().Element(".nav-user-icon-base").MustHandle(func(e *rod.Element) {208 // print the username after successful login209 fmt.Println(*e.MustAttribute("title"))210 }).Element("[data-cy=sign-in-error]").MustDo()211 if elm.MustMatches("[data-cy=sign-in-error]") {212 // when wrong username or password213 panic(elm.MustText())214 }215}216// Rod uses mouse cursor to simulate clicks, so if a button is moving because of animation, the click may not work as expected.217// We usually use WaitStable to make sure the target isn't changing anymore.218func Example_wait_for_animation() {219 browser := rod.New().MustConnect()220 defer browser.MustClose()221 page := browser.MustPage("https://getbootstrap.com/docs/4.0/components/modal/")222 page.MustWaitLoad().MustElement("[data-target='#exampleModalLive']").MustClick()223 saveBtn := page.MustElementR("#exampleModalLive button", "Close")224 // Here, WaitStable will wait until the button's position and size become stable.225 saveBtn.MustWaitStable().MustClick().MustWaitInvisible()226 fmt.Println("done")227 // Output: done228}229// When you want to wait for an ajax request to complete, this example will be useful.230func Example_wait_for_request() {231 browser := rod.New().MustConnect()232 defer browser.MustClose()233 page := browser.MustPage("https://www.wikipedia.org/").MustWaitLoad()234 // Start to analyze request events235 wait := page.MustWaitRequestIdle()236 // This will trigger the search ajax request237 page.MustElement("#searchInput").MustClick().MustInput("lisp")238 // Wait until there's no active requests239 wait()240 // We want to make sure that after waiting, there are some autocomplete241 // suggestions available.242 fmt.Println(len(page.MustElements(".suggestion-link")) > 0)243 // Output: true244}245// Shows how to change the retry/polling options that is used to query elements.246// This is useful when you want to customize the element query retry logic.247func Example_customize_retry_strategy() {248 browser := rod.New().MustConnect()249 defer browser.MustClose()250 page := browser.MustPage("https://github.com")251 // sleep for 0.5 seconds before every retry252 sleeper := func() utils.Sleeper {253 return func(context.Context) error {254 time.Sleep(time.Second / 2)255 return nil256 }257 }258 el, _ := page.Sleeper(sleeper).Element("input")259 fmt.Println(el.MustProperty("name"))260 // If sleeper is nil page.ElementE will query without retrying.261 // If nothing found it will return an error.262 el, err := page.Sleeper(rod.NotFoundSleeper).Element("input")263 if errors.Is(err, &rod.ErrElementNotFound{}) {264 fmt.Println("element not found")265 } else if err != nil {266 panic(err)267 }268 fmt.Println(el.MustProperty("name"))269 // Output:270 // q271 // q272}273// Shows how we can further customize the browser with the launcher library.274// Usually you use launcher lib to set the browser's command line flags (switches).275// Doc for flags: https://peter.sh/experiments/chromium-command-line-switches276func Example_customize_browser_launch() {277 url := launcher.New().278 Proxy("127.0.0.1:8080"). // set flag "--proxy-server=127.0.0.1:8080"279 Delete("use-mock-keychain"). // delete flag "--use-mock-keychain"280 MustLaunch()281 browser := rod.New().ControlURL(url).MustConnect()282 defer browser.MustClose()283 // So that we don't have to self issue certs for MITM284 browser.MustIgnoreCertErrors(true)285 // Adding authentication to the proxy, for the next auth request.286 // We use CLI tool "mitmproxy --proxyauth user:pass" as an example.287 go browser.MustHandleAuth("user", "pass")()288 // mitmproxy needs a cert config to support https. We use http here instead,289 // for example290 fmt.Println(browser.MustPage("https://mdn.dev/").MustElement("title").MustText())291}292// When rod doesn't have a feature that you need. You can easily call the cdp to achieve it.293// List of cdp API: https://github.com/moredure/xrod/tree/master/lib/proto294func Example_direct_cdp() {295 page := rod.New().MustConnect().MustPage()296 // Rod doesn't have a method to enable AD blocking,297 // but you can call cdp interface directly to achieve it.298 // The two code blocks below are equal to enable AD blocking299 {300 _ = proto.PageSetAdBlockingEnabled{301 Enabled: true,302 }.Call(page)303 }304 {305 // Interact with the cdp JSON API directly306 _, _ = page.Call(context.TODO(), "", "Page.setAdBlockingEnabled", map[string]bool{307 "enabled": true,308 })309 }310}311// Shows how to listen for events.312func Example_handle_events() {313 browser := rod.New().MustConnect()314 defer browser.MustClose()315 page := browser.MustPage()316 done := make(chan struct{})317 // Listen for all events of console output.318 go page.EachEvent(func(e *proto.RuntimeConsoleAPICalled) {319 fmt.Println(page.MustObjectsToJSON(e.Args))320 close(done)321 })()322 wait := page.WaitEvent(&proto.PageLoadEventFired{})323 page.MustNavigate("https://mdn.dev")324 wait()325 // EachEvent allows us to achieve the same functionality as above.326 if false {327 // Subscribe events before they happen, run the "wait()" to start consuming328 // the events. We can return an optional stop signal to unsubscribe events.329 wait := page.EachEvent(func(e *proto.PageLoadEventFired) (stop bool) {330 return true331 })332 page.MustNavigate("https://mdn.dev")333 wait()334 }335 // Or the for-loop style to handle events to do the same thing above.336 if false {337 page.MustNavigate("https://mdn.dev")338 for msg := range page.Event() {339 e := proto.PageLoadEventFired{}340 if msg.Load(&e) {341 break342 }343 }344 }345 page.MustEval(`() => console.log("hello", "world")`)346 <-done347 // Output:348 // [hello world]349}350func Example_download_file() {351 browser := rod.New().MustConnect()352 page := browser.MustPage("https://file-examples.com/index.php/sample-documents-download/sample-pdf-download/")353 wait := browser.MustWaitDownload()354 page.MustElementR("a", "DOWNLOAD SAMPLE PDF FILE").MustClick()355 _ = utils.OutputFile("t.pdf", wait())356}357// Shows how to intercept requests and modify358// both the request and the response.359// The entire process of hijacking one request:360//361// browser --req-> rod ---> server ---> rod --res-> browser362//363// The --req-> and --res-> are the parts that can be modified.364func Example_hijack_requests() {365 browser := rod.New().MustConnect()366 defer browser.MustClose()367 router := browser.HijackRequests()368 defer router.MustStop()369 router.MustAdd("*.js", func(ctx *rod.Hijack) {370 // Here we update the request's header. Rod gives functionality to371 // change or update all parts of the request. Refer to the documentation372 // for more information.373 ctx.Request.Req().Header.Set("My-Header", "test")374 // LoadResponse runs the default request to the destination of the request.375 // Not calling this will require you to mock the entire response.376 // This can be done with the SetXxx (Status, Header, Body) functions on the377 // ctx.Response struct.378 _ = ctx.LoadResponse(http.DefaultClient, true)379 // Here we append some code to every js file.380 // The code will update the document title to "hi"381 ctx.Response.SetBody(ctx.Response.Body() + "\n document.title = 'hi' ")382 })383 go router.Run()384 browser.MustPage("https://go-rod.github.io").MustWait(`() => document.title === 'hi'`)385 fmt.Println("done")386 // Output: done387}388// Shows how to share a remote object reference between two Eval389func Example_eval_reuse_remote_object() {390 page := rod.New().MustConnect().MustPage()391 fn := page.MustEvaluate(rod.Eval(`() => Math.random`).ByObject())392 res := page.MustEval(`f => f()`, fn)393 // print a random number394 fmt.Println(res.Num())395}396// Shows how to update the state of the current page.397// In this example we enable the network domain.398func Example_states() {399 browser := rod.New().MustConnect()400 defer browser.MustClose()401 page := browser.MustPage()402 // LoadState detects whether the network domain is enabled or not.403 fmt.Println(page.LoadState(&proto.NetworkEnable{}))404 _ = proto.NetworkEnable{}.Call(page)405 // Check if the network domain is successfully enabled.406 fmt.Println(page.LoadState(&proto.NetworkEnable{}))407 // Output:408 // false409 // true410}411// We can use PagePool to concurrently control and reuse pages.412func ExamplePage_pool() {413 browser := rod.New().MustConnect()414 defer browser.MustClose()415 // We create a pool that will hold at most 3 pages which means the max concurrency is 3416 pool := rod.NewPagePool(3)417 // Create a page if needed418 create := func() *rod.Page {419 // We use MustIncognito to isolate pages with each other420 return browser.MustIncognito().MustPage()421 }422 yourJob := func() {423 page := pool.Get(create)424 defer pool.Put(page)425 page.MustNavigate("http://mdn.dev").MustWaitLoad()426 fmt.Println(page.MustInfo().Title)427 }428 // Run jobs concurrently429 wg := sync.WaitGroup{}430 for range "...." {431 wg.Add(1)432 go func() {433 defer wg.Done()434 yourJob()435 }()436 }437 wg.Wait()438 // cleanup pool439 pool.Cleanup(func(p *rod.Page) { p.MustClose() })440 // Output:441 // mdn.dev442 // mdn.dev443 // mdn.dev444 // mdn.dev445}446func Example_load_extension() {447 extPath, _ := filepath.Abs("fixtures/chrome-extension")448 u := launcher.New().449 // Must use abs path for an extension450 Set("load-extension", extPath).451 // Headless mode doesn't support extension yet.452 // Reason: https://bugs.chromium.org/p/chromium/issues/detail?id=706008#c5453 // You can use XVFB to get rid of it: https://github.com/moredure/xrod/blob/master/lib/examples/launch-managed/main.go...
browser_test.go
Source:browser_test.go
...18func TestIncognito(t *testing.T) {19 g := setup(t)20 k := g.RandStr(16)21 b := g.browser.MustIncognito().Sleeper(rod.DefaultSleeper)22 defer b.MustClose()23 page := b.MustPage(g.blank())24 defer page.MustClose()25 page.MustEval(`k => localStorage[k] = 1`, k)26 g.True(g.page.MustNavigate(g.blank()).MustEval(`k => localStorage[k]`, k).Nil())27 g.Eq(page.MustEval(`k => localStorage[k]`, k).Str(), "1") // localStorage can only store string28 g.Panic(func() {29 g.mc.stubErr(1, proto.TargetCreateBrowserContext{})30 g.browser.MustIncognito()31 })32}33func TestBrowserResetControlURL(t *testing.T) {34 rod.New().ControlURL("test").ControlURL("")35}36func TestDefaultDevice(t *testing.T) {37 g := setup(t)38 ua := ""39 s := g.Serve()40 s.Mux.HandleFunc("/t", func(rw http.ResponseWriter, r *http.Request) {41 ua = r.Header.Get("User-Agent")42 })43 // TODO: https://github.com/golang/go/issues/5145944 b := *g.browser45 b.DefaultDevice(devices.IPhoneX)46 b.MustPage(s.URL("/t")).MustClose()47 g.Eq(ua, devices.IPhoneX.UserAgentEmulation().UserAgent)48 b.NoDefaultDevice()49 b.MustPage(s.URL("/t")).MustClose()50 g.Neq(ua, devices.IPhoneX.UserAgentEmulation().UserAgent)51}52func TestPageErr(t *testing.T) {53 g := setup(t)54 g.Panic(func() {55 g.mc.stubErr(1, proto.TargetAttachToTarget{})56 g.browser.MustPage()57 })58}59func TestPageFromTarget(t *testing.T) {60 g := setup(t)61 g.Panic(func() {62 res, err := proto.TargetCreateTarget{URL: "about:blank"}.Call(g.browser)63 g.E(err)64 defer func() {65 g.browser.MustPageFromTargetID(res.TargetID).MustClose()66 }()67 g.mc.stubErr(1, proto.EmulationSetDeviceMetricsOverride{})68 g.browser.MustPageFromTargetID(res.TargetID)69 })70}71func TestBrowserPages(t *testing.T) {72 g := setup(t)73 b := g.browser74 pages := b.MustPages()75 g.Gte(len(pages), 1)76 {77 g.mc.stub(1, proto.TargetGetTargets{}, func(send StubSend) (gson.JSON, error) {78 d, _ := send()79 return *d.Set("targetInfos.0.type", "iframe"), nil80 })81 b.MustPages()82 }83 g.Panic(func() {84 g.mc.stubErr(1, proto.TargetCreateTarget{})85 b.MustPage()86 })87 g.Panic(func() {88 g.mc.stubErr(1, proto.TargetGetTargets{})89 b.MustPages()90 })91 g.Panic(func() {92 _, err := proto.TargetCreateTarget{URL: "about:blank"}.Call(b)93 g.E(err)94 g.mc.stubErr(1, proto.TargetAttachToTarget{})95 b.MustPages()96 })97}98func TestBrowserClearStates(t *testing.T) {99 g := setup(t)100 g.E(proto.EmulationClearGeolocationOverride{}.Call(g.page))101}102func TestBrowserEvent(t *testing.T) {103 g := setup(t)104 messages := g.browser.Context(g.Context()).Event()105 p := g.newPage()106 wait := make(chan struct{})107 for msg := range messages {108 e := proto.TargetAttachedToTarget{}109 if msg.Load(&e) {110 g.Eq(e.TargetInfo.TargetID, p.TargetID)111 close(wait)112 break113 }114 }115 <-wait116}117func TestBrowserWaitEvent(t *testing.T) {118 g := setup(t)119 g.NotNil(g.browser.Context(g.Context()).Event())120 wait := g.page.WaitEvent(proto.PageFrameNavigated{})121 g.page.MustNavigate(g.blank())122 wait()123 wait = g.browser.EachEvent(func(e *proto.PageFrameNavigated, id proto.TargetSessionID) bool {124 return true125 })126 g.page.MustNavigate(g.blank())127 wait()128}129func TestBrowserCrash(t *testing.T) {130 g := setup(t)131 browser := rod.New().Context(g.Context()).MustConnect()132 page := browser.MustPage()133 js := `() => new Promise(r => setTimeout(r, 10000))`134 go g.Panic(func() {135 page.MustEval(js)136 })137 utils.Sleep(0.2)138 _ = proto.BrowserCrash{}.Call(browser)139 utils.Sleep(0.3)140 _, err := page.Eval(js)141 g.Has(err.Error(), "use of closed network connection")142}143func TestBrowserCall(t *testing.T) {144 g := setup(t)145 v, err := proto.BrowserGetVersion{}.Call(g.browser)146 g.E(err)147 g.Regex("1.3", v.ProtocolVersion)148}149func TestBlockingNavigation(t *testing.T) {150 g := setup(t)151 /*152 Navigate can take forever if a page doesn't response.153 If one page is blocked, other pages should still work.154 */155 s := g.Serve()156 pause := g.Context()157 s.Mux.HandleFunc("/a", func(w http.ResponseWriter, r *http.Request) {158 <-pause.Done()159 })160 s.Route("/b", ".html", `<html>ok</html>`)161 blocked := g.newPage()162 go func() {163 g.Panic(func() {164 blocked.MustNavigate(s.URL("/a"))165 })166 }()167 utils.Sleep(0.3)168 g.newPage(s.URL("/b"))169}170func TestResolveBlocking(t *testing.T) {171 g := setup(t)172 s := g.Serve()173 pause := g.Context()174 s.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {175 <-pause.Done()176 })177 p := g.newPage()178 go func() {179 utils.Sleep(0.1)180 p.MustStopLoading()181 }()182 g.Panic(func() {183 p.MustNavigate(s.URL())184 })185}186func TestTestTry(t *testing.T) {187 g := setup(t)188 g.Nil(rod.Try(func() {}))189 err := rod.Try(func() { panic(1) })190 var errVal *rod.ErrTry191 g.True(errors.As(err, &errVal))192 g.Is(err, &rod.ErrTry{})193 g.Eq(errVal.Unwrap().Error(), "1")194 g.Eq(1, errVal.Value)195 g.Has(errVal.Error(), "error value: 1\ngoroutine")196 errVal = rod.Try(func() { panic(errors.New("t")) }).(*rod.ErrTry)197 g.Eq(errVal.Unwrap().Error(), "t")198}199func TestBrowserOthers(t *testing.T) {200 g := setup(t)201 g.browser.Timeout(time.Second).CancelTimeout().MustGetCookies()202}203func TestBinarySize(t *testing.T) {204 g := setup(t)205 if runtime.GOOS == "windows" || utils.InContainer {206 g.SkipNow()207 }208 cmd := exec.Command("go", "build",209 "-trimpath",210 "-ldflags", "-w -s",211 "-o", "tmp/translator",212 "./lib/examples/translator")213 cmd.Env = append(os.Environ(), "GOOS=linux")214 g.Nil(cmd.Run())215 stat, err := os.Stat("tmp/translator")216 g.E(err)217 g.Lte(float64(stat.Size())/1024/1024, 10.4) // mb218}219func TestBrowserCookies(t *testing.T) {220 g := setup(t)221 b := g.browser.MustIncognito()222 defer b.MustClose()223 b.MustSetCookies(&proto.NetworkCookie{224 Name: "a",225 Value: "val",226 Domain: "test.com",227 })228 cookies := b.MustGetCookies()229 g.Len(cookies, 1)230 g.Eq(cookies[0].Name, "a")231 g.Eq(cookies[0].Value, "val")232 {233 b.MustSetCookies()234 cookies := b.MustGetCookies()235 g.Len(cookies, 0)236 }237 g.mc.stubErr(1, proto.StorageGetCookies{})238 g.Err(b.GetCookies())239}240func TestWaitDownload(t *testing.T) {241 g := setup(t)242 s := g.Serve()243 content := "test content"244 s.Route("/d", ".bin", []byte(content))245 s.Route("/page", ".html", fmt.Sprintf(`<html><a href="%s/d" download>click</a></html>`, s.URL()))246 page := g.page.MustNavigate(s.URL("/page"))247 wait := g.browser.MustWaitDownload()248 page.MustElement("a").MustClick()249 data := wait()250 g.Eq(content, string(data))251}252func TestWaitDownloadDataURI(t *testing.T) {253 g := setup(t)254 s := g.Serve()255 s.Route("/", ".html",256 `<html>257 <a id="a" href="data:text/plain;,test%20data" download>click</a>258 <a id="b" download>click</a>259 <script>260 const b = document.getElementById('b')261 b.href = URL.createObjectURL(new Blob(['test blob'], {262 type: "text/plain; charset=utf-8"263 }))264 </script>265 </html>`,266 )267 page := g.page.MustNavigate(s.URL())268 wait1 := g.browser.MustWaitDownload()269 page.MustElement("#a").MustClick()270 data := wait1()271 g.Eq("test data", string(data))272 wait2 := g.browser.MustWaitDownload()273 page.MustElement("#b").MustClick()274 data = wait2()275 g.Eq("test blob", string(data))276}277func TestWaitDownloadCancel(t *testing.T) {278 g := setup(t)279 wait := g.browser.Context(g.Timeout(0)).WaitDownload(os.TempDir())280 g.Eq(wait(), (*proto.PageDownloadWillBegin)(nil))281}282func TestWaitDownloadFromNewPage(t *testing.T) {283 g := setup(t)284 s := g.Serve()285 content := "test content"286 s.Route("/d", ".bin", content)287 s.Route("/page", ".html", fmt.Sprintf(288 `<html><a href="%s/d" download target="_blank">click</a></html>`,289 s.URL()),290 )291 page := g.page.MustNavigate(s.URL("/page"))292 wait := g.browser.MustWaitDownload()293 page.MustElement("a").MustClick()294 data := wait()295 g.Eq(content, string(data))296}297func TestBrowserConnectErr(t *testing.T) {298 g := setup(t)299 g.Panic(func() {300 rod.New().ControlURL(g.RandStr(16)).MustConnect()301 })302}303func TestStreamReader(t *testing.T) {304 g := setup(t)305 r := rod.NewStreamReader(g.page, "")306 g.mc.stub(1, proto.IORead{}, func(send StubSend) (gson.JSON, error) {307 return gson.New(proto.IOReadResult{308 Data: "test",309 }), nil310 })311 b := make([]byte, 4)312 _, _ = r.Read(b)313 g.Eq("test", string(b))314 g.mc.stubErr(1, proto.IORead{})315 _, err := r.Read(nil)316 g.Err(err)317 g.mc.stub(1, proto.IORead{}, func(send StubSend) (gson.JSON, error) {318 return gson.New(proto.IOReadResult{319 Base64Encoded: true,320 Data: "@",321 }), nil322 })323 _, err = r.Read(nil)324 g.Err(err)325}326func TestBrowserConnectFailure(t *testing.T) {327 g := setup(t)328 c := g.Context()329 c.Cancel()330 err := rod.New().Context(c).Connect()331 if err == nil {332 g.Fatal("expected an error on connect failure")333 }334}335func TestBrowserPool(t *testing.T) {336 pool := rod.NewBrowserPool(3)337 create := func() *rod.Browser { return rod.New().MustConnect() }338 b := pool.Get(create)339 pool.Put(b)340 pool.Cleanup(func(p *rod.Browser) {341 p.MustClose()342 })343}344func TestOldBrowser(t *testing.T) {345 t.Skip()346 g := setup(t)347 u := launcher.New().Revision(686378).MustLaunch()348 b := rod.New().ControlURL(u).MustConnect()349 g.Cleanup(b.MustClose)350 res, err := proto.BrowserGetVersion{}.Call(b)351 g.E(err)352 g.Eq(res.Revision, "@19d4547535ab5aba70b4730443f84e8153052174")353}354func TestBrowserLostConnection(t *testing.T) {355 g := setup(t)356 l := launcher.New()357 p := rod.New().ControlURL(l.MustLaunch()).MustConnect().MustPage(g.blank())358 go func() {359 utils.Sleep(1)360 l.Kill()361 }()362 _, err := p.Eval(`() => new Promise(r => {})`)363 g.Err(err)...
MustClose
Using AI Code Generation
1import (2func main() {3 browser := rod.New().MustConnect()4 page.MustElement("input[name=q]").MustInput("rod")5 page.MustElement("input[name=q]").MustPress(input.Enter)6 page.MustWaitLoad()7 page.MustWaitLoad()8 page.MustElement("input[name=q]").MustInput("rod")9 page.MustElement("input[name=q]").MustPress(input.Enter)10 page.MustWaitLoad()11 page.MustWaitLoad()12 links := page.MustElements("a")13 fmt.Println(len(links))14 fmt.Println(links[0].MustText())15 fmt.Println(links[0].MustProperty("href"))16 fmt.Println(links[0].MustAttribute("href"))17 fmt.Println(links[0].MustInfo().NodeName)18 fmt.Println(links[0].MustInfo().NodeType)19 fmt.Println(links[0].MustInfo().NodeValue)20 fmt.Println(links[0].MustInfo().LocalName)21 fmt.Println(links[0].MustInfo().ChildNodeCount)22 fmt.Println(links[0].MustInfo().ShadowRootType)23 fmt.Println(links[0].MustInfo().FrameID)24 fmt.Println(links[0].MustInfo().ContentDocument)25 fmt.Println(links[0].MustInfo().ShadowRoots)26 fmt.Println(links[0].MustInfo().PseudoElements)27 fmt.Println(links[0].MustInfo().ImportedDocument)28 fmt.Println(links[0].MustInfo().DistributedNodes)29 fmt.Println(links[0].MustInfo().Is
MustClose
Using AI Code Generation
1import (2func main() {3 l := launcher.New().MustLaunch()4 defer utils.E(l.Close)5 browser := rod.New().ControlURL(l).MustConnect()6 title := page.MustElement("title").MustText()7 fmt.Println(title)8 browser.MustClose()9}
MustClose
Using AI Code Generation
1import (2func main() {3 browser := rod.New().MustConnect()4 input := page.MustElement("#lst-ib")5 input.MustInput("Rod").MustPress(proto.InputKeyEventCodeEnter)6 page.MustWaitLoad()7 browser.MustClose()8}9import (10func main() {11 browser := rod.New().MustConnect()12 input := page.MustElement("#lst-ib")13 input.MustInput("Rod").MustPress(proto.InputKeyEventCodeEnter)14 page.MustWaitLoad()15 browser.MustClose()16}17import (18func main() {19 browser := rod.New().MustConnect()20 input := page.MustElement("#lst-ib")21 input.MustInput("Rod").MustPress(proto.InputKeyEventCodeEnter)22 page.MustWaitLoad()23 browser.MustClose()24}25import (26func main() {27 browser := rod.New().MustConnect()
MustClose
Using AI Code Generation
1import (2func main() {3 b := rod.New().MustConnect()4 title := p.MustElement("h1").MustText()5 fmt.Println(title)6 b.MustClose()7}
MustClose
Using AI Code Generation
1import (2func main() {3 browser := rod.New().MustConnect()4 title := page.MustTitle()5 fmt.Println(title)6 browser.MustClose()7}8import (9func main() {10 browser := rod.New().Connect()11 title := page.Title()12 fmt.Println(title)13 browser.Close()14}15import (16func main() {17 browser := rod.New().Connect()18 title := page.Title()19 fmt.Println(title)20 browser.Close()21}22import (23func main() {24 browser := rod.New().Connect()25 title := page.Title()26 fmt.Println(title)27 browser.Close()28}29import (30func main() {31 browser := rod.New().Connect()32 title := page.Title()33 fmt.Println(title)34 browser.Close()35}
MustClose
Using AI Code Generation
1import "fmt"2type rod struct {3}4func (r rod) MustClose() {5fmt.Println("rod is closed")6}7func main() {8r := rod{5}9r.MustClose()10}
MustClose
Using AI Code Generation
1import "fmt"2type rod struct {3}4func (r rod) MustClose() {5fmt.Println("Closing rod")6}7func main() {8r.MustClose()9}
MustClose
Using AI Code Generation
1import (2func main() {3 browser := rod.New().MustConnect()4 defer browser.MustClose()5 element := page.MustElement("input[name=q]")6 element.MustInput("rod")7 element.MustPress(proto.InputKeyEventEnter)8 fmt.Println(page.MustInfo().Title)9}
MustClose
Using AI Code Generation
1import (2func main() {3 f, err := os.Create("test.txt")4 if err != nil {5 log.Fatal(err)6 }7 defer rod.MustClose(f)8 _, err = f.WriteString("Hello World!")9 if err != nil {10 log.Fatal(err)11 }12 err = f.Sync()13 if err != nil {14 log.Fatal(err)15 }16 f, err = os.Open("test.txt")17 if err != nil {18 log.Fatal(err)19 }20 b := make([]byte, 100)21 n, err := f.Read(b)22 if err != nil {23 log.Fatal(err)24 }25 fmt.Printf("The file contains: %s", b[:n])26}27import (28func main() {29 f, err := os.Create("test.txt")30 if err != nil {31 log.Fatal(err)32 }33 defer rod.MustClose(f)34 _, err = f.WriteString("Hello World!")35 if err != nil {36 log.Fatal(err)37 }38 err = f.Sync()39 if err != nil {40 log.Fatal(err)41 }42 f, err = os.Open("test.txt")43 if err != nil {44 log.Fatal(err)45 }46 b := make([]byte, 100)47 n, err := f.Read(b)48 if err != nil {49 log.Fatal(err)50 }51 fmt.Printf("The file contains: %s", b[:n])52}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!