How to use ContinueRequest method of rod Package

Best Rod code snippet using rod.ContinueRequest

hijack.go

Source:hijack.go Github

copy

Full Screen

...171 Response *HijackResponse172 OnError func(error)173 // 跳过下一个 handler174 Skip bool175 continueRequest *proto.FetchContinueRequest176 // CustomState用于存储此context的内容177 CustomState interface{}178 browser *Browser179}180// ContinueRequest 不被劫持。RequestID将由router设置,你不需要设置它。181func (h *Hijack) ContinueRequest(cq *proto.FetchContinueRequest) {182 h.continueRequest = cq183}184// LoadResponse will send request to the real destination and load the response as default response to override.185// LoadResponse 将向实际目标发送请求,并将响应作为默认响应加载以覆盖。186func (h *Hijack) LoadResponse(client *http.Client, loadBody bool) error {187 res, err := client.Do(h.Request.req)188 if err != nil {189 return err190 }191 defer func() { _ = res.Body.Close() }()192 h.Response.payload.ResponseCode = res.StatusCode193 for k, vs := range res.Header {194 for _, v := range vs {195 h.Response.SetHeader(k, v)196 }197 }198 if loadBody {199 b, err := ioutil.ReadAll(res.Body)200 if err != nil {201 return err202 }203 h.Response.payload.Body = b204 }205 return nil206}207// HijackRequest context208type HijackRequest struct {209 event *proto.FetchRequestPaused210 req *http.Request211}212// Type of the resource213// 资源的类型214func (ctx *HijackRequest) Type() proto.NetworkResourceType {215 return ctx.event.ResourceType216}217// Method of the request218// 请求的方法219func (ctx *HijackRequest) Method() string {220 return ctx.event.Request.Method221}222// URL of the request223// 请求的URL224func (ctx *HijackRequest) URL() *url.URL {225 u, _ := url.Parse(ctx.event.Request.URL)226 return u227}228// Header via a key229// 通过key获得相应的请求头的值230func (ctx *HijackRequest) Header(key string) string {231 return ctx.event.Request.Headers[key].String()232}233// Headers of request234// 请求的请求头235func (ctx *HijackRequest) Headers() proto.NetworkHeaders {236 return ctx.event.Request.Headers237}238// Body of the request, devtools API doesn't support binary data yet, only string can be captured.239// 请求体,devtools API还不支持二进制数据,只能捕获字符串。240func (ctx *HijackRequest) Body() string {241 return ctx.event.Request.PostData242}243// JSONBody of the request244// 请求的JSONBody245func (ctx *HijackRequest) JSONBody() gson.JSON {246 return gson.NewFrom(ctx.Body())247}248// Req returns the underlaying http.Request instance that will be used to send the request.249// Req返回将用于发送请求的http.Request实例的底层。250func (ctx *HijackRequest) Req() *http.Request {251 return ctx.req252}253// SetContext of the underlaying http.Request instance254// 设置底层http.Request实例的上下文。255func (ctx *HijackRequest) SetContext(c context.Context) *HijackRequest {256 ctx.req = ctx.req.WithContext(c)257 return ctx258}259// SetBody of the request, if obj is []byte or string, raw body will be used, else it will be encoded as json.260// 设置请求的正文,如果obj是[]字节或字符串,将使用原始正文,否则将被编码为json。261func (ctx *HijackRequest) SetBody(obj interface{}) *HijackRequest {262 var b []byte263 switch body := obj.(type) {264 case []byte:265 b = body266 case string:267 b = []byte(body)268 default:269 b = utils.MustToJSONBytes(body)270 }271 ctx.req.Body = ioutil.NopCloser(bytes.NewBuffer(b))272 return ctx273}274// IsNavigation determines whether the request is a navigation request275// IsNavigation 确定请求是否是一个导航请求276func (ctx *HijackRequest) IsNavigation() bool {277 return ctx.Type() == proto.NetworkResourceTypeDocument278}279// HijackResponse context280type HijackResponse struct {281 payload *proto.FetchFulfillRequest282 fail *proto.FetchFailRequest283}284// Payload to respond the request from the browser.285// 来自浏览器请求响应的 payload286func (ctx *HijackResponse) Payload() *proto.FetchFulfillRequest {287 return ctx.payload288}289// Body of the payload290// playload 的主体291func (ctx *HijackResponse) Body() string {292 return string(ctx.payload.Body)293}294// Headers returns the clone of response headers.295// 返回响应头的克隆296// If you want to modify the response headers use HijackResponse.SetHeader .297// 如果想修改响应头请使用:HijackResponse.SetHeader298func (ctx *HijackResponse) Headers() http.Header {299 header := http.Header{}300 for _, h := range ctx.payload.ResponseHeaders {301 header.Add(h.Name, h.Value)302 }303 return header304}305// SetHeader of the payload via key-value pairs306// 通过键值对儿为 playload 设置响应头307func (ctx *HijackResponse) SetHeader(pairs ...string) *HijackResponse {308 for i := 0; i < len(pairs); i += 2 {309 ctx.payload.ResponseHeaders = append(ctx.payload.ResponseHeaders, &proto.FetchHeaderEntry{310 Name: pairs[i],311 Value: pairs[i+1],312 })313 }314 return ctx315}316// SetBody of the payload, if obj is []byte or string, raw body will be used, else it will be encoded as json.317// 设置有效载荷的主体,如果obj是[]字节或字符串,将使用原始主体,否则将被编码为json。318func (ctx *HijackResponse) SetBody(obj interface{}) *HijackResponse {319 switch body := obj.(type) {320 case []byte:321 ctx.payload.Body = body322 case string:323 ctx.payload.Body = []byte(body)324 default:325 ctx.payload.Body = utils.MustToJSONBytes(body)326 }327 return ctx328}329// Fail request330func (ctx *HijackResponse) Fail(reason proto.NetworkErrorReason) *HijackResponse {331 ctx.fail.ErrorReason = reason332 return ctx333}334// HandleAuth for the next basic HTTP authentication.335// HandleAuth用于下一次基本HTTP认证。336// It will prevent the popup that requires user to input user name and password.337// 它将阻止要求用户输入用户名和密码的弹出窗口。338// Ref: https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication339func (b *Browser) HandleAuth(username, password string) func() error {340 enable := b.DisableDomain("", &proto.FetchEnable{})341 disable := b.EnableDomain("", &proto.FetchEnable{342 HandleAuthRequests: true,343 })344 paused := &proto.FetchRequestPaused{}345 auth := &proto.FetchAuthRequired{}346 ctx, cancel := context.WithCancel(b.ctx)347 waitPaused := b.Context(ctx).WaitEvent(paused)348 waitAuth := b.Context(ctx).WaitEvent(auth)349 return func() (err error) {350 defer enable()351 defer disable()352 defer cancel()353 waitPaused()354 err = proto.FetchContinueRequest{355 RequestID: paused.RequestID,356 }.Call(b)357 if err != nil {358 return359 }360 waitAuth()361 err = proto.FetchContinueWithAuth{362 RequestID: auth.RequestID,363 AuthChallengeResponse: &proto.FetchAuthChallengeResponse{364 Response: proto.FetchAuthChallengeResponseResponseProvideCredentials,365 Username: username,366 Password: password,367 },368 }.Call(b)...

Full Screen

Full Screen

crawler.go

Source:crawler.go Github

copy

Full Screen

...22 URL: config.target.String(),23 Type: p.NetworkResourceTypeDocument,24 }25}26func (r *Request) continueRequest(id p.FetchRequestID) p.FetchContinueRequest {27 return p.FetchContinueRequest{28 RequestID: id,29 URL: r.URL,30 Method: r.Method,31 PostData: []byte(r.PostData),32 }33}34type Analyzer interface {35 Analyze(page *rod.Page, url string)36}37type Crawler struct {38 browser *rod.Browser39 allow []string40 limit chan struct{}41 wg sync.WaitGroup42 replays sync.Map43 documents sync.Map44 blocked sync.Map45}46func NewCrawler(browser *rod.Browser, limit int) *Crawler {47 go p.BrowserSetDownloadBehavior{Behavior: p.BrowserSetDownloadBehaviorBehaviorDeny}.Call(browser)48 crawler := &Crawler{49 browser: browser,50 limit: make(chan struct{}, limit),51 }52 go browser.EachEvent(func(e *p.FetchRequestPaused) {53 id := e.RequestID54 req := Request{55 Type: e.ResourceType,56 URL: e.Request.URL,57 Method: e.Request.Method,58 PostData: e.Request.PostData,59 }60 if e.ResourceType != p.NetworkResourceTypeDocument {61 go p.FetchContinueRequest{RequestID: id}.Call(browser)62 } else if replay, ok := crawler.replay(e.FrameID); ok {63 go replay.continueRequest(id).Call(browser)64 log.Println("[INFO] ", "Replay | Frame:", string(e.FrameID)[:6], replay)65 return66 } else {67 go p.FetchFailRequest{RequestID: id, ErrorReason: p.NetworkErrorReasonAborted}.Call(browser)68 crawler.Feed(&req)69 }70 })()71 return crawler72}73func (c *Crawler) Wait() {74 c.wg.Wait()75}...

Full Screen

Full Screen

ContinueRequest

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().MustConnect()4 defer browser.MustClose()5 page.MustElement("input.gLFyf.gsfi").MustInput("hello world")6 page.MustElement("input.gNO89b").MustPress("Enter")7 page.MustWaitLoad()8 url, err := page.URL()9 if err != nil {10 log.Fatal(err)11 }12 fmt.Println(url)13 title, err := page.Title()14 if err != nil {15 log.Fatal(err)16 }17 fmt.Println(title)18 html, err := page.HTML()19 if err != nil {20 log.Fatal(err)21 }22 fmt.Println(html)23 text, err := page.Text()24 if err != nil {25 log.Fatal(err)26 }27 fmt.Println(text)28}

Full Screen

Full Screen

ContinueRequest

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().MustConnect()4 page.MustElement("input[name=q]").MustInput("rod")5 page.MustElement("input[name=btnK]").MustClick()6 page.MustWaitLoad()7 page.MustWaitLoad()8 fmt.Println("Page title is: ", page.MustTitle())9}10import (11func main() {12 browser := rod.New().MustConnect()13 page.MustElement("input[name=q]").MustInput("rod")14 page.MustElement("input[name=btnK]").MustClick()15 page.MustWaitLoad()16 page.MustWaitLoad()17 fmt.Println("Page title is: ", page.MustTitle())18}19import (20func main() {21 browser := rod.New().MustConnect()22 page.MustElement("input[name=q]").MustInput("rod")23 page.MustElement("input[name=btnK]").MustClick()24 page.MustWaitLoad()25 page.MustWaitLoad()26 fmt.Println("Page title is: ", page.MustTitle())27}28import (29func main() {30 browser := rod.New().MustConnect()31 page.MustElement("input[name=q]").MustInput("rod")32 page.MustElement("input[name=btnK]").MustClick()33 page.MustWaitLoad()34 page.MustWaitLoad()35 fmt.Println("Page title is: ", page.MustTitle())36}

Full Screen

Full Screen

ContinueRequest

Using AI Code Generation

copy

Full Screen

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

Full Screen

Full Screen

ContinueRequest

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().Connect()4 fmt.Println(page.Element("input[name=q]").MustAttribute("name"))5}6import (7func main() {8 browser := rod.New().Connect()9 fmt.Println(page.Element("input[name=search_query]").MustAttribute("name"))10}11import (12func main() {13 browser := rod.New().Connect()14 page.NavigateBack()15 fmt.Println(page.Element("input[name=q]").MustAttribute("name"))16}17import (18func main() {19 browser := rod.New().Connect()20 page.NavigateBack()21 page.NavigateForward()22 fmt.Println(page.Element("input[name=search_query]").MustAttribute("name"))23}24import (25func main() {26 browser := rod.New().Connect()27 fmt.Println(page.Element("input[name=q]").MustAttribute("name"))28 page.Reload()29 fmt.Println(page.Element("input[name=q]").MustAttribute("name"))30}31import (32func main() {33 browser := rod.New().Connect()

Full Screen

Full Screen

ContinueRequest

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().MustConnect()4 page.MustSetRequestInterception(true).OnRequestIntercepted(func(i *proto.NetworkRequestIntercepted) {5 i.ContinueRequest(proto.NetworkContinueRequest{6 })7 } else {8 i.ContinueRequest(proto.NetworkContinueRequest{})9 }10 })11 title := page.MustEval(`() => document.title`).String()12 fmt.Println(title)13}

Full Screen

Full Screen

ContinueRequest

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 bin, _ := launcher.NewUserMode().Bin().Headless(false).Launch()4 fmt.Println(bin)5 })6 page.WaitLoad()7 fmt.Println(page.MustTitle())8}9import (10func main() {11 bin, _ := launcher.NewUserMode().Bin().Headless(false).Launch()12 fmt.Println(bin)13 })14 page.WaitLoad()15 fmt.Println(page.MustTitle())16}17import (18func main() {19 bin, _ := launcher.NewUserMode().Bin().Headless(false).Launch()20 fmt.Println(bin)

Full Screen

Full Screen

ContinueRequest

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser, err := rod.NewBrowser()4 if err != nil {5 log.Fatal(err)6 }7 defer browser.Close()8 page, err := browser.NewPage()9 if err != nil {10 log.Fatal(err)11 }12 if err != nil {13 log.Fatal(err)14 }15 title, err := page.Title()16 if err != nil {17 log.Fatal(err)18 }19 fmt.Println(title)20 html, err := page.HTML()21 if err != nil {22 log.Fatal(err)23 }24 fmt.Println(html)25 url, err := page.URL()26 if err != nil {27 log.Fatal(err)28 }29 fmt.Println(url)30 cookies, err := page.Cookies()31 if err != nil {32 log.Fatal(err)33 }34 fmt.Println(cookies)35 if err != nil {36 log.Fatal(err)37 }38 fmt.Println(cookies)39 if err != nil {40 log.Fatal(err)41 }42 fmt.Println(cookies)43 if err != nil {44 log.Fatal(err)45 }46 fmt.Println(cookies)47 if err != nil {48 log.Fatal(err)49 }50 fmt.Println(cookies)

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