github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/html/wait_navigation.go (about) 1 package html 2 3 import ( 4 "context" 5 6 "github.com/pkg/errors" 7 8 "github.com/MontFerret/ferret/pkg/drivers" 9 "github.com/MontFerret/ferret/pkg/runtime/core" 10 "github.com/MontFerret/ferret/pkg/runtime/values" 11 "github.com/MontFerret/ferret/pkg/runtime/values/types" 12 ) 13 14 type WaitNavigationParams struct { 15 TargetURL values.String 16 Timeout values.Int 17 Frame drivers.HTMLDocument 18 } 19 20 // WAIT_NAVIGATION waits for a given page to navigate to a new url. 21 // Stops the execution until the navigation ends or operation times out. 22 // @param {HTMLPage} page - Target page. 23 // @param {Int} [timeout=5000] - Navigation timeout. 24 // @param {Object} [params=None] - Navigation parameters. 25 // @param {Int} [params.timeout=5000] - Navigation timeout. 26 // @param {String} [params.target] - Navigation target url. 27 // @param {HTMLDocument} [params.frame] - Navigation frame. 28 func WaitNavigation(ctx context.Context, args ...core.Value) (core.Value, error) { 29 err := core.ValidateArgs(args, 1, 2) 30 31 if err != nil { 32 return values.None, err 33 } 34 35 doc, err := drivers.ToPage(args[0]) 36 37 if err != nil { 38 return values.None, err 39 } 40 41 var params WaitNavigationParams 42 43 if len(args) > 1 { 44 p, err := parseWaitNavigationParams(args[1]) 45 46 if err != nil { 47 return values.None, err 48 } 49 50 params = p 51 } else { 52 params = defaultWaitNavigationParams() 53 } 54 55 ctx, fn := waitTimeout(ctx, params.Timeout) 56 defer fn() 57 58 if params.Frame == nil { 59 return values.True, doc.WaitForNavigation(ctx, params.TargetURL) 60 } 61 62 return values.True, doc.WaitForFrameNavigation(ctx, params.Frame, params.TargetURL) 63 } 64 65 func parseWaitNavigationParams(arg core.Value) (WaitNavigationParams, error) { 66 params := defaultWaitNavigationParams() 67 err := core.ValidateType(arg, types.Int, types.Object) 68 69 if err != nil { 70 return params, err 71 } 72 73 if arg.Type() == types.Int { 74 params.Timeout = arg.(values.Int) 75 } else { 76 obj := arg.(*values.Object) 77 78 if v, exists := obj.Get("timeout"); exists { 79 err := core.ValidateType(v, types.Int) 80 81 if err != nil { 82 return params, errors.Wrap(err, "navigation parameters: timeout") 83 } 84 85 params.Timeout = v.(values.Int) 86 } 87 88 if v, exists := obj.Get("target"); exists { 89 err := core.ValidateType(v, types.String) 90 91 if err != nil { 92 return params, errors.Wrap(err, "navigation parameters: url") 93 } 94 95 params.TargetURL = v.(values.String) 96 } 97 98 if v, exists := obj.Get("frame"); exists { 99 doc, err := drivers.ToDocument(v) 100 101 if err != nil { 102 return params, errors.Wrap(err, "navigation parameters: frame") 103 } 104 105 params.Frame = doc 106 } 107 } 108 109 return params, nil 110 } 111 112 func defaultWaitNavigationParams() WaitNavigationParams { 113 return WaitNavigationParams{ 114 TargetURL: "", 115 Timeout: values.NewInt(drivers.DefaultWaitTimeout), 116 } 117 }