From df02a1dfa40c028a778cb5ba7e6cdf1902f31a66 Mon Sep 17 00:00:00 2001 From: Anupriya Kumari Date: Tue, 21 Oct 2025 22:44:58 +0000 Subject: [PATCH 1/2] Moved Element options and some Frame options to mapping layer --- .../browser/browser/element_handle_mapping.go | 427 ++++++++++++++++-- .../k6/browser/browser/frame_mapping.go | 290 ++++++++++-- .../k6/browser/browser/locator_mapping.go | 46 +- .../js/modules/k6/browser/browser/mapping.go | 14 - .../k6/browser/browser/page_mapping.go | 51 +-- .../browser/common/element_handle_options.go | 275 ----------- .../k6/browser/common/frame_options.go | 95 ---- 7 files changed, 699 insertions(+), 499 deletions(-) diff --git a/internal/js/modules/k6/browser/browser/element_handle_mapping.go b/internal/js/modules/k6/browser/browser/element_handle_mapping.go index b3ae85f534..5a40f4101f 100644 --- a/internal/js/modules/k6/browser/browser/element_handle_mapping.go +++ b/internal/js/modules/k6/browser/browser/element_handle_mapping.go @@ -1,15 +1,31 @@ package browser import ( + "context" "errors" "fmt" + "strings" + "time" "github.com/grafana/sobek" "go.k6.io/k6/internal/js/modules/k6/browser/common" "go.k6.io/k6/internal/js/modules/k6/browser/k6ext" + k6common "go.k6.io/k6/js/common" ) +const ( + optionButton = "button" + optionClickCount = "clickCount" + optionDelay = "delay" + optionModifiers = "modifiers" +) + +var imageFormatToID = map[string]common.ImageFormat{ //nolint:gochecknoglobals + "jpeg": common.ImageFormatJPEG, + "png": common.ImageFormatPNG, +} + // mapElementHandle to the JS module. func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint:gocognit,funlen,cyclop rt := vu.Runtime() @@ -26,8 +42,8 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: }) }, "check": func(opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleSetCheckedOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseElementHandleSetCheckedOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing check options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -35,11 +51,10 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: }), nil }, "click": func(opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleClickOptions(eh.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseElementHandleClickOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing element click options: %w", err) } - return k6ext.Promise(vu.Context(), func() (any, error) { err := eh.Click(popts) return nil, err //nolint:wrapcheck @@ -55,8 +70,8 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: }) }, "dblclick": func(opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleDblclickOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseElementHandleDblclickOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing element double click options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -69,8 +84,8 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: }) }, "fill": func(value string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleBaseOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseElementHandleBaseOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing element fill options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -95,8 +110,8 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: }) }, "hover": func(opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleHoverOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseElementHandleHoverOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing element hover options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -114,8 +129,8 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: }) }, "inputValue": func(opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleBaseOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseElementHandleBaseOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing element input value options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -162,8 +177,8 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: }) }, "press": func(key string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandlePressOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseElementHandlePressOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing press %q options: %w", key, err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -171,11 +186,10 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: }), nil }, "screenshot": func(opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleScreenshotOptions(eh.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseElementHandleScreenshotOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing element handle screenshot options: %w", err) } - return k6ext.Promise(vu.Context(), func() (any, error) { bb, err := eh.Screenshot(popts, vu.filePersister) if err != nil { @@ -188,8 +202,8 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: }), nil }, "scrollIntoViewIfNeeded": func(opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleBaseOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseElementHandleBaseOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing scrollIntoViewIfNeeded options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -201,8 +215,8 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: if err != nil { return nil, fmt.Errorf("parsing select options values: %w", err) } - popts := common.NewElementHandleBaseOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseElementHandleBaseOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing selectOption options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -210,8 +224,8 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: }), nil }, "selectText": func(opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleBaseOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseElementHandleBaseOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing selectText options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -219,8 +233,8 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: }), nil }, "setChecked": func(checked bool, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleSetCheckedOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseElementHandleSetCheckedOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing setChecked options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -228,8 +242,8 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: }), nil }, "setInputFiles": func(files sobek.Value, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleSetInputFilesOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseElementHandleSetInputFilesOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing setInputFiles options: %w", err) } var pfiles common.Files @@ -241,8 +255,8 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: }), nil }, "tap": func(opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleTapOptions(eh.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseElementHandleTapOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing element tap options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -262,8 +276,8 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: }) }, "type": func(text string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleTypeOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseElementHandleTypeOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing type options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -271,8 +285,8 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: }), nil }, "uncheck": func(opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleSetCheckedOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseElementHandleSetCheckedOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing uncheck options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -280,8 +294,8 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: }), nil }, "waitForElementState": func(state string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewElementHandleWaitForElementStateOptions(eh.DefaultTimeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseElementHandleWaitForElementStateOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing waitForElementState options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -341,3 +355,344 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: return maps } + +// parseElementHandleBaseOptions parses ElementHandleBaseOptions from opts +func parseElementHandleBaseOptions(ctx context.Context, opts sobek.Value) (*common.ElementHandleBaseOptions, error) { + if k6common.IsNullish(opts) { + return nil, nil + } + + parsed := &common.ElementHandleBaseOptions{ + Force: false, + NoWaitAfter: false, + Timeout: 0, + } + rt := k6ext.Runtime(ctx) + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + switch k { + case "force": + parsed.Force = obj.Get(k).ToBoolean() + case "noWaitAfter": + parsed.NoWaitAfter = obj.Get(k).ToBoolean() + case "timeout": + parsed.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond + } + } + + return parsed, nil +} + +// parseElementHandleBasePointerOptions parses ElementHandleBasePointerOptions from opts +func parseElementHandleBasePointerOptions( + ctx context.Context, + opts sobek.Value, +) (*common.ElementHandleBasePointerOptions, error) { + o := &common.ElementHandleBasePointerOptions{} + + baseOpts, err := parseElementHandleBaseOptions(ctx, opts) + if err != nil { + return nil, err + } + if baseOpts != nil { + o.ElementHandleBaseOptions = *baseOpts + } + + rt := k6ext.Runtime(ctx) + if !k6common.IsNullish(opts) { + optsObj := opts.ToObject(rt) + for _, k := range optsObj.Keys() { + switch k { + case "position": + var p map[string]float64 + o.Position = &common.Position{} + if rt.ExportTo(optsObj.Get(k), &p) == nil { + o.Position.X = p["x"] + o.Position.Y = p["y"] + } + case "trial": + o.Trial = optsObj.Get(k).ToBoolean() + } + } + } + + return o, nil +} + +// parseElementHandleSetInputFilesOptions parses ElementHandleSetInputFilesOptions from opts. +func parseElementHandleSetInputFilesOptions( + ctx context.Context, + opts sobek.Value, +) (*common.ElementHandleSetInputFilesOptions, error) { + baseOpts, err := parseElementHandleBaseOptions(ctx, opts) + if err != nil { + return nil, err + } + if baseOpts == nil { + return nil, nil + } + + o := &common.ElementHandleSetInputFilesOptions{ + ElementHandleBaseOptions: *baseOpts, + } + + return o, nil +} + +// parseElementHandleClickOptions parses ElementHandleClickOptions from opts +func parseElementHandleClickOptions(ctx context.Context, opts sobek.Value) (*common.ElementHandleClickOptions, error) { + if k6common.IsNullish(opts) { + return nil, nil + } + basePointer, err := parseElementHandleBasePointerOptions(ctx, opts) + if err != nil { + return nil, err + } + + o := &common.ElementHandleClickOptions{} + if basePointer != nil { + o.ElementHandleBasePointerOptions = *basePointer + } + rt := k6ext.Runtime(ctx) + optsObj := opts.ToObject(rt) + for _, k := range optsObj.Keys() { + switch k { + case optionButton: + o.Button = optsObj.Get(k).String() + case optionClickCount: + o.ClickCount = optsObj.Get(k).ToInteger() + case optionDelay: + o.Delay = optsObj.Get(k).ToInteger() + case optionModifiers: + var m []string + if err := rt.ExportTo(optsObj.Get(k), &m); err == nil { + o.Modifiers = m + } + } + } + + return o, nil +} + +// parseElementHandleDblclickOptions parses ElementHandleDblclickOptions from opts +func parseElementHandleDblclickOptions( + ctx context.Context, + opts sobek.Value, +) (*common.ElementHandleDblclickOptions, error) { + o := &common.ElementHandleDblclickOptions{} + + // Parse base pointer options first, propagating error if any + basePointer, err := parseElementHandleBasePointerOptions(ctx, opts) + if err != nil { + return nil, err + } + if basePointer != nil { + o.ElementHandleBasePointerOptions = *basePointer + } + rt := k6ext.Runtime(ctx) + if !k6common.IsNullish(opts) { + optsObj := opts.ToObject(rt) + for _, k := range optsObj.Keys() { + switch k { + case "button": + o.Button = optsObj.Get(k).String() + case "delay": + o.Delay = optsObj.Get(k).ToInteger() + case "modifiers": + var m []string + if err := rt.ExportTo(optsObj.Get(k), &m); err != nil { + return nil, err + } + o.Modifiers = m + } + } + } + + return o, nil +} + +// parseElementHandleHoverOptions parses ElementHandleHoverOptions from opts +func parseElementHandleHoverOptions(ctx context.Context, opts sobek.Value) (*common.ElementHandleHoverOptions, error) { + o := &common.ElementHandleHoverOptions{} + + basePointer, err := parseElementHandleBasePointerOptions(ctx, opts) + if err != nil { + return nil, err + } + if basePointer != nil { + o.ElementHandleBasePointerOptions = *basePointer + } + rt := k6ext.Runtime(ctx) + if !k6common.IsNullish(opts) { + optsObj := opts.ToObject(rt) + for _, k := range optsObj.Keys() { + if k == "modifiers" { + var m []string + if err := rt.ExportTo(optsObj.Get(k), &m); err != nil { + return nil, err + } + o.Modifiers = m + } + } + } + + return o, nil +} + +// parseElementHandleSetCheckedOptions parses ElementHandleSetCheckedOptions from opts. +func parseElementHandleSetCheckedOptions( + ctx context.Context, + opts sobek.Value, +) (*common.ElementHandleSetCheckedOptions, error) { + o := &common.ElementHandleSetCheckedOptions{} + + basePointerOpts, err := parseElementHandleBasePointerOptions(ctx, opts) + if err != nil { + return nil, err + } + if basePointerOpts != nil { + o.ElementHandleBasePointerOptions = *basePointerOpts + } + rt := k6ext.Runtime(ctx) + if !k6common.IsNullish(opts) { + optsObj := opts.ToObject(rt) + for _, k := range optsObj.Keys() { + if k == "strict" { + o.Strict = optsObj.Get(k).ToBoolean() + } + } + } + + return o, nil +} + +// parseElementHandleTapOptions parses ElementHandleTapOptions from opts. +func parseElementHandleTapOptions(ctx context.Context, opts sobek.Value) (*common.ElementHandleTapOptions, error) { + o := &common.ElementHandleTapOptions{} + + basePointerOpts, err := parseElementHandleBasePointerOptions(ctx, opts) + if err != nil { + return nil, err + } + if basePointerOpts != nil { + o.ElementHandleBasePointerOptions = *basePointerOpts + } + + rt := k6ext.Runtime(ctx) + if !k6common.IsNullish(opts) { + optsObj := opts.ToObject(rt) + for _, k := range optsObj.Keys() { + if k == "modifiers" { + var m []string + if err := rt.ExportTo(optsObj.Get(k), &m); err != nil { + return nil, err + } + o.Modifiers = m + } + } + } + + return o, nil +} + +// parseElementHandlePressOptions parses ElementHandlePressOptions from opts. +func parseElementHandlePressOptions(ctx context.Context, opts sobek.Value) (*common.ElementHandlePressOptions, error) { + o := &common.ElementHandlePressOptions{} + rt := k6ext.Runtime(ctx) + if !k6common.IsNullish(opts) { + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + switch k { + case "delay": + o.Delay = obj.Get(k).ToInteger() + case "noWaitAfter": + o.NoWaitAfter = obj.Get(k).ToBoolean() + case "timeout": + o.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond + } + } + } + + return o, nil +} + +// parseElementHandleScreenshotOptions parses ElementHandleScreenshotOptions from opts. +func parseElementHandleScreenshotOptions( + ctx context.Context, + opts sobek.Value, +) (*common.ElementHandleScreenshotOptions, error) { + o := &common.ElementHandleScreenshotOptions{} + + if k6common.IsNullish(opts) { + return o, nil + } + rt := k6ext.Runtime(ctx) + obj := opts.ToObject(rt) + formatSpecified := false + for _, k := range obj.Keys() { + switch k { + case "omitBackground": + o.OmitBackground = obj.Get(k).ToBoolean() + case "path": + o.Path = obj.Get(k).String() + case "quality": + o.Quality = obj.Get(k).ToInteger() + case "type": + if f, ok := imageFormatToID[obj.Get(k).String()]; ok { + o.Format = f + formatSpecified = true + } + case "timeout": + o.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond + } + } + + // Infer file format by path if not specified explicitly (default PNG) + if o.Path != "" && !formatSpecified { + if strings.HasSuffix(o.Path, ".jpg") || strings.HasSuffix(o.Path, ".jpeg") { + o.Format = common.ImageFormatJPEG + } + } + + return o, nil +} + +// parseElementHandleTypeOptions parses ElementHandleTypeOptions from opts. +func parseElementHandleTypeOptions(ctx context.Context, opts sobek.Value) (*common.ElementHandleTypeOptions, error) { + o := &common.ElementHandleTypeOptions{} + rt := k6ext.Runtime(ctx) + if !k6common.IsNullish(opts) { + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + switch k { + case "delay": + o.Delay = obj.Get(k).ToInteger() + case "noWaitAfter": + o.NoWaitAfter = obj.Get(k).ToBoolean() + case "timeout": + o.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond + } + } + } + + return o, nil +} + +// parseElementHandleWaitForElementStateOptions parses ElementHandleWaitForElementStateOptions from opts. +func parseElementHandleWaitForElementStateOptions( + ctx context.Context, + opts sobek.Value, +) (*common.ElementHandleWaitForElementStateOptions, error) { + o := &common.ElementHandleWaitForElementStateOptions{} + rt := k6ext.Runtime(ctx) + if !k6common.IsNullish(opts) { + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + if k == "timeout" { + o.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond + } + } + } + + return o, nil +} diff --git a/internal/js/modules/k6/browser/browser/frame_mapping.go b/internal/js/modules/k6/browser/browser/frame_mapping.go index 072c5af7d3..9fdea769cd 100644 --- a/internal/js/modules/k6/browser/browser/frame_mapping.go +++ b/internal/js/modules/k6/browser/browser/frame_mapping.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "time" "github.com/grafana/sobek" "github.com/mstoykov/k6-taskqueue-lib/taskqueue" @@ -30,8 +31,8 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { rt := vu.Runtime() maps := mapping{ "check": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameCheckOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFrameCheckOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing new frame check options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -49,7 +50,7 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { return mcfs }, "click": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts, err := parseFrameClickOptions(vu.Context(), opts, f.Timeout()) + popts, err := parseFrameClickOptions(vu.Context(), opts) if err != nil { return nil, err } @@ -65,8 +66,8 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { }) }, "dblclick": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameDblClickOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFrameDblClickOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing double click options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -108,8 +109,8 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { }), nil }, "fill": func(selector, value string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameFillOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFrameFillOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing fill options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -231,8 +232,8 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { }), nil }, "hover": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameHoverOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFrameHoverOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing hover options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -332,8 +333,8 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { return mapFrame(vu, f.ParentFrame()) }, "press": func(selector, key string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFramePressOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFramePressOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parse press options of selector %q on key %q: %w", selector, key, err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -341,11 +342,10 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { }), nil }, "selectOption": func(selector string, values sobek.Value, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameSelectOptionOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFrameSelectOptionOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing select option options: %w", err) } - convValues, err := common.ConvertSelectOptionValues(rt, values) if err != nil { return nil, fmt.Errorf("parsing select options values: %w", err) @@ -356,11 +356,10 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { }), nil }, "setChecked": func(selector string, checked bool, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameCheckOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFrameCheckOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing frame set check options: %w", err) } - return k6ext.Promise(vu.Context(), func() (any, error) { return nil, f.SetChecked(selector, checked, popts) //nolint:wrapcheck }), nil @@ -375,11 +374,10 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { }), nil }, "setInputFiles": func(selector string, files sobek.Value, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameSetInputFilesOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFrameSetInputFilesOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing setInputFiles options: %w", err) } - pfiles := new(common.Files) if err := pfiles.Parse(vu.Context(), files); err != nil { return nil, fmt.Errorf("parsing setInputFiles parameter: %w", err) @@ -390,8 +388,8 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { }), nil }, "tap": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameTapOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFrameTapOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing frame tap options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -421,21 +419,19 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { }) }, "type": func(selector, text string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameTypeOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFrameTypeOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing type options: %w", err) } - return k6ext.Promise(vu.Context(), func() (any, error) { return nil, f.Type(selector, text, popts) //nolint:wrapcheck }), nil }, "uncheck": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameUncheckOptions(f.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFrameUncheckOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing frame uncheck options %q: %w", selector, err) } - return k6ext.Promise(vu.Context(), func() (any, error) { return nil, f.Uncheck(selector, popts) //nolint:wrapcheck }), nil @@ -524,3 +520,241 @@ func mapFrame(vu moduleVU, f *common.Frame) mapping { return maps } + +func parseStrict(ctx context.Context, opts sobek.Value) bool { + var strict bool + + rt := k6ext.Runtime(ctx) + if !k6common.IsNullish(opts) { + opts := opts.ToObject(rt) + for _, k := range opts.Keys() { + if k == "strict" { + strict = opts.Get(k).ToBoolean() + } + } + } + + return strict +} + +// parseFrameCheckOptions parses FrameCheckOptions from opts. +func parseFrameCheckOptions(ctx context.Context, opts sobek.Value) (*common.FrameCheckOptions, error) { + basePointerOpts, err := parseElementHandleBasePointerOptions(ctx, opts) + if err != nil { + return nil, err + } + + o := &common.FrameCheckOptions{ + Strict: parseStrict(ctx, opts), + } + if basePointerOpts != nil { + o.ElementHandleBasePointerOptions = *basePointerOpts + } + + return o, nil +} + +// parseFrameClickOptions parses FrameClickOptions from opts. +func parseFrameClickOptions(ctx context.Context, opts sobek.Value) (*common.FrameClickOptions, error) { + clickOpts, err := parseElementHandleClickOptions(ctx, opts) + if err != nil { + return nil, err + } + + o := &common.FrameClickOptions{ + Strict: parseStrict(ctx, opts), + } + if clickOpts != nil { + o.ElementHandleClickOptions = *clickOpts + } + + return o, nil +} + +// parseFrameDblClickOptions parses FrameDblClickOptions from opts. +func parseFrameDblClickOptions(ctx context.Context, opts sobek.Value) (*common.FrameDblclickOptions, error) { + dblclickOpts, err := parseElementHandleDblclickOptions(ctx, opts) + if err != nil { + return nil, err + } + + o := &common.FrameDblclickOptions{ + Strict: parseStrict(ctx, opts), + } + if dblclickOpts != nil { + o.ElementHandleDblclickOptions = *dblclickOpts + } + + return o, nil +} + +// parseFrameFillOptions parses FrameFillOptions from opts. +func parseFrameFillOptions(ctx context.Context, opts sobek.Value) (*common.FrameFillOptions, error) { + baseOpts, err := parseElementHandleBaseOptions(ctx, opts) + if err != nil { + return nil, err + } + + o := &common.FrameFillOptions{ + Strict: parseStrict(ctx, opts), + } + if baseOpts != nil { + o.ElementHandleBaseOptions = *baseOpts + } + + return o, nil +} + +// parseFrameHoverOptions parses FrameHoverOptions from opts. +func parseFrameHoverOptions(ctx context.Context, opts sobek.Value) (*common.FrameHoverOptions, error) { + hoverOpts, err := parseElementHandleHoverOptions(ctx, opts) + if err != nil { + return nil, err + } + + o := &common.FrameHoverOptions{ + Strict: parseStrict(ctx, opts), + } + if hoverOpts != nil { + o.ElementHandleHoverOptions = *hoverOpts + } + + return o, nil +} + +// parseFrameSelectOptionOptions parses FrameSelectOptionOptions from opts. +func parseFrameSelectOptionOptions(ctx context.Context, opts sobek.Value) (*common.FrameSelectOptionOptions, error) { + baseOpts, err := parseElementHandleBaseOptions(ctx, opts) + if err != nil { + return nil, err + } + + o := &common.FrameSelectOptionOptions{ + Strict: parseStrict(ctx, opts), + } + if baseOpts != nil { + o.ElementHandleBaseOptions = *baseOpts + } + + return o, nil +} + +// parseFrameSetInputFilesOptions parses FrameSetInputFilesOptions from opts. +func parseFrameSetInputFilesOptions(ctx context.Context, opts sobek.Value) (*common.FrameSetInputFilesOptions, error) { + inputOpts, err := parseElementHandleSetInputFilesOptions(ctx, opts) + if err != nil { + return nil, err + } + if inputOpts != nil { + return &common.FrameSetInputFilesOptions{ + ElementHandleSetInputFilesOptions: *inputOpts, + }, nil + } + return nil, nil +} + +// parseFrameTapOptions parses FrameTapOptions from opts. +func parseFrameTapOptions(ctx context.Context, opts sobek.Value) (*common.FrameTapOptions, error) { + basePointerOpts, err := parseElementHandleBasePointerOptions(ctx, opts) + if err != nil { + return nil, err + } + + o := &common.FrameTapOptions{} + if basePointerOpts != nil { + o.ElementHandleBasePointerOptions = *basePointerOpts + } + rt := k6ext.Runtime(ctx) + if !k6common.IsNullish(opts) { + obj := opts.ToObject(rt) + for _, k := range obj.Keys() { + switch k { + case "modifiers": + var m []string + if err := rt.ExportTo(obj.Get(k), &m); err != nil { + return nil, err + } + o.Modifiers = m + case "strict": + o.Strict = obj.Get(k).ToBoolean() + } + } + } + + return o, nil +} + +// parseFrameUncheckOptions parses FrameUncheckOptions from opts. +func parseFrameUncheckOptions(ctx context.Context, opts sobek.Value) (*common.FrameUncheckOptions, error) { + basePointerOpts, err := parseElementHandleBasePointerOptions(ctx, opts) + if err != nil { + return nil, err + } + + o := &common.FrameUncheckOptions{ + Strict: parseStrict(ctx, opts), + } + if basePointerOpts != nil { + o.ElementHandleBasePointerOptions = *basePointerOpts + } + + return o, nil +} + +func parseFrameTypeOptions(ctx context.Context, opts sobek.Value) (*common.FrameTypeOptions, error) { + o := &common.FrameTypeOptions{ + ElementHandleTypeOptions: common.ElementHandleTypeOptions{}, // embed base struct + Strict: false, + } + + if k6common.IsNullish(opts) { + return o, nil + } + rt := k6ext.Runtime(ctx) + obj := opts.ToObject(rt) + + for _, k := range obj.Keys() { + switch k { + case "delay": + o.Delay = obj.Get(k).ToInteger() + case "noWaitAfter": + o.NoWaitAfter = obj.Get(k).ToBoolean() + case "timeout": + o.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond + case "strict": + o.Strict = obj.Get(k).ToBoolean() + } + } + + return o, nil +} + +// parseFramePressOptions parses FramePressOptions from opts. +func parseFramePressOptions(ctx context.Context, opts sobek.Value) (*common.FramePressOptions, error) { + o := &common.FramePressOptions{ + ElementHandlePressOptions: common.ElementHandlePressOptions{}, + Strict: false, + } + + if k6common.IsNullish(opts) { + return o, nil + } + + rt := k6ext.Runtime(ctx) + obj := opts.ToObject(rt) + + for _, k := range obj.Keys() { + switch k { + case "delay": + o.Delay = obj.Get(k).ToInteger() + case "noWaitAfter": + o.NoWaitAfter = obj.Get(k).ToBoolean() + case "timeout": + o.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond + case "strict": + o.Strict = obj.Get(k).ToBoolean() + } + } + + return o, nil +} diff --git a/internal/js/modules/k6/browser/browser/locator_mapping.go b/internal/js/modules/k6/browser/browser/locator_mapping.go index 71bb985f06..ba6b7bcc90 100644 --- a/internal/js/modules/k6/browser/browser/locator_mapping.go +++ b/internal/js/modules/k6/browser/browser/locator_mapping.go @@ -41,8 +41,8 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { }), nil }, "clear": func(opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameFillOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + copts, err := parseFrameFillOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing clear options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -50,7 +50,7 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { }), nil }, "click": func(opts sobek.Value) (*sobek.Promise, error) { - popts, err := parseFrameClickOptions(vu.Context(), opts, lo.Timeout()) + popts, err := parseFrameClickOptions(vu.Context(), opts) if err != nil { return nil, err } @@ -69,8 +69,8 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { }) }, "dblclick": func(opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameDblClickOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + copts, err := parseFrameDblClickOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing double click options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -102,8 +102,8 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { }), nil }, "setChecked": func(checked bool, opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameCheckOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + copts, err := parseFrameCheckOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing set checked options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -111,8 +111,8 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { }), nil }, "check": func(opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameCheckOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + copts, err := parseFrameCheckOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing check options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -120,8 +120,8 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { }), nil }, "uncheck": func(opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameUncheckOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + copts, err := parseFrameUncheckOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing uncheck options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -175,8 +175,8 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { }) }, "fill": func(value string, opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameFillOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + copts, err := parseFrameFillOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing fill options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -335,8 +335,8 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { }), nil }, "selectOption": func(values sobek.Value, opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameSelectOptionOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + copts, err := parseFrameSelectOptionOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing select option options: %w", err) } convValues, err := common.ConvertSelectOptionValues(vu.Runtime(), values) @@ -348,8 +348,8 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { }), nil }, "press": func(key string, opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFramePressOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + copts, err := parseFramePressOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing press options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -357,8 +357,8 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { }), nil }, "type": func(text string, opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameTypeOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + copts, err := parseFrameTypeOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing type options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -366,8 +366,8 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { }), nil }, "hover": func(opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameHoverOptions(lo.Timeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + copts, err := parseFrameHoverOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing hover options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -375,8 +375,8 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping { }), nil }, "tap": func(opts sobek.Value) (*sobek.Promise, error) { - copts := common.NewFrameTapOptions(lo.DefaultTimeout()) - if err := copts.Parse(vu.Context(), opts); err != nil { + copts, err := parseFrameTapOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing locator tap options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { diff --git a/internal/js/modules/k6/browser/browser/mapping.go b/internal/js/modules/k6/browser/browser/mapping.go index 50aabd58ea..9613e2e780 100644 --- a/internal/js/modules/k6/browser/browser/mapping.go +++ b/internal/js/modules/k6/browser/browser/mapping.go @@ -1,14 +1,10 @@ package browser import ( - "context" "fmt" - "time" "github.com/grafana/sobek" - "go.k6.io/k6/internal/js/modules/k6/browser/common" - k6common "go.k6.io/k6/js/common" ) @@ -35,13 +31,3 @@ func mapBrowserToSobek(vu moduleVU) *sobek.Object { return obj } - -func parseFrameClickOptions( - ctx context.Context, opts sobek.Value, defaultTimeout time.Duration, -) (*common.FrameClickOptions, error) { - copts := common.NewFrameClickOptions(defaultTimeout) - if err := copts.Parse(ctx, opts); err != nil { - return nil, fmt.Errorf("parsing click options: %w", err) - } - return copts, nil -} diff --git a/internal/js/modules/k6/browser/browser/page_mapping.go b/internal/js/modules/k6/browser/browser/page_mapping.go index 6f8bf73d33..df03fb5fa7 100644 --- a/internal/js/modules/k6/browser/browser/page_mapping.go +++ b/internal/js/modules/k6/browser/browser/page_mapping.go @@ -27,8 +27,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop }) }, "check": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameCheckOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFrameCheckOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing new frame check options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -36,7 +36,7 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop }), nil }, "click": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts, err := parseFrameClickOptions(vu.Context(), opts, p.MainFrame().Timeout()) + popts, err := parseFrameClickOptions(vu.Context(), opts) if err != nil { return nil, err } @@ -66,8 +66,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop return mapBrowserContext(vu, p.Context()) }, "dblclick": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameDblClickOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFrameDblClickOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing double click options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -123,8 +123,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop }), nil }, "fill": func(selector string, value string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameFillOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFrameFillOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing fill options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -247,8 +247,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop }), nil }, "hover": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameHoverOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFrameHoverOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing hover options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -354,8 +354,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop }) }, "press": func(selector string, key string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFramePressOptions(p.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFramePressOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing press options of selector %q: %w", selector, err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -405,11 +405,10 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop return promise, nil }, "selectOption": func(selector string, values sobek.Value, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameSelectOptionOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFrameSelectOptionOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing select option options: %w", err) } - convValues, err := common.ConvertSelectOptionValues(vu.Runtime(), values) if err != nil { return nil, fmt.Errorf("parsing select options values: %w", err) @@ -419,11 +418,10 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop }), nil }, "setChecked": func(selector string, checked bool, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameCheckOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFrameCheckOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing frame set check options: %w", err) } - return k6ext.Promise(vu.Context(), func() (any, error) { return nil, p.SetChecked(selector, checked, popts) //nolint:wrapcheck }), nil @@ -445,11 +443,10 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop }) }, "setInputFiles": func(selector string, files sobek.Value, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameSetInputFilesOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFrameSetInputFilesOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing setInputFiles options: %w", err) } - pfiles := new(common.Files) if err := pfiles.Parse(vu.Context(), files); err != nil { return nil, fmt.Errorf("parsing setInputFiles parameter: %w", err) @@ -469,8 +466,8 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop }), nil }, "tap": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameTapOptions(p.Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFrameTapOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing page tap options: %w", err) } return k6ext.Promise(vu.Context(), func() (any, error) { @@ -511,21 +508,19 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop }, "touchscreen": mapTouchscreen(vu, p.GetTouchscreen()), "type": func(selector string, text string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameTypeOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFrameTypeOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing type options: %w", err) } - return k6ext.Promise(vu.Context(), func() (any, error) { return nil, p.Type(selector, text, popts) //nolint:wrapcheck }), nil }, "uncheck": func(selector string, opts sobek.Value) (*sobek.Promise, error) { - popts := common.NewFrameUncheckOptions(p.MainFrame().Timeout()) - if err := popts.Parse(vu.Context(), opts); err != nil { + popts, err := parseFrameUncheckOptions(vu.Context(), opts) + if err != nil { return nil, fmt.Errorf("parsing frame uncheck options %q: %w", selector, err) } - return k6ext.Promise(vu.Context(), func() (any, error) { return nil, p.Uncheck(selector, popts) //nolint:wrapcheck }), nil diff --git a/internal/js/modules/k6/browser/common/element_handle_options.go b/internal/js/modules/k6/browser/common/element_handle_options.go index 71f9608a63..03e234db42 100644 --- a/internal/js/modules/k6/browser/common/element_handle_options.go +++ b/internal/js/modules/k6/browser/common/element_handle_options.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "reflect" - "strings" "time" "github.com/grafana/sobek" @@ -43,13 +42,6 @@ const ( ScrollPositionNearest ScrollPosition = "nearest" ) -const ( - optionButton = "button" - optionDelay = "delay" - optionClickCount = "clickCount" - optionModifiers = "modifiers" -) - // ScrollIntoViewOptions change the behavior of ScrollIntoView. // See: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView type ScrollIntoViewOptions struct { @@ -147,26 +139,6 @@ func NewElementHandleBaseOptions(defaultTimeout time.Duration) *ElementHandleBas } } -// Parse parses the ElementHandleBaseOptions from the given opts. -func (o *ElementHandleBaseOptions) Parse(ctx context.Context, opts sobek.Value) error { - if common.IsNullish(opts) { - return nil - } - gopts := opts.ToObject(k6ext.Runtime(ctx)) - for _, k := range gopts.Keys() { - switch k { - case "force": - o.Force = gopts.Get(k).ToBoolean() - case "noWaitAfter": - o.NoWaitAfter = gopts.Get(k).ToBoolean() - case "timeout": - o.Timeout = time.Duration(gopts.Get(k).ToInteger()) * time.Millisecond - } - } - - return nil -} - func NewElementHandleBasePointerOptions(defaultTimeout time.Duration) *ElementHandleBasePointerOptions { return &ElementHandleBasePointerOptions{ ElementHandleBaseOptions: *NewElementHandleBaseOptions(defaultTimeout), @@ -175,42 +147,12 @@ func NewElementHandleBasePointerOptions(defaultTimeout time.Duration) *ElementHa } } -// Parse parses the ElementHandleBasePointerOptions from the given opts. -func (o *ElementHandleBasePointerOptions) Parse(ctx context.Context, opts sobek.Value) error { - rt := k6ext.Runtime(ctx) - if err := o.ElementHandleBaseOptions.Parse(ctx, opts); err != nil { - return err - } - if !common.IsNullish(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - switch k { - case "position": - var p map[string]float64 - o.Position = &Position{} - if rt.ExportTo(opts.Get(k), &p) != nil { - o.Position.X = p["x"] - o.Position.Y = p["y"] - } - case "trial": - o.Trial = opts.Get(k).ToBoolean() - } - } - } - return nil -} - func NewElementHandleCheckOptions(defaultTimeout time.Duration) *ElementHandleCheckOptions { return &ElementHandleCheckOptions{ ElementHandleBasePointerOptions: *NewElementHandleBasePointerOptions(defaultTimeout), } } -// Parse parses the ElementHandleCheckOptions from the given opts. -func (o *ElementHandleCheckOptions) Parse(ctx context.Context, opts sobek.Value) error { - return o.ElementHandleBasePointerOptions.Parse(ctx, opts) -} - // NewElementHandleSetInputFilesOptions creates a new ElementHandleSetInputFilesOption. func NewElementHandleSetInputFilesOptions(defaultTimeout time.Duration) *ElementHandleSetInputFilesOptions { return &ElementHandleSetInputFilesOptions{ @@ -263,15 +205,6 @@ func (f *Files) Parse(ctx context.Context, files sobek.Value) error { return nil } -// Parse parses the ElementHandleSetInputFilesOption from the given opts. -func (o *ElementHandleSetInputFilesOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.ElementHandleBaseOptions.Parse(ctx, opts); err != nil { - return err - } - - return nil -} - func NewElementHandleClickOptions(defaultTimeout time.Duration) *ElementHandleClickOptions { return &ElementHandleClickOptions{ ElementHandleBasePointerOptions: *NewElementHandleBasePointerOptions(defaultTimeout), @@ -282,38 +215,6 @@ func NewElementHandleClickOptions(defaultTimeout time.Duration) *ElementHandleCl } } -// Parse parses the ElementHandleClickOptions from the given opts. -func (o *ElementHandleClickOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil { - return err - } - - if common.IsNullish(opts) { - return nil - } - - rt := k6ext.Runtime(ctx) - obj := opts.ToObject(rt) - for _, k := range obj.Keys() { - switch k { - case optionButton: - o.Button = obj.Get(k).String() - case optionClickCount: - o.ClickCount = obj.Get(k).ToInteger() - case optionDelay: - o.Delay = obj.Get(k).ToInteger() - case optionModifiers: - var m []string - if err := rt.ExportTo(obj.Get(k), &m); err != nil { - return fmt.Errorf("parsing element handle click option modifiers: %w", err) - } - o.Modifiers = m - } - } - - return nil -} - func (o *ElementHandleClickOptions) ToMouseClickOptions() *MouseClickOptions { o2 := NewMouseClickOptions() o2.Button = o.Button @@ -331,32 +232,6 @@ func NewElementHandleDblclickOptions(defaultTimeout time.Duration) *ElementHandl } } -// Parse parses the ElementHandleDblclickOptions from the given opts. -func (o *ElementHandleDblclickOptions) Parse(ctx context.Context, opts sobek.Value) error { - rt := k6ext.Runtime(ctx) - if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil { - return err - } - if !common.IsNullish(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - switch k { - case "button": - o.Button = opts.Get(k).String() - case "delay": - o.Delay = opts.Get(k).ToInteger() - case "modifiers": - var m []string - if err := rt.ExportTo(opts.Get(k), &m); err != nil { - return err - } - o.Modifiers = m - } - } - } - return nil -} - func (o *ElementHandleDblclickOptions) ToMouseClickOptions() *MouseClickOptions { o2 := NewMouseClickOptions() o2.Button = o.Button @@ -372,27 +247,6 @@ func NewElementHandleHoverOptions(defaultTimeout time.Duration) *ElementHandleHo } } -// Parse parses the ElementHandleHoverOptions from the given opts. -func (o *ElementHandleHoverOptions) Parse(ctx context.Context, opts sobek.Value) error { - rt := k6ext.Runtime(ctx) - if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil { - return err - } - if !common.IsNullish(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - if k == "modifiers" { - var m []string - if err := rt.ExportTo(opts.Get(k), &m); err != nil { - return err - } - o.Modifiers = m - } - } - } - return nil -} - func NewElementHandlePressOptions(defaultTimeout time.Duration) *ElementHandlePressOptions { return &ElementHandlePressOptions{ Delay: 0, @@ -401,25 +255,6 @@ func NewElementHandlePressOptions(defaultTimeout time.Duration) *ElementHandlePr } } -// Parse parses the ElementHandlePressOptions from the given opts. -func (o *ElementHandlePressOptions) Parse(ctx context.Context, opts sobek.Value) error { - rt := k6ext.Runtime(ctx) - if !common.IsNullish(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - switch k { - case "delay": - o.Delay = opts.Get(k).ToInteger() - case "noWaitAfter": - o.NoWaitAfter = opts.Get(k).ToBoolean() - case "timeout": - o.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond - } - } - } - return nil -} - func (o *ElementHandlePressOptions) ToBaseOptions() *ElementHandleBaseOptions { o2 := ElementHandleBaseOptions{} o2.Force = false @@ -438,43 +273,6 @@ func NewElementHandleScreenshotOptions(defaultTimeout time.Duration) *ElementHan } } -// Parse parses the ElementHandleScreenshotOptions from the given opts. -func (o *ElementHandleScreenshotOptions) Parse(ctx context.Context, opts sobek.Value) error { - if common.IsNullish(opts) { - return nil - } - - rt := k6ext.Runtime(ctx) - formatSpecified := false - obj := opts.ToObject(rt) - for _, k := range obj.Keys() { - switch k { - case "omitBackground": - o.OmitBackground = obj.Get(k).ToBoolean() - case "path": - o.Path = obj.Get(k).String() - case "quality": - o.Quality = obj.Get(k).ToInteger() - case "type": - if f, ok := imageFormatToID[obj.Get(k).String()]; ok { - o.Format = f - formatSpecified = true - } - case "timeout": - o.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond - } - } - - // Infer file format by path if format not explicitly specified (default is PNG) - if o.Path != "" && !formatSpecified { - if strings.HasSuffix(o.Path, ".jpg") || strings.HasSuffix(o.Path, ".jpeg") { - o.Format = ImageFormatJPEG - } - } - - return nil -} - func NewElementHandleSetCheckedOptions(defaultTimeout time.Duration) *ElementHandleSetCheckedOptions { return &ElementHandleSetCheckedOptions{ ElementHandleBasePointerOptions: *NewElementHandleBasePointerOptions(defaultTimeout), @@ -482,25 +280,6 @@ func NewElementHandleSetCheckedOptions(defaultTimeout time.Duration) *ElementHan } } -// Parse parses the ElementHandleSetCheckedOptions from the given opts. -func (o *ElementHandleSetCheckedOptions) Parse(ctx context.Context, opts sobek.Value) error { - rt := k6ext.Runtime(ctx) - - if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil { - return err - } - - if !common.IsNullish(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - if k == "strict" { - o.Strict = opts.Get(k).ToBoolean() - } - } - } - return nil -} - func NewElementHandleTapOptions(defaultTimeout time.Duration) *ElementHandleTapOptions { return &ElementHandleTapOptions{ ElementHandleBasePointerOptions: *NewElementHandleBasePointerOptions(defaultTimeout), @@ -508,27 +287,6 @@ func NewElementHandleTapOptions(defaultTimeout time.Duration) *ElementHandleTapO } } -// Parse parses the ElementHandleTapOptions from the given opts. -func (o *ElementHandleTapOptions) Parse(ctx context.Context, opts sobek.Value) error { - rt := k6ext.Runtime(ctx) - if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil { - return err - } - if !common.IsNullish(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - if k == "modifiers" { - var m []string - if err := rt.ExportTo(opts.Get(k), &m); err != nil { - return err - } - o.Modifiers = m - } - } - } - return nil -} - func NewElementHandleTypeOptions(defaultTimeout time.Duration) *ElementHandleTypeOptions { return &ElementHandleTypeOptions{ Delay: 0, @@ -537,25 +295,6 @@ func NewElementHandleTypeOptions(defaultTimeout time.Duration) *ElementHandleTyp } } -// Parse parses the ElementHandleTypeOptions from the given opts. -func (o *ElementHandleTypeOptions) Parse(ctx context.Context, opts sobek.Value) error { - rt := k6ext.Runtime(ctx) - if !common.IsNullish(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - switch k { - case "delay": - o.Delay = opts.Get(k).ToInteger() - case "noWaitAfter": - o.NoWaitAfter = opts.Get(k).ToBoolean() - case "timeout": - o.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond - } - } - } - return nil -} - func (o *ElementHandleTypeOptions) ToBaseOptions() *ElementHandleBaseOptions { o2 := ElementHandleBaseOptions{} o2.Force = false @@ -570,20 +309,6 @@ func NewElementHandleWaitForElementStateOptions(defaultTimeout time.Duration) *E } } -// Parse parses the ElementHandleWaitForElementStateOptions from the given opts. -func (o *ElementHandleWaitForElementStateOptions) Parse(ctx context.Context, opts sobek.Value) error { - rt := k6ext.Runtime(ctx) - if !common.IsNullish(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - if k == "timeout" { - o.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond - } - } - } - return nil -} - // ElementHandleDispatchEventOptions are options for ElementHandle.dispatchEvent. type ElementHandleDispatchEventOptions struct { *ElementHandleBaseOptions diff --git a/internal/js/modules/k6/browser/common/frame_options.go b/internal/js/modules/k6/browser/common/frame_options.go index f90582a648..ab38a0ca65 100644 --- a/internal/js/modules/k6/browser/common/frame_options.go +++ b/internal/js/modules/k6/browser/common/frame_options.go @@ -230,15 +230,6 @@ func NewFrameCheckOptions(defaultTimeout time.Duration) *FrameCheckOptions { } } -// Parse parses the frame check options. -func (o *FrameCheckOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil { - return err - } - o.Strict = parseStrict(ctx, opts) - return nil -} - func NewFrameClickOptions(defaultTimeout time.Duration) *FrameClickOptions { return &FrameClickOptions{ ElementHandleClickOptions: *NewElementHandleClickOptions(defaultTimeout), @@ -246,15 +237,6 @@ func NewFrameClickOptions(defaultTimeout time.Duration) *FrameClickOptions { } } -// Parse parses the frame click options. -func (o *FrameClickOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.ElementHandleClickOptions.Parse(ctx, opts); err != nil { - return err - } - o.Strict = parseStrict(ctx, opts) - return nil -} - func NewFrameDblClickOptions(defaultTimeout time.Duration) *FrameDblclickOptions { return &FrameDblclickOptions{ ElementHandleDblclickOptions: *NewElementHandleDblclickOptions(defaultTimeout), @@ -262,15 +244,6 @@ func NewFrameDblClickOptions(defaultTimeout time.Duration) *FrameDblclickOptions } } -// Parse parses the frame dblclick options. -func (o *FrameDblclickOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.ElementHandleDblclickOptions.Parse(ctx, opts); err != nil { - return err - } - o.Strict = parseStrict(ctx, opts) - return nil -} - func NewFrameFillOptions(defaultTimeout time.Duration) *FrameFillOptions { return &FrameFillOptions{ ElementHandleBaseOptions: *NewElementHandleBaseOptions(defaultTimeout), @@ -278,15 +251,6 @@ func NewFrameFillOptions(defaultTimeout time.Duration) *FrameFillOptions { } } -// Parse parses the frame fill options. -func (o *FrameFillOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.ElementHandleBaseOptions.Parse(ctx, opts); err != nil { - return err - } - o.Strict = parseStrict(ctx, opts) - return nil -} - func NewFrameGotoOptions(defaultReferer string, defaultTimeout time.Duration) *FrameGotoOptions { return &FrameGotoOptions{ Referer: defaultReferer, @@ -324,15 +288,6 @@ func NewFrameHoverOptions(defaultTimeout time.Duration) *FrameHoverOptions { } } -// Parse parses the frame hover options. -func (o *FrameHoverOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.ElementHandleHoverOptions.Parse(ctx, opts); err != nil { - return err - } - o.Strict = parseStrict(ctx, opts) - return nil -} - func NewFrameInnerHTMLOptions(defaultTimeout time.Duration) *FrameInnerHTMLOptions { return &FrameInnerHTMLOptions{ FrameBaseOptions: *NewFrameBaseOptions(defaultTimeout), @@ -475,15 +430,6 @@ func NewFrameSelectOptionOptions(defaultTimeout time.Duration) *FrameSelectOptio } } -// Parse parses the frame selectOption options. -func (o *FrameSelectOptionOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.ElementHandleBaseOptions.Parse(ctx, opts); err != nil { - return err - } - o.Strict = parseStrict(ctx, opts) - return nil -} - func NewFrameSetContentOptions(defaultTimeout time.Duration) *FrameSetContentOptions { return &FrameSetContentOptions{ Timeout: defaultTimeout, @@ -521,14 +467,6 @@ func NewFrameSetInputFilesOptions(defaultTimeout time.Duration) *FrameSetInputFi } } -// Parse parses FrameSetInputFilesOptions from sobek.Value. -func (o *FrameSetInputFilesOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.ElementHandleSetInputFilesOptions.Parse(ctx, opts); err != nil { - return err - } - return nil -} - func NewFrameTapOptions(defaultTimeout time.Duration) *FrameTapOptions { return &FrameTapOptions{ ElementHandleBasePointerOptions: *NewElementHandleBasePointerOptions(defaultTimeout), @@ -537,30 +475,6 @@ func NewFrameTapOptions(defaultTimeout time.Duration) *FrameTapOptions { } } -// Parse parses the frame tap options. -func (o *FrameTapOptions) Parse(ctx context.Context, opts sobek.Value) error { - rt := k6ext.Runtime(ctx) - if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil { - return err - } - if !common.IsNullish(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - switch k { - case "modifiers": - var m []string - if err := rt.ExportTo(opts.Get(k), &m); err != nil { - return err - } - o.Modifiers = m - case "strict": - o.Strict = opts.Get(k).ToBoolean() - } - } - } - return nil -} - func NewFrameTextContentOptions(defaultTimeout time.Duration) *FrameTextContentOptions { return &FrameTextContentOptions{ FrameBaseOptions: *NewFrameBaseOptions(defaultTimeout), @@ -596,15 +510,6 @@ func NewFrameUncheckOptions(defaultTimeout time.Duration) *FrameUncheckOptions { } } -// Parse parses the frame uncheck options. -func (o *FrameUncheckOptions) Parse(ctx context.Context, opts sobek.Value) error { - if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil { - return err - } - o.Strict = parseStrict(ctx, opts) - return nil -} - func NewFrameWaitForFunctionOptions(defaultTimeout time.Duration) *FrameWaitForFunctionOptions { return &FrameWaitForFunctionOptions{ Polling: PollingRaf, From 16cab73b1334a820f91a7f3ca6dfc3e5ceb5e0c0 Mon Sep 17 00:00:00 2001 From: Anupriya Kumari Date: Fri, 24 Oct 2025 11:50:52 +0000 Subject: [PATCH 2/2] Handled linter issues --- .../browser/browser/element_handle_mapping.go | 28 +++++++++---------- .../k6/browser/browser/frame_mapping.go | 13 ++------- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/internal/js/modules/k6/browser/browser/element_handle_mapping.go b/internal/js/modules/k6/browser/browser/element_handle_mapping.go index 5a40f4101f..adda525989 100644 --- a/internal/js/modules/k6/browser/browser/element_handle_mapping.go +++ b/internal/js/modules/k6/browser/browser/element_handle_mapping.go @@ -21,6 +21,8 @@ const ( optionModifiers = "modifiers" ) +const noWaitAfterOption = "noWaitAfter" + var imageFormatToID = map[string]common.ImageFormat{ //nolint:gochecknoglobals "jpeg": common.ImageFormatJPEG, "png": common.ImageFormatPNG, @@ -356,9 +358,10 @@ func mapElementHandle(vu moduleVU, eh *common.ElementHandle) mapping { //nolint: return maps } -// parseElementHandleBaseOptions parses ElementHandleBaseOptions from opts +//nolint:unparam // keeping error for consistency with other parse functions func parseElementHandleBaseOptions(ctx context.Context, opts sobek.Value) (*common.ElementHandleBaseOptions, error) { if k6common.IsNullish(opts) { + //nolint:nilnil // returning (nil, nil) intentionally means "no options provided" return nil, nil } @@ -373,7 +376,7 @@ func parseElementHandleBaseOptions(ctx context.Context, opts sobek.Value) (*comm switch k { case "force": parsed.Force = obj.Get(k).ToBoolean() - case "noWaitAfter": + case noWaitAfterOption: parsed.NoWaitAfter = obj.Get(k).ToBoolean() case "timeout": parsed.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond @@ -383,7 +386,6 @@ func parseElementHandleBaseOptions(ctx context.Context, opts sobek.Value) (*comm return parsed, nil } -// parseElementHandleBasePointerOptions parses ElementHandleBasePointerOptions from opts func parseElementHandleBasePointerOptions( ctx context.Context, opts sobek.Value, @@ -419,7 +421,6 @@ func parseElementHandleBasePointerOptions( return o, nil } -// parseElementHandleSetInputFilesOptions parses ElementHandleSetInputFilesOptions from opts. func parseElementHandleSetInputFilesOptions( ctx context.Context, opts sobek.Value, @@ -429,6 +430,7 @@ func parseElementHandleSetInputFilesOptions( return nil, err } if baseOpts == nil { + //nolint:nilnil // returning (nil, nil) intentionally means "no options provided" return nil, nil } @@ -439,9 +441,9 @@ func parseElementHandleSetInputFilesOptions( return o, nil } -// parseElementHandleClickOptions parses ElementHandleClickOptions from opts func parseElementHandleClickOptions(ctx context.Context, opts sobek.Value) (*common.ElementHandleClickOptions, error) { if k6common.IsNullish(opts) { + //nolint:nilnil // returning (nil, nil) intentionally means "no options provided" return nil, nil } basePointer, err := parseElementHandleBasePointerOptions(ctx, opts) @@ -474,7 +476,6 @@ func parseElementHandleClickOptions(ctx context.Context, opts sobek.Value) (*com return o, nil } -// parseElementHandleDblclickOptions parses ElementHandleDblclickOptions from opts func parseElementHandleDblclickOptions( ctx context.Context, opts sobek.Value, @@ -511,7 +512,6 @@ func parseElementHandleDblclickOptions( return o, nil } -// parseElementHandleHoverOptions parses ElementHandleHoverOptions from opts func parseElementHandleHoverOptions(ctx context.Context, opts sobek.Value) (*common.ElementHandleHoverOptions, error) { o := &common.ElementHandleHoverOptions{} @@ -539,7 +539,6 @@ func parseElementHandleHoverOptions(ctx context.Context, opts sobek.Value) (*com return o, nil } -// parseElementHandleSetCheckedOptions parses ElementHandleSetCheckedOptions from opts. func parseElementHandleSetCheckedOptions( ctx context.Context, opts sobek.Value, @@ -566,7 +565,6 @@ func parseElementHandleSetCheckedOptions( return o, nil } -// parseElementHandleTapOptions parses ElementHandleTapOptions from opts. func parseElementHandleTapOptions(ctx context.Context, opts sobek.Value) (*common.ElementHandleTapOptions, error) { o := &common.ElementHandleTapOptions{} @@ -595,7 +593,7 @@ func parseElementHandleTapOptions(ctx context.Context, opts sobek.Value) (*commo return o, nil } -// parseElementHandlePressOptions parses ElementHandlePressOptions from opts. +//nolint:unparam // keeping error for consistency with other parse functions func parseElementHandlePressOptions(ctx context.Context, opts sobek.Value) (*common.ElementHandlePressOptions, error) { o := &common.ElementHandlePressOptions{} rt := k6ext.Runtime(ctx) @@ -605,7 +603,7 @@ func parseElementHandlePressOptions(ctx context.Context, opts sobek.Value) (*com switch k { case "delay": o.Delay = obj.Get(k).ToInteger() - case "noWaitAfter": + case noWaitAfterOption: o.NoWaitAfter = obj.Get(k).ToBoolean() case "timeout": o.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond @@ -616,7 +614,7 @@ func parseElementHandlePressOptions(ctx context.Context, opts sobek.Value) (*com return o, nil } -// parseElementHandleScreenshotOptions parses ElementHandleScreenshotOptions from opts. +//nolint:unparam // keeping error for consistency with other parse functions func parseElementHandleScreenshotOptions( ctx context.Context, opts sobek.Value, @@ -657,7 +655,7 @@ func parseElementHandleScreenshotOptions( return o, nil } -// parseElementHandleTypeOptions parses ElementHandleTypeOptions from opts. +//nolint:unparam // keeping error for consistency with other parse functions func parseElementHandleTypeOptions(ctx context.Context, opts sobek.Value) (*common.ElementHandleTypeOptions, error) { o := &common.ElementHandleTypeOptions{} rt := k6ext.Runtime(ctx) @@ -667,7 +665,7 @@ func parseElementHandleTypeOptions(ctx context.Context, opts sobek.Value) (*comm switch k { case "delay": o.Delay = obj.Get(k).ToInteger() - case "noWaitAfter": + case noWaitAfterOption: o.NoWaitAfter = obj.Get(k).ToBoolean() case "timeout": o.Timeout = time.Duration(obj.Get(k).ToInteger()) * time.Millisecond @@ -678,7 +676,7 @@ func parseElementHandleTypeOptions(ctx context.Context, opts sobek.Value) (*comm return o, nil } -// parseElementHandleWaitForElementStateOptions parses ElementHandleWaitForElementStateOptions from opts. +//nolint:unparam // keeping error for consistency with other parse functions func parseElementHandleWaitForElementStateOptions( ctx context.Context, opts sobek.Value, diff --git a/internal/js/modules/k6/browser/browser/frame_mapping.go b/internal/js/modules/k6/browser/browser/frame_mapping.go index 9fdea769cd..93f229e5ee 100644 --- a/internal/js/modules/k6/browser/browser/frame_mapping.go +++ b/internal/js/modules/k6/browser/browser/frame_mapping.go @@ -537,7 +537,6 @@ func parseStrict(ctx context.Context, opts sobek.Value) bool { return strict } -// parseFrameCheckOptions parses FrameCheckOptions from opts. func parseFrameCheckOptions(ctx context.Context, opts sobek.Value) (*common.FrameCheckOptions, error) { basePointerOpts, err := parseElementHandleBasePointerOptions(ctx, opts) if err != nil { @@ -554,7 +553,6 @@ func parseFrameCheckOptions(ctx context.Context, opts sobek.Value) (*common.Fram return o, nil } -// parseFrameClickOptions parses FrameClickOptions from opts. func parseFrameClickOptions(ctx context.Context, opts sobek.Value) (*common.FrameClickOptions, error) { clickOpts, err := parseElementHandleClickOptions(ctx, opts) if err != nil { @@ -571,7 +569,6 @@ func parseFrameClickOptions(ctx context.Context, opts sobek.Value) (*common.Fram return o, nil } -// parseFrameDblClickOptions parses FrameDblClickOptions from opts. func parseFrameDblClickOptions(ctx context.Context, opts sobek.Value) (*common.FrameDblclickOptions, error) { dblclickOpts, err := parseElementHandleDblclickOptions(ctx, opts) if err != nil { @@ -588,7 +585,6 @@ func parseFrameDblClickOptions(ctx context.Context, opts sobek.Value) (*common.F return o, nil } -// parseFrameFillOptions parses FrameFillOptions from opts. func parseFrameFillOptions(ctx context.Context, opts sobek.Value) (*common.FrameFillOptions, error) { baseOpts, err := parseElementHandleBaseOptions(ctx, opts) if err != nil { @@ -605,7 +601,6 @@ func parseFrameFillOptions(ctx context.Context, opts sobek.Value) (*common.Frame return o, nil } -// parseFrameHoverOptions parses FrameHoverOptions from opts. func parseFrameHoverOptions(ctx context.Context, opts sobek.Value) (*common.FrameHoverOptions, error) { hoverOpts, err := parseElementHandleHoverOptions(ctx, opts) if err != nil { @@ -622,7 +617,6 @@ func parseFrameHoverOptions(ctx context.Context, opts sobek.Value) (*common.Fram return o, nil } -// parseFrameSelectOptionOptions parses FrameSelectOptionOptions from opts. func parseFrameSelectOptionOptions(ctx context.Context, opts sobek.Value) (*common.FrameSelectOptionOptions, error) { baseOpts, err := parseElementHandleBaseOptions(ctx, opts) if err != nil { @@ -639,7 +633,6 @@ func parseFrameSelectOptionOptions(ctx context.Context, opts sobek.Value) (*comm return o, nil } -// parseFrameSetInputFilesOptions parses FrameSetInputFilesOptions from opts. func parseFrameSetInputFilesOptions(ctx context.Context, opts sobek.Value) (*common.FrameSetInputFilesOptions, error) { inputOpts, err := parseElementHandleSetInputFilesOptions(ctx, opts) if err != nil { @@ -650,10 +643,10 @@ func parseFrameSetInputFilesOptions(ctx context.Context, opts sobek.Value) (*com ElementHandleSetInputFilesOptions: *inputOpts, }, nil } + //nolint:nilnil // returning (nil, nil) intentionally means "no options provided" return nil, nil } -// parseFrameTapOptions parses FrameTapOptions from opts. func parseFrameTapOptions(ctx context.Context, opts sobek.Value) (*common.FrameTapOptions, error) { basePointerOpts, err := parseElementHandleBasePointerOptions(ctx, opts) if err != nil { @@ -684,7 +677,6 @@ func parseFrameTapOptions(ctx context.Context, opts sobek.Value) (*common.FrameT return o, nil } -// parseFrameUncheckOptions parses FrameUncheckOptions from opts. func parseFrameUncheckOptions(ctx context.Context, opts sobek.Value) (*common.FrameUncheckOptions, error) { basePointerOpts, err := parseElementHandleBasePointerOptions(ctx, opts) if err != nil { @@ -701,6 +693,7 @@ func parseFrameUncheckOptions(ctx context.Context, opts sobek.Value) (*common.Fr return o, nil } +//nolint:unparam // keeping error for consistency with other parse functions func parseFrameTypeOptions(ctx context.Context, opts sobek.Value) (*common.FrameTypeOptions, error) { o := &common.FrameTypeOptions{ ElementHandleTypeOptions: common.ElementHandleTypeOptions{}, // embed base struct @@ -729,7 +722,7 @@ func parseFrameTypeOptions(ctx context.Context, opts sobek.Value) (*common.Frame return o, nil } -// parseFramePressOptions parses FramePressOptions from opts. +//nolint:unparam // keeping error for consistency with other parse functions func parseFramePressOptions(ctx context.Context, opts sobek.Value) (*common.FramePressOptions, error) { o := &common.FramePressOptions{ ElementHandlePressOptions: common.ElementHandlePressOptions{},