Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 80ae11c

Browse files
committed
Refactor ElementHandleClickOptions.Parse
1 parent cb80906 commit 80ae11c

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

common/element_handle_options.go

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ const (
3838
ScrollPositionNearest ScrollPosition = "nearest"
3939
)
4040

41+
const (
42+
optionButton = "button"
43+
optionDelay = "delay"
44+
optionClickCount = "clickCount"
45+
optionModifiers = "modifiers"
46+
)
47+
4148
// ScrollIntoViewOptions change the behavior of ScrollIntoView.
4249
// See: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
4350
type ScrollIntoViewOptions struct {
@@ -272,29 +279,33 @@ func NewElementHandleClickOptions(defaultTimeout time.Duration) *ElementHandleCl
272279

273280
// Parse parses the ElementHandleClickOptions from the given opts.
274281
func (o *ElementHandleClickOptions) Parse(ctx context.Context, opts sobek.Value) error {
275-
rt := k6ext.Runtime(ctx)
276282
if err := o.ElementHandleBasePointerOptions.Parse(ctx, opts); err != nil {
277283
return err
278284
}
279-
if opts != nil && !sobek.IsUndefined(opts) && !sobek.IsNull(opts) {
280-
opts := opts.ToObject(rt)
281-
for _, k := range opts.Keys() {
282-
switch k {
283-
case "button":
284-
o.Button = opts.Get(k).String()
285-
case "clickCount":
286-
o.ClickCount = opts.Get(k).ToInteger()
287-
case "delay":
288-
o.Delay = opts.Get(k).ToInteger()
289-
case "modifiers":
290-
var m []string
291-
if err := rt.ExportTo(opts.Get(k), &m); err != nil {
292-
return err
293-
}
294-
o.Modifiers = m
285+
286+
if !sobekValueExists(opts) {
287+
return nil
288+
}
289+
290+
rt := k6ext.Runtime(ctx)
291+
obj := opts.ToObject(rt)
292+
for _, k := range obj.Keys() {
293+
switch k {
294+
case optionButton:
295+
o.Button = obj.Get(k).String()
296+
case optionClickCount:
297+
o.ClickCount = obj.Get(k).ToInteger()
298+
case optionDelay:
299+
o.Delay = obj.Get(k).ToInteger()
300+
case optionModifiers:
301+
var m []string
302+
if err := rt.ExportTo(obj.Get(k), &m); err != nil {
303+
return fmt.Errorf("parsing element handle click option modifiers: %w", err)
295304
}
305+
o.Modifiers = m
296306
}
297307
}
308+
298309
return nil
299310
}
300311

0 commit comments

Comments
 (0)