How to use unsetJSCtxID method of rod Package

Best Rod code snippet using rod.unsetJSCtxID

page_eval.go

Source:page_eval.go Github

copy

Full Screen

...122 backoff = utils.BackoffSleeper(30*time.Millisecond, 3*time.Second, nil)123 } else {124 _ = backoff(p.ctx)125 }126 p.unsetJSCtxID()127 continue128 }129 return130 }131}132func (p *Page) evaluate(opts *EvalOptions) (*proto.RuntimeRemoteObject, error) {133 args, err := p.formatArgs(opts)134 if err != nil {135 return nil, err136 }137 req := proto.RuntimeCallFunctionOn{138 AwaitPromise: opts.AwaitPromise,139 ReturnByValue: opts.ByValue,140 UserGesture: opts.UserGesture,141 FunctionDeclaration: opts.formatToJSFunc(),142 Arguments: args,143 }144 if opts.ThisObj == nil {145 req.ObjectID, err = p.getJSCtxID()146 if err != nil {147 return nil, err148 }149 } else {150 req.ObjectID = opts.ThisObj.ObjectID151 }152 res, err := req.Call(p)153 if err != nil {154 return nil, err155 }156 if res.ExceptionDetails != nil {157 return nil, &ErrEval{res.ExceptionDetails}158 }159 return res.Result, nil160}161// Expose fn to the page's window object with the name. The exposure survives reloads.162// Call stop to unbind the fn.163func (p *Page) Expose(name string, fn func(gson.JSON) (interface{}, error)) (stop func() error, err error) {164 bind := "_" + utils.RandString(8)165 err = proto.RuntimeAddBinding{Name: bind}.Call(p)166 if err != nil {167 return168 }169 code := fmt.Sprintf(`(%s)("%s", "%s")`, js.ExposeFunc.Definition, name, bind)170 _, err = p.Evaluate(Eval(code))171 if err != nil {172 return173 }174 remove, err := p.EvalOnNewDocument(code)175 if err != nil {176 return177 }178 p, cancel := p.WithCancel()179 stop = func() error {180 defer cancel()181 err := remove()182 if err != nil {183 return err184 }185 return proto.RuntimeRemoveBinding{Name: bind}.Call(p)186 }187 go p.EachEvent(func(e *proto.RuntimeBindingCalled) {188 if e.Name == bind {189 payload := gson.NewFrom(e.Payload)190 res, err := fn(payload.Get("req"))191 code := fmt.Sprintf("(res, err) => %s(res, err)", payload.Get("cb").Str())192 _, _ = p.Evaluate(Eval(code, res, err))193 }194 })()195 return196}197func (p *Page) formatArgs(opts *EvalOptions) ([]*proto.RuntimeCallArgument, error) {198 formated := []*proto.RuntimeCallArgument{}199 for _, arg := range opts.JSArgs {200 if obj, ok := arg.(*proto.RuntimeRemoteObject); ok { // remote object201 formated = append(formated, &proto.RuntimeCallArgument{ObjectID: obj.ObjectID})202 } else if obj, ok := arg.(*js.Function); ok { // js helper203 id, err := p.ensureJSHelper(obj)204 if err != nil {205 return nil, err206 }207 formated = append(formated, &proto.RuntimeCallArgument{ObjectID: id})208 } else { // plain json data209 formated = append(formated, &proto.RuntimeCallArgument{Value: gson.New(arg)})210 }211 }212 return formated, nil213}214// Check the doc of EvalHelper215func (p *Page) ensureJSHelper(fn *js.Function) (proto.RuntimeRemoteObjectID, error) {216 jsCtxID, err := p.getJSCtxID()217 if err != nil {218 return "", err219 }220 fnID, has := p.getHelper(jsCtxID, js.Functions.Name)221 if !has {222 res, err := proto.RuntimeCallFunctionOn{223 ObjectID: jsCtxID,224 FunctionDeclaration: js.Functions.Definition,225 }.Call(p)226 if err != nil {227 return "", err228 }229 fnID = res.Result.ObjectID230 p.setHelper(jsCtxID, js.Functions.Name, fnID)231 }232 id, has := p.getHelper(jsCtxID, fn.Name)233 if !has {234 for _, dep := range fn.Dependencies {235 _, err := p.ensureJSHelper(dep)236 if err != nil {237 return "", err238 }239 }240 res, err := proto.RuntimeCallFunctionOn{241 ObjectID: jsCtxID,242 Arguments: []*proto.RuntimeCallArgument{{ObjectID: fnID}},243 FunctionDeclaration: fmt.Sprintf(244 // we only need the object id, but the cdp will return the whole function string.245 // So we override the toString to reduce the overhead.246 "functions => { const f = functions.%s = %s; f.toString = () => 'fn'; return f }",247 fn.Name, fn.Definition,248 ),249 }.Call(p)250 if err != nil {251 return "", err252 }253 id = res.Result.ObjectID254 p.setHelper(jsCtxID, fn.Name, id)255 }256 return id, nil257}258func (p *Page) getHelper(jsCtxID proto.RuntimeRemoteObjectID, name string) (proto.RuntimeRemoteObjectID, bool) {259 p.helpersLock.Lock()260 defer p.helpersLock.Unlock()261 if p.helpers == nil {262 p.helpers = map[proto.RuntimeRemoteObjectID]map[string]proto.RuntimeRemoteObjectID{}263 }264 list, ok := p.helpers[jsCtxID]265 if !ok {266 list = map[string]proto.RuntimeRemoteObjectID{}267 p.helpers[jsCtxID] = list268 }269 id, ok := list[name]270 return id, ok271}272func (p *Page) setHelper(jsCtxID proto.RuntimeRemoteObjectID, name string, fnID proto.RuntimeRemoteObjectID) {273 p.helpersLock.Lock()274 defer p.helpersLock.Unlock()275 p.helpers[jsCtxID][name] = fnID276}277// Returns the page's window object, the page can be an iframe278func (p *Page) getJSCtxID() (proto.RuntimeRemoteObjectID, error) {279 p.jsCtxLock.Lock()280 defer p.jsCtxLock.Unlock()281 if *p.jsCtxID != "" {282 return *p.jsCtxID, nil283 }284 if !p.IsIframe() {285 obj, err := proto.RuntimeEvaluate{Expression: "window"}.Call(p)286 if err != nil {287 return "", err288 }289 *p.jsCtxID = obj.Result.ObjectID290 p.helpersLock.Lock()291 p.helpers = nil292 p.helpersLock.Unlock()293 return *p.jsCtxID, nil294 }295 node, err := p.element.Describe(1, true)296 if err != nil {297 return "", err298 }299 obj, err := proto.DOMResolveNode{BackendNodeID: node.ContentDocument.BackendNodeID}.Call(p)300 if err != nil {301 return "", err302 }303 p.helpersLock.Lock()304 delete(p.helpers, *p.jsCtxID)305 p.helpersLock.Unlock()306 id, err := p.jsCtxIDByObjectID(obj.Object.ObjectID)307 *p.jsCtxID = id308 return *p.jsCtxID, err309}310func (p *Page) unsetJSCtxID() {311 p.jsCtxLock.Lock()312 defer p.jsCtxLock.Unlock()313 *p.jsCtxID = ""314}315func (p *Page) jsCtxIDByObjectID(id proto.RuntimeRemoteObjectID) (proto.RuntimeRemoteObjectID, error) {316 res, err := proto.RuntimeCallFunctionOn{317 ObjectID: id,318 FunctionDeclaration: `() => window`,319 }.Call(p)320 if err != nil {321 return "", err322 }323 return res.Result.ObjectID, nil324}...

Full Screen

Full Screen

unsetJSCtxID

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 l := launcher.New().Headless(false)4 defer l.Cleanup()5 url := l.MustLaunch()6 page.MustElement("input[name='q']").MustInput("rod").MustPress(input.Enter)7 page.MustElement("h3").MustClick()8 page.MustWaitLoad()9 log.Println("Page title is:", page.MustTitle())10 page.UnsetJSCtxID()11 page.MustEval(`() => {12 console.log("Hello World")13 }`)14}

Full Screen

Full Screen

unsetJSCtxID

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()4 page := browser.MustPage("")5 ctxID := page.MustEval(`() => {6 return window.__proto__.__proto__.__proto__.constructor.constructor("return this")().Symbol("context-id")7 }`).Int()8 page.UnsetJSCtxID(ctxID)9 page.MustEval(`() => {10 return window.__proto__.__proto__.__proto__.constructor.constructor("return this")().Symbol("context-id")11 }`)12}13import (14func main() {15 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()16 page := browser.MustPage("")17 ctxID := page.MustEval(`() => {18 return window.__proto__.__proto__.__proto__.constructor.constructor("return this")().Symbol("context-id")19 }`).Int()20 page.UnsetJSCtxID(ctxID)21 page.MustEval(`() => {22 return window.__proto__.__proto__.__proto__.constructor.constructor("return this")().Symbol("context-id")23 }`)24}25import (26func main() {27 browser := rod.New().ControlURL(launcher.New().MustLaunch()).MustConnect()28 page := browser.MustPage("")29 ctxID := page.MustEval(`() => {30 return window.__proto__.__proto__.__proto__.constructor.constructor("return this")().Symbol("context-id")31 }`).Int()

Full Screen

Full Screen

unsetJSCtxID

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().Connect()4 page := browser.Page("")5 page.UnsetJSCtxID()6}7import (8func main() {9 browser := rod.New().Connect()

Full Screen

Full Screen

unsetJSCtxID

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().Connect()4 page.UnsetJSCtxID()5 page.WaitLoad()6 title := page.Title()7 fmt.Println(title)8}9import (10func main() {11 browser := rod.New().Connect()12 page.UnsetJSCtxID()13 page.WaitLoad()14 title := page.Title()15 fmt.Println(title)16}17import (18func main() {19 browser := rod.New().Connect()20 page.UnsetJSCtxID()21 page.WaitLoad()22 title := page.Title()23 fmt.Println(title)24}25import (26func main() {27 browser := rod.New().Connect()28 page.UnsetJSCtxID()

Full Screen

Full Screen

unsetJSCtxID

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 browser := rod.New().MustConnect()4 page.UnsetJSCtxID()5 browser.Close()6}7github.com/go-rod/rod.(*Page).MustEval(0xc0000a6000, 0x5a1d7f, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)

Full Screen

Full Screen

unsetJSCtxID

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 la := launcher.New().Headless(false).MustLaunch()4 defer la.Kill()5 browser := proto.NewBrowser(proto.BrowserDriver(la))6 page := proto.NewPage(proto.PageDriver(browser))7 title, _ := page.GetTitle()8 fmt.Println(title)9 page.UnsetJSCtxID()10 title, _ = page.GetTitle()11 fmt.Println(title)12}13import (14func main() {15 la := launcher.New().Headless(false).MustLaunch()16 defer la.Kill()17 browser := proto.NewBrowser(proto.BrowserDriver(la))18 page := proto.NewPage(proto.PageDriver(browser))19 title, _ := page.GetTitle()20 fmt.Println(title)21 page.SetJSCtxID("1")22 title, _ = page.GetTitle()23 fmt.Println(title)24}25import (26func main() {27 la := launcher.New().Headless(false).MustLaunch()28 defer la.Kill()

Full Screen

Full Screen

unsetJSCtxID

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "github.com/go-rod/rod"3func main() {4 r := rod.New().UnsetJSCtxID()5 fmt.Println("Rod:", r)6}7import "fmt"8import "github.com/go-rod/rod"9func main() {10 r := rod.New().UnsetJSCtxID()11 fmt.Println("Rod:", r)12}13import "fmt"14import "github.com/go-rod/rod"15func main() {16 r := rod.New().UnsetJSCtxID()17 fmt.Println("Rod:", r)18}19import "fmt"20import "github.com/go-rod/rod"21func main() {22 r := rod.New().UnsetJSCtxID()23 fmt.Println("Rod:", r)24}25import "fmt"26import "github.com/go-rod/rod"27func main() {28 r := rod.New().UnsetJSCtxID()29 fmt.Println("Rod:", r)30}31import "fmt"32import "github.com/go-rod/rod"33func main() {34 r := rod.New().UnsetJSCtxID()35 fmt.Println("Rod:", r)36}37import "fmt"38import "github.com/go-rod/rod"39func main() {40 r := rod.New().UnsetJSCtxID()41 fmt.Println("Rod:", r)42}43import "fmt"44import "github.com/go-rod/rod"45func main() {46 r := rod.New().UnsetJSCtxID()47 fmt.Println("Rod:", r)48}

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