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

Desobekify KeyboardOptions #1523

Merged
merged 13 commits into from
Nov 7, 2024
14 changes: 6 additions & 8 deletions browser/keyboard_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,20 @@
},
"press": func(key string, opts sobek.Value) *sobek.Promise {
return k6ext.Promise(vu.Context(), func() (any, error) {
kbdOpts := common.NewKeyboardOptions()
if err := kbdOpts.Parse(vu.Context(), opts); err != nil {
kbopts, err := exportTo[common.KeyboardOptions](vu.Runtime(), opts)

Check failure on line 26 in browser/keyboard_mapping.go

View workflow job for this annotation

GitHub Actions / lint

vu.Runtime undefined (type moduleVU has no field or method Runtime) (typecheck)
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 {
return k6ext.Promise(vu.Context(), func() (any, error) {
kbdOpts := common.NewKeyboardOptions()
if err := kbdOpts.Parse(vu.Context(), opts); err != nil {
kbopts, err := exportTo[common.KeyboardOptions](vu.Runtime(), opts)

Check failure on line 35 in browser/keyboard_mapping.go

View workflow job for this annotation

GitHub Actions / lint

vu.Runtime undefined (type moduleVU has no field or method Runtime) (typecheck)
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 {
Expand Down
14 changes: 6 additions & 8 deletions browser/sync_keyboard_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@
"down": kb.Down,
"up": kb.Up,
"press": func(key string, opts sobek.Value) error {
kbdOpts := common.NewKeyboardOptions()
if err := kbdOpts.Parse(vu.Context(), opts); err != nil {
kbopts, err := exportTo[common.KeyboardOptions](vu.Runtime(), opts)

Check failure on line 16 in browser/sync_keyboard_mapping.go

View workflow job for this annotation

GitHub Actions / lint

vu.Runtime undefined (type moduleVU has no field or method Runtime) (typecheck)
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 {
kbdOpts := common.NewKeyboardOptions()
if err := kbdOpts.Parse(vu.Context(), opts); err != nil {
kbopts, err := exportTo[common.KeyboardOptions](vu.Runtime(), opts)

Check failure on line 23 in browser/sync_keyboard_mapping.go

View workflow job for this annotation

GitHub Actions / lint

vu.Runtime undefined (type moduleVU has no field or method Runtime) (typecheck)
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,
}
Expand Down
8 changes: 4 additions & 4 deletions common/element_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 7 additions & 4 deletions common/frame_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,14 @@ func NewFramePressOptions(defaultTimeout time.Duration) *FramePressOptions {
}
}

func (o *FramePressOptions) ToKeyboardOptions() *KeyboardOptions {
o2 := NewKeyboardOptions()
// ToKeyboardOptions converts FramePressOptions to KeyboardOptions.
func (o *FramePressOptions) ToKeyboardOptions() KeyboardOptions {
var o2 KeyboardOptions
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),
Expand Down Expand Up @@ -579,8 +581,9 @@ func NewFrameTypeOptions(defaultTimeout time.Duration) *FrameTypeOptions {
}
}

func (o *FrameTypeOptions) ToKeyboardOptions() *KeyboardOptions {
o2 := NewKeyboardOptions()
// ToKeyboardOptions converts FrameTypeOptions to KeyboardOptions.
func (o *FrameTypeOptions) ToKeyboardOptions() KeyboardOptions {
var o2 KeyboardOptions
o2.Delay = o.Delay
return o2
}
Expand Down
15 changes: 10 additions & 5 deletions common/keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -62,7 +67,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)
}
Expand All @@ -83,7 +88,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)
}
Expand Down Expand Up @@ -245,7 +250,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
Expand Down Expand Up @@ -291,7 +296,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
Expand All @@ -303,7 +308,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 {
Expand Down
34 changes: 0 additions & 34 deletions common/keyboard_options.go

This file was deleted.

2 changes: 1 addition & 1 deletion common/keyboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}))
})
}

Expand Down
62 changes: 31 additions & 31 deletions tests/keyboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}))
}
})

Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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"))
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Loading