How to use SetFiles method of rod Package

Best Rod code snippet using rod.SetFiles

page_test.go

Source:page_test.go Github

copy

Full Screen

1package rod_test2import (3 "bytes"4 "context"5 "image/png"6 "math"7 "net/http"8 "os"9 "path/filepath"10 "sort"11 "sync"12 "testing"13 "time"14 "github.com/go-rod/rod"15 "github.com/go-rod/rod/lib/cdp"16 "github.com/go-rod/rod/lib/defaults"17 "github.com/go-rod/rod/lib/devices"18 "github.com/go-rod/rod/lib/proto"19 "github.com/go-rod/rod/lib/utils"20 "github.com/ysmood/gson"21)22func TestGetPageBrowser(t *testing.T) {23 g := setup(t)24 g.Eq(g.page.Browser().BrowserContextID, g.browser.BrowserContextID)25}26func TestGetPageURL(t *testing.T) {27 g := setup(t)28 g.page.MustNavigate(g.srcFile("fixtures/click-iframe.html")).MustWaitLoad()29 g.Regex(`/fixtures/click-iframe.html\z`, g.page.MustInfo().URL)30}31func TestSetCookies(t *testing.T) {32 g := setup(t)33 s := g.Serve()34 page := g.page.MustSetCookies([]*proto.NetworkCookieParam{{35 Name: "cookie-a",36 Value: "1",37 URL: s.URL(),38 }, {39 Name: "cookie-b",40 Value: "2",41 URL: s.URL(),42 }}...).MustNavigate(s.URL()).MustWaitLoad()43 cookies := page.MustCookies()44 sort.Slice(cookies, func(i, j int) bool {45 return cookies[i].Value < cookies[j].Value46 })47 g.Eq("1", cookies[0].Value)48 g.Eq("2", cookies[1].Value)49 page.MustSetCookies()50 cookies = page.MustCookies()51 g.Len(cookies, 0)52 g.Panic(func() {53 g.mc.stubErr(1, proto.TargetGetTargetInfo{})54 page.MustCookies()55 })56 g.Panic(func() {57 g.mc.stubErr(1, proto.NetworkGetCookies{})58 page.MustCookies()59 })60}61func TestSetExtraHeaders(t *testing.T) {62 g := setup(t)63 s := g.Serve()64 wg := sync.WaitGroup{}65 var header http.Header66 s.Mux.HandleFunc("/", func(rw http.ResponseWriter, r *http.Request) {67 header = r.Header68 wg.Done()69 })70 p := g.newPage()71 cleanup := p.MustSetExtraHeaders("a", "1", "b", "2")72 wg.Add(1)73 p.MustNavigate(s.URL())74 wg.Wait()75 g.Eq(header.Get("a"), "1")76 g.Eq(header.Get("b"), "2")77 cleanup()78 // TODO: I don't know why it will fail randomly79 if false {80 wg.Add(1)81 p.MustReload()82 wg.Wait()83 g.Eq(header.Get("a"), "")84 g.Eq(header.Get("b"), "")85 }86}87func TestSetUserAgent(t *testing.T) {88 g := setup(t)89 s := g.Serve()90 ua := ""91 lang := ""92 wg := sync.WaitGroup{}93 wg.Add(1)94 s.Mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {95 ua = r.Header.Get("User-Agent")96 lang = r.Header.Get("Accept-Language")97 wg.Done()98 })99 g.newPage().MustSetUserAgent(nil).MustNavigate(s.URL())100 wg.Wait()101 g.Eq(ua, "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_0_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")102 g.Eq(lang, "en")103}104func TestPageHTML(t *testing.T) {105 g := setup(t)106 p := g.page.MustNavigate(g.srcFile("fixtures/click.html")).MustWaitLoad()107 p.MustElement("button").MustClick()108 g.Has(p.MustHTML(), `a="ok"`)109 g.mc.stubErr(1, proto.RuntimeCallFunctionOn{})110 g.Err(p.HTML())111}112func TestMustWaitElementsMoreThan(t *testing.T) {113 g := setup(t)114 p := g.page.MustNavigate(g.srcFile("fixtures/wait_elements.html")).MustWaitElementsMoreThan("li", 5)115 g.Gt(len(p.MustElements("li")), 5)116}117func TestPageCloseCancel(t *testing.T) {118 g := setup(t)119 page := g.browser.MustPage(g.srcFile("fixtures/prevent-close.html"))120 page.MustElement("body").MustClick() // only focused page will handle beforeunload event121 w, h := page.MustHandleDialog()122 go func() {123 w()124 h(false, "")125 }()126 g.Eq(page.Close().Error(), "page close canceled")127 page.MustEval(`() => window.onbeforeunload = null`)128 page.MustClose()129}130func TestLoadState(t *testing.T) {131 g := setup(t)132 g.True(g.page.LoadState(&proto.PageEnable{}))133}134func TestDisableDomain(t *testing.T) {135 g := setup(t)136 defer g.page.DisableDomain(&proto.PageEnable{})()137}138func TestPageContext(t *testing.T) {139 g := setup(t)140 g.page.Timeout(time.Hour).CancelTimeout().MustEval(`() => 1`)141 _, _ = g.page.Timeout(time.Second).Timeout(time.Hour).CancelTimeout().Element("not-exist")142}143func TestPageActivate(t *testing.T) {144 g := setup(t)145 g.page.MustActivate()146}147func TestWindow(t *testing.T) {148 g := setup(t)149 page := g.newPage(g.blank())150 g.E(page.SetViewport(nil))151 bounds := page.MustGetWindow()152 defer page.MustSetWindow(153 *bounds.Left,154 *bounds.Top,155 *bounds.Width,156 *bounds.Height,157 )158 page.MustWindowMaximize()159 page.MustWindowNormal()160 page.MustWindowFullscreen()161 page.MustWindowNormal()162 page.MustWindowMinimize()163 page.MustWindowNormal()164 page.MustSetWindow(0, 0, 1211, 611)165 w, err := proto.BrowserGetWindowForTarget{}.Call(page)166 g.E(err)167 g.Eq(w.Bounds.Width, 1211)168 g.Eq(w.Bounds.Height, 611)169 g.Panic(func() {170 g.mc.stubErr(1, proto.BrowserGetWindowForTarget{})171 page.MustGetWindow()172 })173 g.Panic(func() {174 g.mc.stubErr(1, proto.BrowserGetWindowBounds{})175 page.MustGetWindow()176 })177 g.Panic(func() {178 g.mc.stubErr(1, proto.BrowserGetWindowForTarget{})179 page.MustSetWindow(0, 0, 1000, 1000)180 })181}182func TestSetViewport(t *testing.T) {183 g := setup(t)184 page := g.newPage(g.blank())185 page.MustSetViewport(317, 419, 0, false)186 res := page.MustEval(`() => [window.innerWidth, window.innerHeight]`)187 g.Eq(317, res.Get("0").Int())188 g.Eq(419, res.Get("1").Int())189 page2 := g.newPage(g.blank())190 res = page2.MustEval(`() => [window.innerWidth, window.innerHeight]`)191 g.Neq(int(317), res.Get("0").Int())192}193func TestSetDocumentContent(t *testing.T) {194 g := setup(t)195 page := g.newPage(g.blank())196 doctype := "<!DOCTYPE html>"197 html4StrictDoctype := `<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">`198 html4LooseDoctype := `<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">`199 xhtml11Doctype := `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">`200 exampleWithHTML4StrictDoctype := html4StrictDoctype + "<html><head></head><body><div>test</div></body></html>"201 page.MustSetDocumentContent(exampleWithHTML4StrictDoctype)202 exp1 := page.MustEval(`() => new XMLSerializer().serializeToString(document)`).Str()203 g.Eq(exp1, `<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>test</div></body></html>`)204 g.Eq(page.MustElement("html").MustHTML(), "<html><head></head><body><div>test</div></body></html>")205 g.Eq(page.MustElement("head").MustText(), "")206 exampleWithHTML4LooseDoctype := html4LooseDoctype + "<html><head></head><body><div>test</div></body></html>"207 page.MustSetDocumentContent(exampleWithHTML4LooseDoctype)208 exp2 := page.MustEval(`() => new XMLSerializer().serializeToString(document)`).Str()209 g.Eq(exp2, `<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>test</div></body></html>`)210 g.Eq(page.MustElement("html").MustHTML(), "<html><head></head><body><div>test</div></body></html>")211 g.Eq(page.MustElement("head").MustText(), "")212 exampleWithXHTMLDoctype := xhtml11Doctype + "<html><head></head><body><div>test</div></body></html>"213 page.MustSetDocumentContent(exampleWithXHTMLDoctype)214 exp3 := page.MustEval(`() => new XMLSerializer().serializeToString(document)`).Str()215 g.Eq(exp3, `<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>test</div></body></html>`)216 g.Eq(page.MustElement("html").MustHTML(), "<html><head></head><body><div>test</div></body></html>")217 g.Eq(page.MustElement("head").MustText(), "")218 exampleWithHTML5Doctype := doctype + "<html><head></head><body><div>test</div></body></html>"219 page.MustSetDocumentContent(exampleWithHTML5Doctype)220 exp4 := page.MustEval(`() => new XMLSerializer().serializeToString(document)`).Str()221 g.Eq(exp4, `<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head></head><body><div>test</div></body></html>`)222 g.Eq(page.MustElement("html").MustHTML(), "<html><head></head><body><div>test</div></body></html>")223 g.Eq(page.MustElement("head").MustText(), "")224 exampleWithoutDoctype := "<html><head></head><body><div>test</div></body></html>"225 page.MustSetDocumentContent(exampleWithoutDoctype)226 g.Eq(page.MustElement("html").MustHTML(), "<html><head></head><body><div>test</div></body></html>")227 exampleBasic := doctype + "<div>test</div>"228 page.MustSetDocumentContent(exampleBasic)229 g.Eq(page.MustElement("div").MustText(), "test")230 exampleWithTrickyContent := "<div>test</div>\x7F"231 page.MustSetDocumentContent(exampleWithTrickyContent)232 g.Eq(page.MustElement("div").MustText(), "test")233 exampleWithEmoji := "<div>💪</div>"234 page.MustSetDocumentContent(exampleWithEmoji)235 g.Eq(page.MustElement("div").MustText(), "💪")236}237func TestEmulateDevice(t *testing.T) {238 g := setup(t)239 page := g.newPage(g.blank())240 page.MustEmulate(devices.IPhone6or7or8)241 res := page.MustEval(`() => [window.innerWidth, window.innerHeight, navigator.userAgent]`)242 // TODO: this seems like a bug of chromium243 {244 g.Lt(math.Abs(float64(980-res.Get("0").Int())), 10)245 g.Lt(math.Abs(float64(1743-res.Get("1").Int())), 10)246 }247 g.Eq(248 "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1",249 res.Get("2").String(),250 )251 g.Panic(func() {252 g.mc.stubErr(1, proto.EmulationSetDeviceMetricsOverride{})253 page.MustEmulate(devices.IPad)254 })255 g.Panic(func() {256 g.mc.stubErr(1, proto.EmulationSetTouchEmulationEnabled{})257 page.MustEmulate(devices.IPad)258 })259}260func TestPageCloseErr(t *testing.T) {261 g := setup(t)262 page := g.newPage(g.blank())263 g.Panic(func() {264 g.mc.stubErr(1, proto.PageClose{})265 page.MustClose()266 })267}268func TestPageCloseWhenNotAttached(t *testing.T) {269 g := setup(t)270 p := g.browser.MustPage(g.blank())271 _ = p.Navigate("http://not-exists")272 g.E(p.Close())273}274func TestPageAddScriptTag(t *testing.T) {275 g := setup(t)276 p := g.page.MustNavigate(g.blank()).MustWaitLoad()277 res := p.MustAddScriptTag(g.srcFile("fixtures/add-script-tag.js")).MustEval(`() => count()`)278 g.Eq(0, res.Int())279 res = p.MustAddScriptTag(g.srcFile("fixtures/add-script-tag.js")).MustEval(`() => count()`)280 g.Eq(1, res.Int())281 g.E(p.AddScriptTag("", `let ok = 'yes'`))282 res = p.MustEval(`() => ok`)283 g.Eq("yes", res.String())284}285func TestPageAddStyleTag(t *testing.T) {286 g := setup(t)287 p := g.page.MustNavigate(g.srcFile("fixtures/click.html")).MustWaitLoad()288 res := p.MustAddStyleTag(g.srcFile("fixtures/add-style-tag.css")).289 MustElement("h4").MustEval(`() => getComputedStyle(this).color`)290 g.Eq("rgb(255, 0, 0)", res.String())291 p.MustAddStyleTag(g.srcFile("fixtures/add-style-tag.css"))292 g.Len(p.MustElements("link"), 1)293 g.E(p.AddStyleTag("", "h4 { color: green; }"))294 res = p.MustElement("h4").MustEval(`() => getComputedStyle(this).color`)295 g.Eq("rgb(0, 128, 0)", res.String())296}297func TestPageWaitOpen(t *testing.T) {298 g := setup(t)299 page := g.page.MustNavigate(g.srcFile("fixtures/open-page.html"))300 wait := page.MustWaitOpen()301 page.MustElement("a").MustClick()302 newPage := wait()303 defer newPage.MustClose()304 g.Eq("new page", newPage.MustEval("() => window.a").String())305}306func TestPageWait(t *testing.T) {307 g := setup(t)308 page := g.page.MustNavigate(g.srcFile("fixtures/click.html"))309 page.MustWait(`() => document.querySelector('button') !== null`)310 g.Panic(func() {311 g.mc.stubErr(1, proto.RuntimeCallFunctionOn{})312 page.MustWait(``)313 })314}315func TestPageNavigateBlank(t *testing.T) {316 g := setup(t)317 g.page.MustNavigate("")318}319func TestPageWaitNavigation(t *testing.T) {320 g := setup(t)321 s := g.Serve().Route("/", "")322 wait := g.page.MustWaitNavigation()323 g.page.MustNavigate(s.URL())324 wait()325}326func TestPageWaitRequestIdle(t *testing.T) {327 g := setup(t)328 s := g.Serve()329 sleep := time.Second330 s.Route("/r1", "")331 s.Mux.HandleFunc("/r2", func(w http.ResponseWriter, r *http.Request) {332 g.E(w.Write([]byte("part")))333 ctx, cancel := context.WithTimeout(g.Context(), sleep)334 defer cancel()335 <-ctx.Done()336 })337 s.Mux.HandleFunc("/r3", func(rw http.ResponseWriter, r *http.Request) {338 rw.Header().Add("Location", "/r4")339 rw.WriteHeader(http.StatusFound)340 })341 s.Route("/r4", "")342 s.Route("/", ".html", `<html></html>`)343 page := g.newPage(s.URL()).MustWaitLoad()344 code := ` () => {345 fetch('/r2').then(r => r.text())346 fetch('/r1')347 fetch('/r3')348 }`349 waitReq := ""350 g.browser.Logger(utils.Log(func(msg ...interface{}) {351 typ := msg[0].(rod.TraceType)352 if typ == rod.TraceTypeWaitRequests {353 list := msg[2].(map[string]string)354 for _, v := range list {355 waitReq = v356 break357 }358 }359 }))360 defer g.browser.Logger(rod.DefaultLogger)361 g.browser.Trace(true)362 wait := page.MustWaitRequestIdle("/r1")363 g.browser.Trace(defaults.Trace)364 page.MustEval(code)365 start := time.Now()366 wait()367 g.Gt(time.Since(start), sleep)368 g.Regex("/r2$", waitReq)369 wait = page.MustWaitRequestIdle("/r2")370 page.MustEval(code)371 start = time.Now()372 wait()373 g.Lt(time.Since(start), sleep)374 g.Panic(func() {375 wait()376 })377}378func TestPageWaitIdle(t *testing.T) {379 g := setup(t)380 p := g.page.MustNavigate(g.srcFile("fixtures/click.html"))381 p.MustElement("button").MustClick()382 p.MustWaitIdle()383 g.True(p.MustHas("[a=ok]"))384}385func TestPageEventSession(t *testing.T) {386 g := setup(t)387 s := g.Serve()388 p := g.newPage(s.URL())389 p.EnableDomain(proto.NetworkEnable{})390 go g.page.Context(g.Context()).EachEvent(func(e *proto.NetworkRequestWillBeSent) {391 g.Log("should not goes to here")392 g.Fail()393 })()394 p.MustEval(`u => fetch(u)`, s.URL())395}396func TestPageWaitEvent(t *testing.T) {397 g := setup(t)398 wait := g.page.WaitEvent(&proto.PageFrameNavigated{})399 g.page.MustNavigate(g.blank())400 wait()401}402func TestPageWaitEventParseEventOnlyOnce(t *testing.T) {403 g := setup(t)404 nav1 := g.page.WaitEvent(&proto.PageFrameNavigated{})405 nav2 := g.page.WaitEvent(&proto.PageFrameNavigated{})406 g.page.MustNavigate(g.blank())407 nav1()408 nav2()409}410func TestPageEvent(t *testing.T) {411 g := setup(t)412 p := g.browser.MustPage()413 ctx := g.Context()414 events := p.Context(ctx).Event()415 p.MustNavigate(g.blank())416 for msg := range events {417 if msg.Load(proto.PageFrameStartedLoading{}) {418 break419 }420 }421 utils.Sleep(0.3)422 ctx.Cancel()423 go func() {424 for range p.Event() {425 }426 }()427 p.MustClose()428}429func TestPageStopEventAfterDetach(t *testing.T) {430 g := setup(t)431 p := g.browser.MustPage().Context(g.Context())432 go func() {433 utils.Sleep(0.3)434 p.MustClose()435 }()436 for range p.Event() {437 }438}439func TestAlert(t *testing.T) {440 g := setup(t)441 page := g.page.MustNavigate(g.srcFile("fixtures/alert.html"))442 wait, handle := page.MustHandleDialog()443 go page.MustElement("button").MustClick()444 e := wait()445 g.Eq(e.Message, "clicked")446 handle(true, "")447}448func TestPageHandleFileDialog(t *testing.T) {449 g := setup(t)450 p := g.page.MustNavigate(g.srcFile("fixtures/input.html"))451 el := p.MustElement(`[type=file]`)452 setFiles := p.MustHandleFileDialog()453 el.MustClick()454 setFiles(slash("fixtures/click.html"), slash("fixtures/alert.html"))455 list := el.MustEval("() => Array.from(this.files).map(f => f.name)").Arr()456 g.Len(list, 2)457 g.Eq("alert.html", list[1].String())458 {459 g.mc.stubErr(1, proto.PageSetInterceptFileChooserDialog{})460 g.Err(p.HandleFileDialog())461 }462 {463 g.mc.stubErr(2, proto.PageSetInterceptFileChooserDialog{})464 setFiles, _ := p.HandleFileDialog()465 el.MustClick()466 g.Err(setFiles([]string{slash("fixtures/click.html")}))467 g.E(proto.PageSetInterceptFileChooserDialog{Enabled: false}.Call(p))468 }469}470func TestPageScreenshot(t *testing.T) {471 g := setup(t)472 f := filepath.Join("tmp", "screenshots", g.RandStr(16)+".png")473 p := g.page.MustNavigate(g.srcFile("fixtures/click.html"))474 p.MustElement("button")475 p.MustScreenshot()476 data := p.MustScreenshot(f)477 img, err := png.Decode(bytes.NewBuffer(data))478 g.E(err)479 g.Eq(1280, img.Bounds().Dx())480 g.Eq(800, img.Bounds().Dy())481 g.Nil(os.Stat(f))482 p.MustScreenshot("")483 g.Panic(func() {484 g.mc.stubErr(1, proto.PageCaptureScreenshot{})485 p.MustScreenshot()486 })487}488func TestScreenshotFullPage(t *testing.T) {489 g := setup(t)490 p := g.page.MustNavigate(g.srcFile("fixtures/scroll.html"))491 p.MustElement("button")492 data := p.MustScreenshotFullPage()493 img, err := png.Decode(bytes.NewBuffer(data))494 g.E(err)495 res := p.MustEval(`() => ({w: document.documentElement.scrollWidth, h: document.documentElement.scrollHeight})`)496 g.Eq(res.Get("w").Int(), img.Bounds().Dx())497 g.Eq(res.Get("h").Int(), img.Bounds().Dy())498 // after the full page screenshot the window size should be the same as before499 res = p.MustEval(`() => ({w: innerWidth, h: innerHeight})`)500 g.Eq(1280, res.Get("w").Int())501 g.Eq(800, res.Get("h").Int())502 p.MustScreenshotFullPage()503 noEmulation := g.newPage(g.blank())504 g.E(noEmulation.SetViewport(nil))505 noEmulation.MustScreenshotFullPage()506 g.Panic(func() {507 g.mc.stubErr(1, proto.PageGetLayoutMetrics{})508 p.MustScreenshotFullPage()509 })510 g.Panic(func() {511 g.mc.stubErr(1, proto.EmulationSetDeviceMetricsOverride{})512 p.MustScreenshotFullPage()513 })514 g.Panic(func() {515 g.mc.stub(1, proto.PageGetLayoutMetrics{}, func(send StubSend) (gson.JSON, error) {516 return gson.New(proto.PageGetLayoutMetricsResult{}), nil517 })518 p.MustScreenshotFullPage()519 })520}521func TestScreenshotFullPageInit(t *testing.T) {522 g := setup(t)523 p := g.newPage(g.srcFile("fixtures/scroll.html"))524 // should not panic525 p.MustScreenshotFullPage()526}527func TestPageConsoleLog(t *testing.T) {528 g := setup(t)529 p := g.newPage(g.blank()).MustWaitLoad()530 e := &proto.RuntimeConsoleAPICalled{}531 wait := p.WaitEvent(e)532 p.MustEval(`() => console.log(1, {b: ['test']})`)533 wait()534 g.Eq("test", p.MustObjectToJSON(e.Args[1]).Get("b.0").String())535 g.Eq(`1 map[b:[test]]`, p.MustObjectsToJSON(e.Args).Join(" "))536}537func TestFonts(t *testing.T) {538 g := setup(t)539 if !utils.InContainer { // No need to test font rendering on regular OS540 g.SkipNow()541 }542 p := g.page.MustNavigate(g.srcFile("fixtures/fonts.html")).MustWaitLoad()543 p.MustPDF("tmp", "fonts.pdf") // download the file from Github Actions Artifacts544}545func TestPagePDF(t *testing.T) {546 g := setup(t)547 p := g.page.MustNavigate(g.srcFile("fixtures/click.html"))548 s, err := p.PDF(&proto.PagePrintToPDF{})549 g.E(err)550 g.Nil(s.Close())551 p.MustPDF("")552 g.Panic(func() {553 g.mc.stubErr(1, proto.PagePrintToPDF{})554 p.MustPDF()555 })556}557func TestPageNavigateNetworkErr(t *testing.T) {558 g := setup(t)559 p := g.newPage()560 err := p.Navigate("http://127.0.0.1:1")561 g.Is(err, &rod.ErrNavigation{})562 g.Is(err.Error(), "navigation failed: net::ERR_NAME_NOT_RESOLVED")563 p.MustNavigate("about:blank")564}565func TestPageNavigateErr(t *testing.T) {566 g := setup(t)567 s := g.Serve()568 s.Mux.HandleFunc("/404", func(w http.ResponseWriter, r *http.Request) {569 w.WriteHeader(404)570 })571 s.Mux.HandleFunc("/500", func(w http.ResponseWriter, r *http.Request) {572 w.WriteHeader(500)573 })574 // will not panic575 g.page.MustNavigate(s.URL("/404"))576 g.page.MustNavigate(s.URL("/500"))577 g.Panic(func() {578 g.mc.stubErr(1, proto.PageNavigate{})579 g.page.MustNavigate(g.blank())580 })581}582func TestPageWaitLoadErr(t *testing.T) {583 g := setup(t)584 g.Panic(func() {585 g.mc.stubErr(1, proto.RuntimeCallFunctionOn{})586 g.page.MustWaitLoad()587 })588}589func TestPageNavigation(t *testing.T) {590 g := setup(t)591 p := g.newPage().MustReload()592 wait := p.WaitNavigation(proto.PageLifecycleEventNameDOMContentLoaded)593 p.MustNavigate(g.srcFile("fixtures/click.html"))594 wait()595 wait = p.WaitNavigation(proto.PageLifecycleEventNameDOMContentLoaded)596 p.MustNavigate(g.srcFile("fixtures/selector.html"))597 wait()598 wait = p.WaitNavigation(proto.PageLifecycleEventNameDOMContentLoaded)599 p.MustNavigateBack()600 wait()601 g.Regex("fixtures/click.html$", p.MustInfo().URL)602 wait = p.WaitNavigation(proto.PageLifecycleEventNameDOMContentLoaded)603 p.MustNavigateForward()604 wait()605 g.Regex("fixtures/selector.html$", p.MustInfo().URL)606 g.mc.stubErr(1, proto.RuntimeCallFunctionOn{})607 g.Err(p.Reload())608}609func TestPagePool(t *testing.T) {610 g := setup(t)611 pool := rod.NewPagePool(3)612 create := func() *rod.Page { return g.browser.MustPage() }613 p := pool.Get(create)614 pool.Put(p)615 pool.Cleanup(func(p *rod.Page) {616 p.MustClose()617 })618}619func TestPageUseNonExistSession(t *testing.T) {620 g := setup(t)621 p := g.browser.PageFromSession("nonexist")622 err := proto.PageClose{}.Call(p)623 g.Eq(err, cdp.ErrSessionNotFound)624}625func TestPageElementFromObjectErr(t *testing.T) {626 g := setup(t)627 p := g.newPage()628 wait := p.WaitNavigation(proto.PageLifecycleEventNameLoad)629 p.MustNavigate(g.srcFile("./fixtures/click.html"))630 wait()631 res, err := proto.DOMGetNodeForLocation{X: 10, Y: 10}.Call(p)632 g.E(err)633 obj, err := proto.DOMResolveNode{634 BackendNodeID: res.BackendNodeID,635 }.Call(p)636 g.E(err)637 g.mc.stubErr(1, proto.RuntimeEvaluate{})638 g.Err(p.ElementFromObject(obj.Object))639}640func TestPageActionAfterClose(t *testing.T) {641 g := setup(t)642 {643 p := g.browser.MustPage(g.blank())644 p.MustClose()645 _, err := p.Element("nonexists")646 g.Eq(err, context.Canceled)647 }648 {649 p := g.browser.MustPage(g.blank())650 go func() {651 utils.Sleep(1)652 p.MustClose()653 }()654 _, err := p.Eval(`() => new Promise(r => {})`)655 g.Eq(err, context.Canceled)656 }657}...

Full Screen

Full Screen

main.go

Source:main.go Github

copy

Full Screen

...82 assertEquals(page.MustElement("div.upload-help > p.drop-files-text.mt-4.mb-0").MustText(), "Drop Files Here to Upload", "The folder `go-rod-test2` is not empty upon creation")83 // Add a file and cancel its upload immediately84 // Add a file into folder85 storjLogo, _ := filepath.Abs("./input/storjlogo.jpeg")86 page.MustElement("input[type=file]").SetFiles([]string{storjLogo})87 // Check to see if file was uploaded88 assertEquals(page.MustElementR("span", " storjlogo.jpeg").MustText(), " storjlogo.jpeg", "The file `storjlog.jpeg` was not uploaded successfully")89 // Click on the file name90 page.MustElementR("span", "storjlogo.jpeg").MustClick()91 assertContains(page.MustElement("img.preview.img-fluid").MustProperty("src").Str(), "storjlogo.jpeg", "The modal did not open on file click")92 // Share a file93 page.MustElementR("span", "Share").MustClick()94 assertEquals(page.MustElement("#generateShareLink").MustText(), "Copy Link", "The modal share functionality is not working")95 page.MustElement("div.col-6.col-lg-4.pr-5 > div.text-right > svg").MustClick()96 // Click on the hamburger and share97 page.MustElement("div.dropleft > button.btn.btn-white.btn-actions").MustClick()98 page.MustElementR("button.dropdown-item.action.p-3", "Share").MustClick()99 assertEquals(page.MustElement("#btn-copy-link").MustText(), "Copy Link", "The dropdown share functionality is not working")100 page.MustScreenshot("./output/shareModal.png")101 page.MustElement("#shareModal > div > div > div.modal-header.border-0 > button > span").MustClick()102 // Click on the hamburger and then details103 page.MustElement("div.dropleft > button.btn.btn-white.btn-actions").MustClick()104 page.MustElementR("button.dropdown-item.action.p-3", "Details").MustClick()105 assertContains(page.MustElement("img.preview.img-fluid").MustProperty("src").Str(), "storjlogo.jpeg", "The dropdown details functionality is not working")106 page.MustElement("div.col-6.col-lg-4.pr-5 > div.text-right > svg").MustClick()107 // Use the `..` to navigate out of the folder108 page.MustElement("td > a > a.px-2.font-weight-bold").MustClick()109 assertEquals(page.MustElement("span > a > a.file-name").MustText(), "go-rod-test2", "The `..` navigation is not working")110 // Add another folder111 page.MustElement("button.btn.btn-light.btn-block").MustClick()112 page.MustElement("td > input.form-control.input-folder").MustInput("go-rod-test3")113 page.MustElement("td > button.btn.btn-primary.btn-sm.px-4").MustClick()114 assertEquals(page.MustElementR("a", "go-rod-test3").MustText(), "go-rod-test3", "The second folder `go-rod-test3` was not created")115 // Add two files116 storjComponents, _ := filepath.Abs("./input/storjcomponents.png")117 page.MustElement("input[type=file]").SetFiles([]string{storjLogo})118 page.MustElement("input[type=file]").SetFiles([]string{storjComponents})119 assertEquals(page.MustElementR("span", " storjcomponents.png").MustText(), " storjcomponents.png", "The second file `storjcomponents.png` was not uploaded successfully")120 // Sort folders/files (by name, size, and date)121 assertEquals(page.MustElement("table > tbody > tr:nth-child(1) > td.w-50 > span > span > a > a").MustText(), "go-rod-test2", "The automatic sorting by name for folders is not working")122 assertEquals(page.MustElement("table > tbody > tr:nth-child(3) > td.w-50 > span").MustText(), " storjcomponents.png", "The automatic sorting by name for files is not working")123 page.MustElement("span > a.arrow > svg").MustClick()124 assertEquals(page.MustElement("table > tbody > tr:nth-child(1) > td.w-50 > span > span > a > a").MustText(), "go-rod-test3", "Sorting by name is not working for folders")125 assertEquals(page.MustElement("table > tbody > tr:nth-child(3) > td.w-50 > span").MustText(), " storjlogo.jpeg", "Sorting by name is not working for files")126 // sort by size and date still left to do127 // Navigate into folders and use the breadcrumbs to navigate out128 page.MustElementR("a", "go-rod-test3").MustClick()129 page.MustElement("div.d-inline > a > a.path").MustClick()130 assertEquals(page.MustElement("table > tbody > tr:nth-child(1) > td.w-50 > span > span > a > a").MustText(), "go-rod-test3", "Unable to get to root of browser by way of breadcrumbs")131 // Single folder select132 page.MustElement("table > tbody > tr:nth-child(1)").MustClick()133 assertContains(page.MustElement("table > tbody > tr:nth-child(1)").String(), ".selected-row", "The clicked folder row has not been selected properly")134 // Multifolder select **CURRENTLY NOT WORKING**135 // page.Keyboard.Down(input.Shift)136 // page.MustElement("table > tbody > tr:nth-child(2)").MustClick()137 // assertContains(page.MustElement("table > tbody > tr:nth-child(2)").String(), ".selected-row", "The second clicked folder row has not been selected properly")138 // assertContains(page.MustElement("table > tbody > tr:nth-child(1)").String(), ".selected-row", "The clicked folder row has not been selected properly")139 // page.Keyboard.MustUp(input.Shift)140 // could use some tests for command select as well141 // Multifolder unselect142 page.MustElementR("p", "Buckets").MustClick()143 secondFolderSelectionStatus, _, _ := page.MustElement("table > tbody > tr:nth-child(1)").Has(".selected-row")144 assertEquals(fmt.Sprint(secondFolderSelectionStatus), "false", "Multiple selected folders were not unselected successfully")145 // Single file select146 page.MustElement("table > tbody > tr:nth-child(3)").MustClick()147 assertContains(page.MustElement("table > tbody > tr:nth-child(3)").String(), ".selected-row", "Single file select is not working properly")148 // Multifile select149 // Multifile unselect150 page.MustElementR("p", "Buckets").MustClick()151 firstFileSelectionStatus, _, _ := page.MustElement("table > tbody > tr:nth-child(3)").Has(".selected-row")152 assertEquals(fmt.Sprint(firstFileSelectionStatus), "false", "Multiple selected files were not unselected successfully")153 // Select file and folders154 // Delete a folder by clicking on hamburger155 page.MustElement("table > tbody > tr:nth-child(1) > td.text-right > div > div > button").MustClick()156 page.MustElement("table > tbody > tr:nth-child(1) > td.text-right > div > div > div > button").MustClick()157 page.MustElement("table > tbody > tr:nth-child(1) > td.text-right > div > div > div > div > div > button.dropdown-item.trash.p-3.action").MustClick()158 waitToEnd(2)159 assertEquals(page.MustElement("table > tbody > tr:nth-child(1) > td.w-50 > span > span > a > a").MustText(), "go-rod-test2", "Folder deletion by way of hamburger is not working")160 // Cancel folder deletion by way of hamburger161 // Delete a folder by selecting and clicking on trashcan162 // Cancel folder deletion by way of hamburger163 // Add a duplicate file and check naming convention164 page.MustElement("input[type=file]").SetFiles([]string{storjLogo})165 assertEquals(page.MustElementR("span", "storjlogo (1).jpeg").MustText(), " storjlogo (1).jpeg", "The duplicate file `storjlogo (1).jpeg` was not uploaded successfully")166 // Delete a file by clicking on the hamburger167 // Cancel file deletion by way of hamburger168 // Delete a file by clicking on the row and clicking on the trashcan169 // Cancel file deletion by way of hamburger170 // Delete multiple folders by selection171 // Delete multiple files by selection172 // Empty out entire folder173}174func main() {175 // Remove all of the contents within the output directory176 u := launcher.New().Bin("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome").MustLaunch()177 // testHome(u)178 // testLogin(u)...

Full Screen

Full Screen

SetFiles

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 page.SetFiles(proto.InputFile{4 }, proto.InputFile{5 })6 page.Element("input[type=file]").Upload("/Users/shubham/Downloads/1.jpg")7}8Recommended Posts: How to use SetFiles() method of Rod class in Python?9How to use SetFiles() method of Rod class in PHP?10How to use SetFiles() method of Rod class in C#?11How to use SetFiles() method of Rod class in C++?12How to use SetFiles() method of Rod class in C?13How to use SetFiles() method of Rod class in Java?14How to use SetFiles() method of Rod class in Ruby?15How to use SetFiles() method of Rod class in Swift?16How to use SetFiles() method of Rod class in Kotlin?17How to use SetFiles() method of Rod class in Scala?18How to use SetFiles() method of Rod class in Go?19How to use SetFiles() method of Rod class in Dart?20How to use SetFiles() method of Rod class in Rust?21How to use SetFiles() method of Rod class in Perl?22How to use SetFiles() method of Rod class in Groovy?23How to use SetFiles() method of Rod class in Objective-C?24How to use SetFiles() method of Rod class in Swift?25How to use SetFiles() method of Rod class in Visual Basic?26How to use SetFiles() method of Rod class in TypeScript?27How to use SetFiles() method of Rod class in Node.js?28How to use SetFiles() method of Rod class in JavaScript?29How to use SetFiles()

Full Screen

Full Screen

SetFiles

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 l := launcher.New().Headless(false)4 defer l.Cleanup()5 browser := rod.New().ControlURL(l).MustConnect()6 defer browser.MustClose()7 defer page.MustClose()8 fileInput := page.MustElement("input[type=file]")9 fileInput.MustSetFiles("file1.txt", "file2.txt")10 page.MustElement("input[type=submit]").MustClick()11 page.MustWaitLoad()12 fmt.Println(page.MustHTML())13}14import (15func main() {16 l := launcher.New().Headless(false)17 defer l.Cleanup()18 browser := rod.New().ControlURL(l).MustConnect()19 defer browser.MustClose()20 defer page.MustClose()21 fileInput := page.MustElement("input[type=file]")22 fileInput.MustSetFiles("file1.txt")23 page.MustElement("input[type=submit]").MustClick()24 page.MustWaitLoad()25 fmt.Println(page.MustHTML())26}27import (28func main() {29 l := launcher.New().Headless(false)30 defer l.Cleanup()31 browser := rod.New().ControlURL(l).MustConnect()32 defer browser.MustClose()33 defer page.MustClose()34 fileInput := page.MustElement("input[type=file]")35 fileInput.MustSetFiles("file1.txt")36 page.MustElement("input[type=submit

Full Screen

Full Screen

SetFiles

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 u := launcher.New().MustLaunch()4 browser := rod.New().ControlURL(u).MustConnect()5 page.MustSetFiles("#file", "file.txt")6 file := page.MustElement("#file").MustProperty("files").MustIndex(0).MustProperty("name").String()7 fmt.Println("File name is", file)8 browser.MustClose()9}

Full Screen

Full Screen

SetFiles

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().Connect()4 page := browser.Page("")5 page.SetFiles(proto.NetworkSetBypassServiceWorker{6 Files: map[string]string{7 },8 })9}10import (11func main() {12 browser := rod.New().Connect()13 page := browser.Page("")14 page.SetFiles(proto.NetworkSetBypassServiceWorker{15 Files: map[string]string{16 },17 })18}19import (20func main() {21 browser := rod.New().Connect()22 page := browser.Page("")23 page.SetFiles(proto.NetworkSetBypassServiceWorker{24 Files: map[string]string{25 },26 })27}28import (29func main() {30 browser := rod.New().Connect()31 page := browser.Page("")32 page.SetFiles(proto.NetworkSetBypassServiceWorker{33 Files: map[string]string{34 },35 })36}37import (

Full Screen

Full Screen

SetFiles

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().Connect()4 page := browser.Page("")5 input := page.Element("#lst-ib")6 input.Input("rod")7 input.Submit()8 page.WaitLoad()9 results := page.Elements(".g")10 fmt.Println(results.Text())11}12import (13func main() {14 browser := rod.New().Connect()15 page := browser.Page("")16 input := page.Element("#lst-ib")17 input.Input("rod")18 input.Submit()19 page.WaitLoad()20 results := page.Elements(".g")21 fmt.Println(results.Text())22}23import (24func main() {25 browser := rod.New().Connect()26 page := browser.Page("")27 input := page.Element("#lst-ib")28 input.Input("rod")29 input.Submit()30 page.WaitLoad()31 results := page.Elements(".g")32 fmt.Println(results.Text())33}

Full Screen

Full Screen

SetFiles

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().Connect()4 fmt.Println(page)5}6import (7func main() {8 browser := rod.New().Connect()9 fmt.Println(page)10}11import (12func main() {13 browser := rod.New().Connect()14 fmt.Println(page)15}16import (17func main() {18 browser := rod.New().Connect()19 fmt.Println(page)20}21import (22func main() {23 browser := rod.New().Connect()24 fmt.Println(page)25}26import (27func main() {28 browser := rod.New().Connect()29 fmt.Println(page)30}31import (32func main() {33 browser := rod.New().Connect()34 fmt.Println(page)35}36import (37func main() {38 browser := rod.New().Connect()39 fmt.Println(page)40}

Full Screen

Full Screen

SetFiles

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().Connect()4 page.SetFiles("#file", "C:/Users/PC/Downloads/1.txt")5 page.Element("#file").Click()6 fmt.Println("Done")7}

Full Screen

Full Screen

SetFiles

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 rod := rod.New()4 rod.SetFiles("test.txt", "test2.txt", "test3.txt")5 fmt.Println(rod.Files())6}7import (8func main() {9 rod := rod.New()10 rod.SetFiles("test.txt", "test2.txt", "test3.txt")11 fmt.Println(rod.Files())12}13import (14func main() {15 rod := rod.New()16 rod.SetFiles("test.txt", "test2.txt", "test3.txt")17 fmt.Println(rod.Files())18}19import (20func main() {21 rod := rod.New()22 rod.SetFiles("test.txt", "test2.txt", "test3.txt")23 fmt.Println(rod.Files())24}

Full Screen

Full Screen

SetFiles

Using AI Code Generation

copy

Full Screen

1import (2func main() {3import (4func main() {5import (6func main() {7import (8func main() {9import (10func main() {11import (12func main() {13import (14func main() {

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Rod automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful