diff --git a/browser/keyboard_mapping.go b/browser/keyboard_mapping.go index b3d17eba3..c129d383e 100644 --- a/browser/keyboard_mapping.go +++ b/browser/keyboard_mapping.go @@ -33,7 +33,12 @@ 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) { - return nil, kb.Type(text, opts) //nolint:wrapcheck + kbdOpts := common.NewKeyboardOptions() + if err := kbdOpts.Parse(vu.Context(), opts); err != nil { + return nil, fmt.Errorf("parsing keyboard options: %w", err) + } + + return nil, kb.Type(text, kbdOpts) //nolint:wrapcheck }) }, "insertText": func(text string) *sobek.Promise { diff --git a/common/keyboard.go b/common/keyboard.go index 00412e49c..8a26c182a 100644 --- a/common/keyboard.go +++ b/common/keyboard.go @@ -9,7 +9,6 @@ import ( "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/input" - "github.com/grafana/sobek" "github.com/grafana/xk6-browser/keyboardlayout" ) @@ -84,11 +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, opts sobek.Value) error { - kbdOpts := NewKeyboardOptions() - if err := kbdOpts.Parse(k.ctx, opts); err != nil { - return fmt.Errorf("parsing keyboard options: %w", err) - } +func (k *Keyboard) Type(text string, kbdOpts *KeyboardOptions) error { if err := k.typ(text, kbdOpts); err != nil { return fmt.Errorf("typing text: %w", err) } diff --git a/tests/keyboard_test.go b/tests/keyboard_test.go index bde4d2a5c..4f07aa028 100644 --- a/tests/keyboard_test.go +++ b/tests/keyboard_test.go @@ -43,7 +43,7 @@ func TestKeyboardPress(t *testing.T) { require.NoError(t, err) require.NoError(t, p.Focus("input", nil)) - require.NoError(t, kb.Type("Hello World!", nil)) + require.NoError(t, kb.Type("Hello World!", common.NewKeyboardOptions())) v, err := el.InputValue(nil) require.NoError(t, err) require.Equal(t, "Hello World!", 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", nil)) + require.NoError(t, kb.Type("L+m+KeyN", common.NewKeyboardOptions())) v, err := el.InputValue(nil) require.NoError(t, err) assert.Equal(t, "L+m+KeyN", v) @@ -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", nil)) + require.NoError(t, kb.Type("oPqR", common.NewKeyboardOptions())) 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", 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!", nil)) + require.NoError(t, kb.Type("World!", common.NewKeyboardOptions())) v, err := el.InputValue(nil) require.NoError(t, err) assert.Equal(t, "Hello\n\nWorld!", v) @@ -293,7 +293,7 @@ func TestKeyboardPress(t *testing.T) { require.NoError(t, err) require.NoError(t, p.Focus("input", nil)) - require.NoError(t, kb.Type("Hello World!", nil)) + require.NoError(t, kb.Type("Hello World!", common.NewKeyboardOptions())) v, err := el.InputValue(nil) require.NoError(t, err) require.Equal(t, "Hello World!", v)