How to use GetResource method of rod Package

Best Rod code snippet using rod.GetResource

page_actions.go

Source:page_actions.go Github

copy

Full Screen

...38 case ActionSelectInput:39 err = p.SelectInputElement(act, outData)40 case ActionWaitLoad:41 err = p.WaitLoad(act, outData)42 case ActionGetResource:43 err = p.GetResource(act, outData)44 case ActionExtract:45 err = p.ExtractElement(act, outData)46 case ActionWaitEvent:47 err = p.WaitEvent(act, outData)48 case ActionFilesInput:49 err = p.FilesInput(act, outData)50 case ActionAddHeader:51 err = p.ActionAddHeader(act, outData)52 case ActionSetHeader:53 err = p.ActionSetHeader(act, outData)54 case ActionDeleteHeader:55 err = p.ActionDeleteHeader(act, outData)56 case ActionSetBody:57 err = p.ActionSetBody(act, outData)58 case ActionSetMethod:59 err = p.ActionSetMethod(act, outData)60 case ActionKeyboard:61 err = p.KeyboardAction(act, outData)62 case ActionDebug:63 err = p.DebugAction(act, outData)64 case ActionSleep:65 err = p.SleepAction(act, outData)66 case ActionWaitVisible:67 err = p.WaitVisible(act, outData)68 default:69 continue70 }71 if err != nil {72 return nil, errors.Wrap(err, "error occurred executing action")73 }74 }75 return outData, nil76}77type requestRule struct {78 Action ActionType79 Part string80 Args map[string]string81}82const elementDidNotAppearMessage = "Element did not appear in the given amount of time"83// WaitVisible waits until an element appears.84func (p *Page) WaitVisible(act *Action, out map[string]string) error {85 timeout, err := getTimeout(act)86 if err != nil {87 return errors.Wrap(err, "Wrong timeout given")88 }89 pollTime, err := getPollTime(act)90 if err != nil {91 return errors.Wrap(err, "Wrong polling time given")92 }93 element, _ := p.Sleeper(pollTime, timeout).94 Timeout(timeout).95 pageElementBy(act.Data)96 if element != nil {97 if err := element.WaitVisible(); err != nil {98 return errors.Wrap(err, elementDidNotAppearMessage)99 }100 } else {101 return errors.New(elementDidNotAppearMessage)102 }103 return nil104}105func (p *Page) Sleeper(pollTimeout, timeout time.Duration) *Page {106 page := *p107 page.page = page.Page().Sleeper(func() utils.Sleeper {108 return createBackOffSleeper(pollTimeout, timeout)109 })110 return &page111}112func (p *Page) Timeout(timeout time.Duration) *Page {113 page := *p114 page.page = page.Page().Timeout(timeout)115 return &page116}117func createBackOffSleeper(pollTimeout, timeout time.Duration) utils.Sleeper {118 backoffSleeper := utils.BackoffSleeper(pollTimeout, timeout, func(duration time.Duration) time.Duration {119 return duration120 })121 return func(ctx context.Context) error {122 if ctx.Err() != nil {123 return ctx.Err()124 }125 return backoffSleeper(ctx)126 }127}128func getTimeout(act *Action) (time.Duration, error) {129 return geTimeParameter(act, "timeout", 3, time.Second)130}131func getPollTime(act *Action) (time.Duration, error) {132 return geTimeParameter(act, "pollTime", 100, time.Millisecond)133}134func geTimeParameter(act *Action, parameterName string, defaultValue time.Duration, duration time.Duration) (time.Duration, error) {135 pollTimeString := act.GetArg(parameterName)136 if pollTimeString == "" {137 return defaultValue * duration, nil138 }139 timeout, err := strconv.Atoi(pollTimeString)140 if err != nil {141 return time.Duration(0), err142 }143 return time.Duration(timeout) * duration, nil144}145// ActionAddHeader executes a AddHeader action.146func (p *Page) ActionAddHeader(act *Action, out map[string]string /*TODO review unused parameter*/) error {147 in := act.GetArg("part")148 args := make(map[string]string)149 args["key"] = act.GetArg("key")150 args["value"] = act.GetArg("value")151 rule := requestRule{152 Action: ActionAddHeader,153 Part: in,154 Args: args,155 }156 p.rules = append(p.rules, rule)157 return nil158}159// ActionSetHeader executes a SetHeader action.160func (p *Page) ActionSetHeader(act *Action, out map[string]string /*TODO review unused parameter*/) error {161 in := act.GetArg("part")162 args := make(map[string]string)163 args["key"] = act.GetArg("key")164 args["value"] = act.GetArg("value")165 rule := requestRule{166 Action: ActionSetHeader,167 Part: in,168 Args: args,169 }170 p.rules = append(p.rules, rule)171 return nil172}173// ActionDeleteHeader executes a DeleteHeader action.174func (p *Page) ActionDeleteHeader(act *Action, out map[string]string /*TODO review unused parameter*/) error {175 in := act.GetArg("part")176 args := make(map[string]string)177 args["key"] = act.GetArg("key")178 rule := requestRule{179 Action: ActionDeleteHeader,180 Part: in,181 Args: args,182 }183 p.rules = append(p.rules, rule)184 return nil185}186// ActionSetBody executes a SetBody action.187func (p *Page) ActionSetBody(act *Action, out map[string]string /*TODO review unused parameter*/) error {188 in := act.GetArg("part")189 args := make(map[string]string)190 args["body"] = act.GetArg("body")191 rule := requestRule{192 Action: ActionSetBody,193 Part: in,194 Args: args,195 }196 p.rules = append(p.rules, rule)197 return nil198}199// ActionSetMethod executes an SetMethod action.200func (p *Page) ActionSetMethod(act *Action, out map[string]string /*TODO review unused parameter*/) error {201 in := act.GetArg("part")202 args := make(map[string]string)203 args["method"] = act.GetArg("method")204 rule := requestRule{205 Action: ActionSetMethod,206 Part: in,207 Args: args,208 }209 p.rules = append(p.rules, rule)210 return nil211}212// NavigateURL executes an ActionLoadURL actions loading a URL for the page.213func (p *Page) NavigateURL(action *Action, out map[string]string, parsed *url.URL /*TODO review unused parameter*/) error {214 URL := action.GetArg("url")215 if URL == "" {216 return errors.New("invalid arguments provided")217 }218 // Handle the dynamic value substitution here.219 URL, parsed = baseURLWithTemplatePrefs(URL, parsed)220 values := map[string]interface{}{"Hostname": parsed.Hostname()}221 if strings.HasSuffix(parsed.Path, "/") && strings.Contains(URL, "{{BaseURL}}/") {222 parsed.Path = strings.TrimSuffix(parsed.Path, "/")223 }224 parsedString := parsed.String()225 values["BaseURL"] = parsedString226 final := fasttemplate.ExecuteStringStd(URL, "{{", "}}", values)227 if err := p.page.Navigate(final); err != nil {228 return errors.Wrap(err, "could not navigate")229 }230 return nil231}232// RunScript runs a script on the loaded page233func (p *Page) RunScript(action *Action, out map[string]string) error {234 code := action.GetArg("code")235 if code == "" {236 return errors.New("invalid arguments provided")237 }238 if action.GetArg("hook") == "true" {239 if _, err := p.page.EvalOnNewDocument(code); err != nil {240 return err241 }242 }243 data, err := p.page.Eval(code)244 if err != nil {245 return err246 }247 if data != nil && action.Name != "" {248 out[action.Name] = data.Value.String()249 }250 return nil251}252// ClickElement executes click actions for an element.253func (p *Page) ClickElement(act *Action, out map[string]string /*TODO review unused parameter*/) error {254 element, err := p.pageElementBy(act.Data)255 if err != nil {256 return errors.Wrap(err, "could not get element")257 }258 if err = element.ScrollIntoView(); err != nil {259 return errors.Wrap(err, "could not scroll into view")260 }261 if err = element.Click(proto.InputMouseButtonLeft); err != nil {262 return errors.Wrap(err, "could not click element")263 }264 return nil265}266// KeyboardAction executes a keyboard action on the page.267func (p *Page) KeyboardAction(act *Action, out map[string]string /*TODO review unused parameter*/) error {268 return p.page.Keyboard.Press([]rune(act.GetArg("keys"))...)269}270// RightClickElement executes right click actions for an element.271func (p *Page) RightClickElement(act *Action, out map[string]string /*TODO review unused parameter*/) error {272 element, err := p.pageElementBy(act.Data)273 if err != nil {274 return errors.Wrap(err, "could not get element")275 }276 if err = element.ScrollIntoView(); err != nil {277 return errors.Wrap(err, "could not scroll into view")278 }279 if err = element.Click(proto.InputMouseButtonRight); err != nil {280 return errors.Wrap(err, "could not right click element")281 }282 return nil283}284// Screenshot executes screenshot action on a page285func (p *Page) Screenshot(act *Action, out map[string]string) error {286 to := act.GetArg("to")287 if to == "" {288 to = ksuid.New().String()289 if act.Name != "" {290 out[act.Name] = to291 }292 }293 var data []byte294 var err error295 if act.GetArg("fullpage") == "true" {296 data, err = p.page.Screenshot(true, &proto.PageCaptureScreenshot{})297 } else {298 data, err = p.page.Screenshot(false, &proto.PageCaptureScreenshot{})299 }300 if err != nil {301 return errors.Wrap(err, "could not take screenshot")302 }303 err = ioutil.WriteFile(to+".png", data, 0540)304 if err != nil {305 return errors.Wrap(err, "could not write screenshot")306 }307 return nil308}309// InputElement executes input element actions for an element.310func (p *Page) InputElement(act *Action, out map[string]string /*TODO review unused parameter*/) error {311 value := act.GetArg("value")312 if value == "" {313 return errors.New("invalid arguments provided")314 }315 element, err := p.pageElementBy(act.Data)316 if err != nil {317 return errors.Wrap(err, "could not get element")318 }319 if err = element.ScrollIntoView(); err != nil {320 return errors.Wrap(err, "could not scroll into view")321 }322 if err = element.Input(value); err != nil {323 return errors.Wrap(err, "could not input element")324 }325 return nil326}327// TimeInputElement executes time input on an element328func (p *Page) TimeInputElement(act *Action, out map[string]string /*TODO review unused parameter*/) error {329 value := act.GetArg("value")330 if value == "" {331 return errors.New("invalid arguments provided")332 }333 element, err := p.pageElementBy(act.Data)334 if err != nil {335 return errors.Wrap(err, "could not get element")336 }337 if err = element.ScrollIntoView(); err != nil {338 return errors.Wrap(err, "could not scroll into view")339 }340 t, err := time.Parse(time.RFC3339, value)341 if err != nil {342 return errors.Wrap(err, "could not parse time")343 }344 if err := element.InputTime(t); err != nil {345 return errors.Wrap(err, "could not input element")346 }347 return nil348}349// SelectInputElement executes select input statement action on a element350func (p *Page) SelectInputElement(act *Action, out map[string]string /*TODO review unused parameter*/) error {351 value := act.GetArg("value")352 if value == "" {353 return errors.New("invalid arguments provided")354 }355 element, err := p.pageElementBy(act.Data)356 if err != nil {357 return errors.Wrap(err, "could not get element")358 }359 if err = element.ScrollIntoView(); err != nil {360 return errors.Wrap(err, "could not scroll into view")361 }362 selectedBool := false363 if act.GetArg("selected") == "true" {364 selectedBool = true365 }366 by := act.GetArg("selector")367 if err := element.Select([]string{value}, selectedBool, selectorBy(by)); err != nil {368 return errors.Wrap(err, "could not select input")369 }370 return nil371}372// WaitLoad waits for the page to load373func (p *Page) WaitLoad(act *Action, out map[string]string /*TODO review unused parameter*/) error {374 p.page.Timeout(1 * time.Second).WaitNavigation(proto.PageLifecycleEventNameDOMContentLoaded)()375 // Wait for the window.onload event and also wait for the network requests376 // to become idle for a maximum duration of 2 seconds. If the requests377 // do not finish,378 if err := p.page.WaitLoad(); err != nil {379 return errors.Wrap(err, "could not reset mouse")380 }381 _ = p.page.WaitIdle(1 * time.Second)382 return nil383}384// GetResource gets a resource from an element from page.385func (p *Page) GetResource(act *Action, out map[string]string) error {386 element, err := p.pageElementBy(act.Data)387 if err != nil {388 return errors.Wrap(err, "could not get element")389 }390 resource, err := element.Resource()391 if err != nil {392 return errors.Wrap(err, "could not get src for element")393 }394 if act.Name != "" {395 out[act.Name] = string(resource)396 }397 return nil398}399// FilesInput acts with a file input element on page...

Full Screen

Full Screen

main.go

Source:main.go Github

copy

Full Screen

...42 for i, img := range images {43 log.Printf("Index %d", i)44 img_src := *img.MustAttribute("src")45 log.Println(img_src)46 bin, _ := page.GetResource(img_src)47 utils.OutputFile("./download/"+lastSlashOfString(img_src), bin)48 }49 }50 if _, err := os.Stat("download"); os.IsNotExist(err) {51 err := os.Mkdir("download", 0777)52 check(err)53 }54 f, err := os.Create("./download/" + pageName + ".html")55 check(err)56 defer f.Close()57 html := content.MustHTML()58 html = strings.ReplaceAll(html, "FONT-FAMILY: NanumBarunGothic;", "")59 html = strings.ReplaceAll(html, "rgb(116,116,116)", "rgb(0,0,0)")60 html = strings.ReplaceAll(html, "rgb(94,94,94)", "rgb(0,0,0)")...

Full Screen

Full Screen

GetResource

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().Connect()4 defer browser.Close()5 defer page.Close()6 page.WaitLoad()7 html, err := page.HTML()8 utils.E(err)9 fmt.Println(html)10}

Full Screen

Full Screen

GetResource

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().MustConnect()4 page := browser.MustPage("")5 page.MustSetViewport(1024, 768, 1, false)6 page.MustScreenshot("example.png")7 fmt.Println("Screenshot saved to example.png")8}9import (10func main() {11 browser := rod.New().MustConnect()12 page := browser.MustPage("")13 page.MustSetViewport(1024, 768, 1, false)14 page.MustElement("input").MustInput("Rod")15 page.MustElement("input").MustInput("{enter}")16 page.MustScreenshot("example.png")17 fmt.Println("Screenshot saved to example.png")18}19import (20func main() {21 browser := rod.New().MustConnect()22 page := browser.MustPage("")23 page.MustSetViewport(1024, 768, 1, false)24 page.MustElement("input").MustInput("Rod")25 page.MustElement("input").MustInput("{enter}")26 page.MustElement("h3").MustClick()27 page.MustScreenshot("example.png")28 fmt.Println("Screenshot saved to example.png")29}30import (31func main() {32 browser := rod.New().MustConnect()33 page := browser.MustPage("")34 page.MustSetViewport(1024, 768, 1, false)35 page.MustElement("input").MustInput("Rod")36 page.MustElement("input").MustInput("{enter}")37 page.MustElement("h3").MustClick()38 page.MustElement("p").MustClick()39 page.MustScreenshot("example.png")40 fmt.Println("Screenshot saved to example.png")41}42import (

Full Screen

Full Screen

GetResource

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

GetResource

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().MustConnect()4 fmt.Println(res.Status)5 fmt.Println(string(res.Body))6}

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