From 738de64cf699f3d7913f93d6e242da7b16881750 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 5 Nov 2024 15:09:55 -0500 Subject: [PATCH] Move WaitForEventOption parsing to mapping --- browser/browser_context_mapping.go | 50 ++++++++++++++++++++++--- browser/sync_browser_context_mapping.go | 10 ++--- common/browser_context_options.go | 43 --------------------- 3 files changed, 50 insertions(+), 53 deletions(-) diff --git a/browser/browser_context_mapping.go b/browser/browser_context_mapping.go index 6e0548269..34196dd2d 100644 --- a/browser/browser_context_mapping.go +++ b/browser/browser_context_mapping.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "reflect" + "time" "github.com/grafana/sobek" @@ -111,11 +112,11 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin }, "waitForEvent": func(event string, optsOrPredicate sobek.Value) (*sobek.Promise, error) { ctx := vu.Context() - popts := common.NewWaitForEventOptions( - bc.Timeout(), - ) - if err := popts.Parse(ctx, optsOrPredicate); err != nil { - return nil, fmt.Errorf("parsing waitForEvent options: %w", err) + rt := k6ext.Runtime(ctx) + + popts, err := parseWaitForEventOptions(rt, optsOrPredicate, bc.Timeout()) + if err != nil { + return nil, fmt.Errorf("parsing wait for event options: %w", err) } return k6ext.Promise(ctx, func() (result any, reason error) { @@ -207,3 +208,42 @@ func parseGrantPermissionOptions(ctx context.Context, opts sobek.Value) (common. return g, nil } + +// parseWaitForEventOptions parses optsOrPredicate into a WaitForEventOptions. +// It returns a WaitForEventOptions with the default timeout if optsOrPredicate is nil, +// or not a callable predicate function. +// It can parse only a callable predicate function or an object which contains a +// callable predicate function and a timeout. +func parseWaitForEventOptions( + rt *sobek.Runtime, optsOrPredicate sobek.Value, defaultTime time.Duration, +) (*common.WaitForEventOptions, error) { + w := &common.WaitForEventOptions{ + Timeout: defaultTime, + } + + if !sobekValueExists(optsOrPredicate) { + return w, nil + } + var isCallable bool + w.PredicateFn, isCallable = sobek.AssertFunction(optsOrPredicate) + if isCallable { + return w, nil + } + + opts := optsOrPredicate.ToObject(rt) + for _, k := range opts.Keys() { + switch k { + case "predicate": + w.PredicateFn, isCallable = sobek.AssertFunction(opts.Get(k)) + if !isCallable { + return nil, errors.New("predicate function is not callable") + } + case "timeout": + w.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond + default: + return nil, fmt.Errorf("unknown option: %s", k) + } + } + + return w, nil +} diff --git a/browser/sync_browser_context_mapping.go b/browser/sync_browser_context_mapping.go index 93c7708d0..75b8a8231 100644 --- a/browser/sync_browser_context_mapping.go +++ b/browser/sync_browser_context_mapping.go @@ -62,11 +62,11 @@ func syncMapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //n "setOffline": bc.SetOffline, "waitForEvent": func(event string, optsOrPredicate sobek.Value) (*sobek.Promise, error) { ctx := vu.Context() - popts := common.NewWaitForEventOptions( - bc.Timeout(), - ) - if err := popts.Parse(ctx, optsOrPredicate); err != nil { - return nil, fmt.Errorf("parsing waitForEvent options: %w", err) + rt := k6ext.Runtime(ctx) + + popts, err := parseWaitForEventOptions(rt, optsOrPredicate, bc.Timeout()) + if err != nil { + return nil, fmt.Errorf("parsing wait for event options: %w", err) } return k6ext.Promise(ctx, func() (result any, reason error) { diff --git a/common/browser_context_options.go b/common/browser_context_options.go index 32a86764c..705967862 100644 --- a/common/browser_context_options.go +++ b/common/browser_context_options.go @@ -2,7 +2,6 @@ package common import ( "context" - "errors" "fmt" "time" @@ -104,48 +103,6 @@ type WaitForEventOptions struct { PredicateFn sobek.Callable } -// NewWaitForEventOptions created a new instance of WaitForEventOptions with a -// default timeout. -func NewWaitForEventOptions(defaultTimeout time.Duration) *WaitForEventOptions { - return &WaitForEventOptions{ - Timeout: defaultTimeout, - } -} - -// Parse will parse the options or a callable predicate function. It can parse -// only a callable predicate function or an object which contains a callable -// predicate function and a timeout. -func (w *WaitForEventOptions) Parse(ctx context.Context, optsOrPredicate sobek.Value) error { - if !sobekValueExists(optsOrPredicate) { - return nil - } - - var ( - isCallable bool - rt = k6ext.Runtime(ctx) - ) - - w.PredicateFn, isCallable = sobek.AssertFunction(optsOrPredicate) - if isCallable { - return nil - } - - opts := optsOrPredicate.ToObject(rt) - for _, k := range opts.Keys() { - switch k { - case "predicate": - w.PredicateFn, isCallable = sobek.AssertFunction(opts.Get(k)) - if !isCallable { - return errors.New("predicate function is not callable") - } - case "timeout": //nolint:goconst - w.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond - } - } - - return nil -} - // GrantPermissionsOptions is used by BrowserContext.GrantPermissions. type GrantPermissionsOptions struct { Origin string