From 3f4f6beb56c0cb9fca606bfaedacba2e4ce82fb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 6 Nov 2024 15:55:34 -0500 Subject: [PATCH 01/13] Refactor KeyboardOptions to value type --- common/element_handle.go | 4 ++-- common/frame_options.go | 7 +++++-- common/keyboard.go | 10 +++++----- common/keyboard_options.go | 7 +++++-- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/common/element_handle.go b/common/element_handle.go index c78f277c1..0a5fd2858 100644 --- a/common/element_handle.go +++ b/common/element_handle.go @@ -464,7 +464,7 @@ func (h *ElementHandle) scrollRectIntoViewIfNeeded(apiCtx context.Context, rect return nil } -func (h *ElementHandle) press(apiCtx context.Context, key string, opts *KeyboardOptions) error { +func (h *ElementHandle) press(apiCtx context.Context, key string, opts KeyboardOptions) error { err := h.focus(apiCtx, true) if err != nil { return err @@ -611,7 +611,7 @@ func (h *ElementHandle) textContent(apiCtx context.Context) (any, error) { return h.eval(apiCtx, opts, js) } -func (h *ElementHandle) typ(apiCtx context.Context, text string, opts *KeyboardOptions) error { +func (h *ElementHandle) typ(apiCtx context.Context, text string, opts KeyboardOptions) error { err := h.focus(apiCtx, true) if err != nil { return err diff --git a/common/frame_options.go b/common/frame_options.go index ccf4df93e..2ce352ee7 100644 --- a/common/frame_options.go +++ b/common/frame_options.go @@ -459,12 +459,14 @@ func NewFramePressOptions(defaultTimeout time.Duration) *FramePressOptions { } } -func (o *FramePressOptions) ToKeyboardOptions() *KeyboardOptions { +// ToKeyboardOptions converts FramePressOptions to KeyboardOptions. +func (o *FramePressOptions) ToKeyboardOptions() KeyboardOptions { o2 := NewKeyboardOptions() o2.Delay = o.Delay return o2 } +// NewFrameSelectOptionOptions creates and returns a new instance of FrameSelectOptionOptions. func NewFrameSelectOptionOptions(defaultTimeout time.Duration) *FrameSelectOptionOptions { return &FrameSelectOptionOptions{ ElementHandleBaseOptions: *NewElementHandleBaseOptions(defaultTimeout), @@ -579,7 +581,8 @@ func NewFrameTypeOptions(defaultTimeout time.Duration) *FrameTypeOptions { } } -func (o *FrameTypeOptions) ToKeyboardOptions() *KeyboardOptions { +// ToKeyboardOptions converts FrameTypeOptions to KeyboardOptions. +func (o *FrameTypeOptions) ToKeyboardOptions() KeyboardOptions { o2 := NewKeyboardOptions() o2.Delay = o.Delay return o2 diff --git a/common/keyboard.go b/common/keyboard.go index 8a26c182a..80cfc3f87 100644 --- a/common/keyboard.go +++ b/common/keyboard.go @@ -62,7 +62,7 @@ func (k *Keyboard) Up(key string) error { // Press sends a key press message to a session target. // It delays the action if `Delay` option is specified. // A press message consists of successive key down and up messages. -func (k *Keyboard) Press(key string, kbdOpts *KeyboardOptions) error { +func (k *Keyboard) Press(key string, kbdOpts KeyboardOptions) error { if err := k.comboPress(key, kbdOpts); err != nil { return fmt.Errorf("pressing key: %w", err) } @@ -83,7 +83,7 @@ func (k *Keyboard) InsertText(text string) error { // // It sends an insertText message if a character is not among // valid characters in the keyboard's layout. -func (k *Keyboard) Type(text string, kbdOpts *KeyboardOptions) error { +func (k *Keyboard) Type(text string, kbdOpts KeyboardOptions) error { if err := k.typ(text, kbdOpts); err != nil { return fmt.Errorf("typing text: %w", err) } @@ -245,7 +245,7 @@ func (k *Keyboard) platformSpecificResolution(key string) string { return key } -func (k *Keyboard) comboPress(keys string, opts *KeyboardOptions) error { +func (k *Keyboard) comboPress(keys string, opts KeyboardOptions) error { if opts.Delay > 0 { if err := wait(k.ctx, opts.Delay); err != nil { return err @@ -291,7 +291,7 @@ func split(keys string) []string { return kk } -func (k *Keyboard) press(key string, opts *KeyboardOptions) error { +func (k *Keyboard) press(key string, opts KeyboardOptions) error { if opts.Delay > 0 { if err := wait(k.ctx, opts.Delay); err != nil { return err @@ -303,7 +303,7 @@ func (k *Keyboard) press(key string, opts *KeyboardOptions) error { return k.up(key) } -func (k *Keyboard) typ(text string, opts *KeyboardOptions) error { +func (k *Keyboard) typ(text string, opts KeyboardOptions) error { layout := keyboardlayout.GetKeyboardLayout(k.layoutName) for _, c := range text { if opts.Delay > 0 { diff --git a/common/keyboard_options.go b/common/keyboard_options.go index c10376ac5..ef697b18c 100644 --- a/common/keyboard_options.go +++ b/common/keyboard_options.go @@ -8,12 +8,14 @@ import ( "github.com/grafana/xk6-browser/k6ext" ) +// KeyboardOptions represents the options for the keyboard. type KeyboardOptions struct { Delay int64 `json:"delay"` } -func NewKeyboardOptions() *KeyboardOptions { - return &KeyboardOptions{ +// NewKeyboardOptions returns a new KeyboardOptions. +func NewKeyboardOptions() KeyboardOptions { + return KeyboardOptions{ Delay: 0, } } @@ -30,5 +32,6 @@ func (o *KeyboardOptions) Parse(ctx context.Context, opts sobek.Value) error { } } } + return nil } From 4c3a0b90098c568ab25aab51ab2be0ed6b14166c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 6 Nov 2024 15:59:30 -0500 Subject: [PATCH 02/13] Refactor browser keyboard mapping --- browser/keyboard_mapping.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/browser/keyboard_mapping.go b/browser/keyboard_mapping.go index c129d383e..6c384427e 100644 --- a/browser/keyboard_mapping.go +++ b/browser/keyboard_mapping.go @@ -23,7 +23,7 @@ func mapKeyboard(vu moduleVU, kb *common.Keyboard) mapping { }, "press": func(key string, opts sobek.Value) *sobek.Promise { return k6ext.Promise(vu.Context(), func() (any, error) { - kbdOpts := common.NewKeyboardOptions() + var kbdOpts common.KeyboardOptions if err := kbdOpts.Parse(vu.Context(), opts); err != nil { return nil, fmt.Errorf("parsing keyboard options: %w", err) } @@ -33,7 +33,7 @@ func mapKeyboard(vu moduleVU, kb *common.Keyboard) mapping { }, "type": func(text string, opts sobek.Value) *sobek.Promise { return k6ext.Promise(vu.Context(), func() (any, error) { - kbdOpts := common.NewKeyboardOptions() + var kbdOpts common.KeyboardOptions if err := kbdOpts.Parse(vu.Context(), opts); err != nil { return nil, fmt.Errorf("parsing keyboard options: %w", err) } From 83eb968fa51a25ff14ae1af66813f65215f7a9d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 6 Nov 2024 15:59:37 -0500 Subject: [PATCH 03/13] Refactor browser keyboard sync mapping --- browser/sync_keyboard_mapping.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/browser/sync_keyboard_mapping.go b/browser/sync_keyboard_mapping.go index 84adfc630..b0417d5ed 100644 --- a/browser/sync_keyboard_mapping.go +++ b/browser/sync_keyboard_mapping.go @@ -13,7 +13,7 @@ func syncMapKeyboard(vu moduleVU, kb *common.Keyboard) mapping { "down": kb.Down, "up": kb.Up, "press": func(key string, opts sobek.Value) error { - kbdOpts := common.NewKeyboardOptions() + var kbdOpts common.KeyboardOptions if err := kbdOpts.Parse(vu.Context(), opts); err != nil { return fmt.Errorf("parsing keyboard options: %w", err) } @@ -21,7 +21,7 @@ func syncMapKeyboard(vu moduleVU, kb *common.Keyboard) mapping { return kb.Press(key, kbdOpts) //nolint:wrapcheck }, "type": func(text string, opts sobek.Value) error { - kbdOpts := common.NewKeyboardOptions() + var kbdOpts common.KeyboardOptions if err := kbdOpts.Parse(vu.Context(), opts); err != nil { return fmt.Errorf("parsing keyboard options: %w", err) } From 37d5ef6605d45fdaef2e59470e3e5cc6b8028bb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 6 Nov 2024 15:59:51 -0500 Subject: [PATCH 04/13] Refactor elementhandle keyboard mapping --- common/element_handle.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/element_handle.go b/common/element_handle.go index 0a5fd2858..e612920e5 100644 --- a/common/element_handle.go +++ b/common/element_handle.go @@ -1085,7 +1085,7 @@ func (h *ElementHandle) Press(key string, opts sobek.Value) error { } press := func(apiCtx context.Context, handle *ElementHandle) (any, error) { - return nil, handle.press(apiCtx, key, NewKeyboardOptions()) + return nil, handle.press(apiCtx, key, KeyboardOptions{}) } pressAction := h.newAction( []string{}, press, false, popts.NoWaitAfter, popts.Timeout, @@ -1457,7 +1457,7 @@ func (h *ElementHandle) Type(text string, opts sobek.Value) error { } typ := func(apiCtx context.Context, handle *ElementHandle) (any, error) { - return nil, handle.typ(apiCtx, text, NewKeyboardOptions()) + return nil, handle.typ(apiCtx, text, KeyboardOptions{}) } typeAction := h.newAction( []string{}, typ, false, popts.NoWaitAfter, popts.Timeout, From b726f56686cc167f1ab3cd39405d12c37a404029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 6 Nov 2024 15:59:58 -0500 Subject: [PATCH 05/13] Refactor frame keyboard mapping --- common/frame_options.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/common/frame_options.go b/common/frame_options.go index 2ce352ee7..f0e4dced8 100644 --- a/common/frame_options.go +++ b/common/frame_options.go @@ -461,7 +461,7 @@ func NewFramePressOptions(defaultTimeout time.Duration) *FramePressOptions { // ToKeyboardOptions converts FramePressOptions to KeyboardOptions. func (o *FramePressOptions) ToKeyboardOptions() KeyboardOptions { - o2 := NewKeyboardOptions() + var o2 KeyboardOptions o2.Delay = o.Delay return o2 } @@ -583,7 +583,7 @@ func NewFrameTypeOptions(defaultTimeout time.Duration) *FrameTypeOptions { // ToKeyboardOptions converts FrameTypeOptions to KeyboardOptions. func (o *FrameTypeOptions) ToKeyboardOptions() KeyboardOptions { - o2 := NewKeyboardOptions() + var o2 KeyboardOptions o2.Delay = o.Delay return o2 } From 606e3dacfa04ca156749d64177c20a33bd89141c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 6 Nov 2024 16:00:11 -0500 Subject: [PATCH 06/13] Refactor keyboard tests --- common/keyboard_test.go | 2 +- tests/keyboard_test.go | 62 ++++++++++++++++++++--------------------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/common/keyboard_test.go b/common/keyboard_test.go index c1664ffc4..f86a5c8e9 100644 --- a/common/keyboard_test.go +++ b/common/keyboard_test.go @@ -83,7 +83,7 @@ func TestKeyboardPress(t *testing.T) { vu := k6test.NewVU(t) k := NewKeyboard(vu.Context(), nil) - require.Error(t, k.Press("", NewKeyboardOptions())) + require.Error(t, k.Press("", KeyboardOptions{})) }) } diff --git a/tests/keyboard_test.go b/tests/keyboard_test.go index 5158ad244..c95a9f08e 100644 --- a/tests/keyboard_test.go +++ b/tests/keyboard_test.go @@ -26,7 +26,7 @@ func TestKeyboardPress(t *testing.T) { layout := keyboardlayout.GetKeyboardLayout("us") for k := range layout.Keys { - assert.NoError(t, kb.Press(string(k), common.NewKeyboardOptions())) + assert.NoError(t, kb.Press(string(k), common.KeyboardOptions{})) } }) @@ -43,12 +43,12 @@ func TestKeyboardPress(t *testing.T) { require.NoError(t, err) require.NoError(t, p.Focus("input", nil)) - require.NoError(t, kb.Type("Hello World!", common.NewKeyboardOptions())) + require.NoError(t, kb.Type("Hello World!", common.KeyboardOptions{})) v, err := el.InputValue(nil) require.NoError(t, err) require.Equal(t, "Hello World!", v) - require.NoError(t, kb.Press("Backspace", common.NewKeyboardOptions())) + require.NoError(t, kb.Press("Backspace", common.KeyboardOptions{})) v, err = el.InputValue(nil) require.NoError(t, err) assert.Equal(t, "Hello World", v) @@ -67,17 +67,17 @@ func TestKeyboardPress(t *testing.T) { require.NoError(t, err) require.NoError(t, p.Focus("input", nil)) - require.NoError(t, kb.Press("Shift++", common.NewKeyboardOptions())) - require.NoError(t, kb.Press("Shift+=", common.NewKeyboardOptions())) - require.NoError(t, kb.Press("Shift+@", common.NewKeyboardOptions())) - require.NoError(t, kb.Press("Shift+6", common.NewKeyboardOptions())) - require.NoError(t, kb.Press("Shift+KeyA", common.NewKeyboardOptions())) - require.NoError(t, kb.Press("Shift+b", common.NewKeyboardOptions())) - require.NoError(t, kb.Press("Shift+C", common.NewKeyboardOptions())) + require.NoError(t, kb.Press("Shift++", common.KeyboardOptions{})) + require.NoError(t, kb.Press("Shift+=", common.KeyboardOptions{})) + require.NoError(t, kb.Press("Shift+@", common.KeyboardOptions{})) + require.NoError(t, kb.Press("Shift+6", common.KeyboardOptions{})) + require.NoError(t, kb.Press("Shift+KeyA", common.KeyboardOptions{})) + require.NoError(t, kb.Press("Shift+b", common.KeyboardOptions{})) + require.NoError(t, kb.Press("Shift+C", common.KeyboardOptions{})) - require.NoError(t, kb.Press("Control+KeyI", common.NewKeyboardOptions())) - require.NoError(t, kb.Press("Control+J", common.NewKeyboardOptions())) - require.NoError(t, kb.Press("Control+k", common.NewKeyboardOptions())) + require.NoError(t, kb.Press("Control+KeyI", common.KeyboardOptions{})) + require.NoError(t, kb.Press("Control+J", common.KeyboardOptions{})) + require.NoError(t, kb.Press("Control+k", common.KeyboardOptions{})) v, err := el.InputValue(nil) require.NoError(t, err) @@ -166,9 +166,9 @@ func TestKeyboardPress(t *testing.T) { require.NoError(t, err) require.NoError(t, p.Focus("input", nil)) - require.NoError(t, kb.Press("Shift+KeyA", common.NewKeyboardOptions())) - require.NoError(t, kb.Press("Shift+b", common.NewKeyboardOptions())) - require.NoError(t, kb.Press("Shift+C", common.NewKeyboardOptions())) + require.NoError(t, kb.Press("Shift+KeyA", common.KeyboardOptions{})) + require.NoError(t, kb.Press("Shift+b", common.KeyboardOptions{})) + require.NoError(t, kb.Press("Shift+C", common.KeyboardOptions{})) v, err := el.InputValue(nil) require.NoError(t, err) @@ -178,8 +178,8 @@ func TestKeyboardPress(t *testing.T) { if runtime.GOOS == "darwin" { metaKey = "Meta" } - require.NoError(t, kb.Press(metaKey+"+A", common.NewKeyboardOptions())) - require.NoError(t, kb.Press("Delete", common.NewKeyboardOptions())) + require.NoError(t, kb.Press(metaKey+"+A", common.KeyboardOptions{})) + require.NoError(t, kb.Press("Delete", common.KeyboardOptions{})) v, err = el.InputValue(nil) require.NoError(t, err) assert.Equal(t, "", v) @@ -198,7 +198,7 @@ func TestKeyboardPress(t *testing.T) { require.NoError(t, err) require.NoError(t, p.Focus("textarea", nil)) - require.NoError(t, kb.Type("L+m+KeyN", common.NewKeyboardOptions())) + require.NoError(t, kb.Type("L+m+KeyN", common.KeyboardOptions{})) v, err := el.InputValue(nil) require.NoError(t, err) assert.Equal(t, "L+m+KeyN", v) @@ -217,9 +217,9 @@ func TestKeyboardPress(t *testing.T) { require.NoError(t, err) require.NoError(t, p.Focus("textarea", nil)) - require.NoError(t, kb.Press("C", common.NewKeyboardOptions())) - require.NoError(t, kb.Press("d", common.NewKeyboardOptions())) - require.NoError(t, kb.Press("KeyE", common.NewKeyboardOptions())) + require.NoError(t, kb.Press("C", common.KeyboardOptions{})) + require.NoError(t, kb.Press("d", common.KeyboardOptions{})) + require.NoError(t, kb.Press("KeyE", common.KeyboardOptions{})) require.NoError(t, kb.Down("Shift")) require.NoError(t, kb.Down("f")) @@ -249,7 +249,7 @@ func TestKeyboardPress(t *testing.T) { require.NoError(t, p.Focus("textarea", nil)) require.NoError(t, kb.Down("Shift")) - require.NoError(t, kb.Type("oPqR", common.NewKeyboardOptions())) + require.NoError(t, kb.Type("oPqR", common.KeyboardOptions{})) require.NoError(t, kb.Up("Shift")) v, err := el.InputValue(nil) @@ -270,10 +270,10 @@ func TestKeyboardPress(t *testing.T) { require.NoError(t, err) require.NoError(t, p.Focus("textarea", nil)) - require.NoError(t, kb.Type("Hello", common.NewKeyboardOptions())) - require.NoError(t, kb.Press("Enter", common.NewKeyboardOptions())) - require.NoError(t, kb.Press("Enter", common.NewKeyboardOptions())) - require.NoError(t, kb.Type("World!", common.NewKeyboardOptions())) + require.NoError(t, kb.Type("Hello", common.KeyboardOptions{})) + require.NoError(t, kb.Press("Enter", common.KeyboardOptions{})) + require.NoError(t, kb.Press("Enter", common.KeyboardOptions{})) + require.NoError(t, kb.Type("World!", common.KeyboardOptions{})) v, err := el.InputValue(nil) require.NoError(t, err) assert.Equal(t, "Hello\n\nWorld!", v) @@ -293,21 +293,21 @@ func TestKeyboardPress(t *testing.T) { require.NoError(t, err) require.NoError(t, p.Focus("input", nil)) - require.NoError(t, kb.Type("Hello World!", common.NewKeyboardOptions())) + require.NoError(t, kb.Type("Hello World!", common.KeyboardOptions{})) v, err := el.InputValue(nil) require.NoError(t, err) require.Equal(t, "Hello World!", v) - require.NoError(t, kb.Press("ArrowLeft", common.NewKeyboardOptions())) + require.NoError(t, kb.Press("ArrowLeft", common.KeyboardOptions{})) // Should hold the key until Up() is called. require.NoError(t, kb.Down("Shift")) for i := 0; i < len(" World"); i++ { - require.NoError(t, kb.Press("ArrowLeft", common.NewKeyboardOptions())) + require.NoError(t, kb.Press("ArrowLeft", common.KeyboardOptions{})) } // Should release the key but the selection should remain active. require.NoError(t, kb.Up("Shift")) // Should delete the selection. - require.NoError(t, kb.Press("Backspace", common.NewKeyboardOptions())) + require.NoError(t, kb.Press("Backspace", common.KeyboardOptions{})) require.NoError(t, err) require.NoError(t, err) From 088809e077840d05f3f1d1ed9bb4cc03bec70a01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 6 Nov 2024 16:00:22 -0500 Subject: [PATCH 07/13] Remove NewKeyboardOptions --- common/keyboard_options.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/common/keyboard_options.go b/common/keyboard_options.go index ef697b18c..ee20fd1c1 100644 --- a/common/keyboard_options.go +++ b/common/keyboard_options.go @@ -13,13 +13,6 @@ type KeyboardOptions struct { Delay int64 `json:"delay"` } -// NewKeyboardOptions returns a new KeyboardOptions. -func NewKeyboardOptions() KeyboardOptions { - return KeyboardOptions{ - Delay: 0, - } -} - // Parse parses the keyboard options. func (o *KeyboardOptions) Parse(ctx context.Context, opts sobek.Value) error { rt := k6ext.Runtime(ctx) From 8957fc05600b26bd892f1c9eebb4a427b052a5d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 6 Nov 2024 16:01:41 -0500 Subject: [PATCH 08/13] Refactor mapKeyboard.press to exportTo --- browser/keyboard_mapping.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/browser/keyboard_mapping.go b/browser/keyboard_mapping.go index 6c384427e..1355c1c21 100644 --- a/browser/keyboard_mapping.go +++ b/browser/keyboard_mapping.go @@ -23,12 +23,11 @@ func mapKeyboard(vu moduleVU, kb *common.Keyboard) mapping { }, "press": func(key string, opts sobek.Value) *sobek.Promise { return k6ext.Promise(vu.Context(), func() (any, error) { - var kbdOpts common.KeyboardOptions - if err := kbdOpts.Parse(vu.Context(), opts); err != nil { + kbopts, err := exportTo[common.KeyboardOptions](vu.Runtime(), opts) + if err != nil { return nil, fmt.Errorf("parsing keyboard options: %w", err) } - - return nil, kb.Press(key, kbdOpts) //nolint:wrapcheck + return nil, kb.Press(key, kbopts) }) }, "type": func(text string, opts sobek.Value) *sobek.Promise { From 6f94425690ccbf2b199a5718aa02bf0f48554bb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 6 Nov 2024 16:02:22 -0500 Subject: [PATCH 09/13] Refactor mapKeyboard.type to exportTo --- browser/keyboard_mapping.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/browser/keyboard_mapping.go b/browser/keyboard_mapping.go index 1355c1c21..a634fe40f 100644 --- a/browser/keyboard_mapping.go +++ b/browser/keyboard_mapping.go @@ -32,12 +32,11 @@ func mapKeyboard(vu moduleVU, kb *common.Keyboard) mapping { }, "type": func(text string, opts sobek.Value) *sobek.Promise { return k6ext.Promise(vu.Context(), func() (any, error) { - var kbdOpts common.KeyboardOptions - if err := kbdOpts.Parse(vu.Context(), opts); err != nil { + kbopts, err := exportTo[common.KeyboardOptions](vu.Runtime(), opts) + if err != nil { return nil, fmt.Errorf("parsing keyboard options: %w", err) } - - return nil, kb.Type(text, kbdOpts) //nolint:wrapcheck + return nil, kb.Type(text, kbopts) }) }, "insertText": func(text string) *sobek.Promise { From 533755a8166a6f2042e4989aac54571060d21375 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 6 Nov 2024 16:02:57 -0500 Subject: [PATCH 10/13] Refactor syncMapKeyboard.press to exportTo --- browser/sync_keyboard_mapping.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/browser/sync_keyboard_mapping.go b/browser/sync_keyboard_mapping.go index b0417d5ed..593a65511 100644 --- a/browser/sync_keyboard_mapping.go +++ b/browser/sync_keyboard_mapping.go @@ -13,12 +13,11 @@ func syncMapKeyboard(vu moduleVU, kb *common.Keyboard) mapping { "down": kb.Down, "up": kb.Up, "press": func(key string, opts sobek.Value) error { - var kbdOpts common.KeyboardOptions - if err := kbdOpts.Parse(vu.Context(), opts); err != nil { + kbopts, err := exportTo[common.KeyboardOptions](vu.Runtime(), opts) + if err != nil { return fmt.Errorf("parsing keyboard options: %w", err) } - - return kb.Press(key, kbdOpts) //nolint:wrapcheck + return kb.Press(key, kbopts) }, "type": func(text string, opts sobek.Value) error { var kbdOpts common.KeyboardOptions From 152e3bf2c91a6f38e04de37265e04eb9fae046eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 6 Nov 2024 16:03:12 -0500 Subject: [PATCH 11/13] Refactor syncMapKeyboard.type to exportTo --- browser/sync_keyboard_mapping.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/browser/sync_keyboard_mapping.go b/browser/sync_keyboard_mapping.go index 593a65511..3d6161de2 100644 --- a/browser/sync_keyboard_mapping.go +++ b/browser/sync_keyboard_mapping.go @@ -20,12 +20,11 @@ func syncMapKeyboard(vu moduleVU, kb *common.Keyboard) mapping { return kb.Press(key, kbopts) }, "type": func(text string, opts sobek.Value) error { - var kbdOpts common.KeyboardOptions - if err := kbdOpts.Parse(vu.Context(), opts); err != nil { + kbopts, err := exportTo[common.KeyboardOptions](vu.Runtime(), opts) + if err != nil { return fmt.Errorf("parsing keyboard options: %w", err) } - - return kb.Type(text, kbdOpts) //nolint:wrapcheck + return kb.Type(text, kbopts) }, "insertText": kb.InsertText, } From 8db943605506b4bffbe0d71ce394762cef697cbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 6 Nov 2024 16:03:27 -0500 Subject: [PATCH 12/13] Remove KeyboardOptions.Parse --- common/keyboard_options.go | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/common/keyboard_options.go b/common/keyboard_options.go index ee20fd1c1..1f92e027f 100644 --- a/common/keyboard_options.go +++ b/common/keyboard_options.go @@ -1,30 +1,6 @@ package common -import ( - "context" - - "github.com/grafana/sobek" - - "github.com/grafana/xk6-browser/k6ext" -) - // KeyboardOptions represents the options for the keyboard. type KeyboardOptions struct { Delay int64 `json:"delay"` } - -// Parse parses the keyboard options. -func (o *KeyboardOptions) Parse(ctx context.Context, opts sobek.Value) error { - rt := k6ext.Runtime(ctx) - if opts != nil && !sobek.IsUndefined(opts) && !sobek.IsNull(opts) { - opts := opts.ToObject(rt) - for _, k := range opts.Keys() { - switch k { - case "delay": - o.Delay = opts.Get(k).ToInteger() - } - } - } - - return nil -} From 170f7eb43a6957be2d8f6ef802629e8a77495aa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 6 Nov 2024 16:04:00 -0500 Subject: [PATCH 13/13] Move KeyboardOptions to keyboard.go --- common/keyboard.go | 5 +++++ common/keyboard_options.go | 6 ------ 2 files changed, 5 insertions(+), 6 deletions(-) delete mode 100644 common/keyboard_options.go diff --git a/common/keyboard.go b/common/keyboard.go index 80cfc3f87..255e03001 100644 --- a/common/keyboard.go +++ b/common/keyboard.go @@ -20,6 +20,11 @@ const ( ModifierKeyShift ) +// KeyboardOptions represents the options for the keyboard. +type KeyboardOptions struct { + Delay int64 `json:"delay"` +} + // Keyboard represents a keyboard input device. // Each Page has a publicly accessible Keyboard. type Keyboard struct { diff --git a/common/keyboard_options.go b/common/keyboard_options.go deleted file mode 100644 index 1f92e027f..000000000 --- a/common/keyboard_options.go +++ /dev/null @@ -1,6 +0,0 @@ -package common - -// KeyboardOptions represents the options for the keyboard. -type KeyboardOptions struct { - Delay int64 `json:"delay"` -}