Skip to content

Commit

Permalink
Remove Sobek from keyboard.Press
Browse files Browse the repository at this point in the history
The sobek code has been moved to the mapping layer, leaving the main
business logic sobek free for keyboard.type.
  • Loading branch information
ankur22 committed Oct 7, 2024
1 parent 131be21 commit 7294a4c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
7 changes: 6 additions & 1 deletion browser/keyboard_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 1 addition & 6 deletions common/keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
Expand Down
12 changes: 6 additions & 6 deletions tests/keyboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
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", nil))
require.NoError(t, kb.Type("oPqR", common.NewKeyboardOptions()))
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", 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)
Expand All @@ -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)
Expand Down

0 comments on commit 7294a4c

Please sign in to comment.