Skip to content

Commit

Permalink
Update keyboard.type to return err
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed May 8, 2024
1 parent 91e0fb7 commit 5132a18
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion browser/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ type keyboardAPI interface {
Up(key string) error
InsertText(char string) error
Press(key string, opts goja.Value) error
Type(text string, opts goja.Value)
Type(text string, opts goja.Value) error
}

// touchscreenAPI is the interface of a touchscreen.
Expand Down
8 changes: 4 additions & 4 deletions common/keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"
"time"

"github.com/grafana/xk6-browser/k6ext"
"github.com/grafana/xk6-browser/keyboardlayout"

"github.com/chromedp/cdproto/cdp"
Expand Down Expand Up @@ -89,14 +88,15 @@ 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 goja.Value) {
func (k *Keyboard) Type(text string, opts goja.Value) error {
kbdOpts := NewKeyboardOptions()
if err := kbdOpts.Parse(k.ctx, opts); err != nil {
k6ext.Panic(k.ctx, "parsing keyboard options: %w", err)
return fmt.Errorf("parsing keyboard options: %w", err)
}
if err := k.typ(text, kbdOpts); err != nil {
k6ext.Panic(k.ctx, "typing text: %w", err)
return fmt.Errorf("typing text: %w", err)
}
return nil
}

func (k *Keyboard) down(key string) error {
Expand Down
12 changes: 6 additions & 6 deletions tests/keyboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestKeyboardPress(t *testing.T) {
require.NoError(t, err)
p.Focus("input", nil)

kb.Type("Hello World!", nil)
require.NoError(t, kb.Type("Hello World!", nil))
require.Equal(t, "Hello World!", el.InputValue(nil))

require.NoError(t, kb.Press("Backspace", nil))
Expand Down Expand Up @@ -114,7 +114,7 @@ func TestKeyboardPress(t *testing.T) {
require.NoError(t, err)
p.Focus("textarea", nil)

kb.Type("L+m+KeyN", nil)
require.NoError(t, kb.Type("L+m+KeyN", nil))
assert.Equal(t, "L+m+KeyN", el.InputValue(nil))
})

Expand Down Expand Up @@ -159,7 +159,7 @@ func TestKeyboardPress(t *testing.T) {
p.Focus("textarea", nil)

require.NoError(t, kb.Down("Shift"))
kb.Type("oPqR", nil)
require.NoError(t, kb.Type("oPqR", nil))
require.NoError(t, kb.Up("Shift"))

assert.Equal(t, "oPqR", el.InputValue(nil))
Expand All @@ -177,10 +177,10 @@ func TestKeyboardPress(t *testing.T) {
require.NoError(t, err)
p.Focus("textarea", nil)

kb.Type("Hello", nil)
require.NoError(t, kb.Type("Hello", nil))
require.NoError(t, kb.Press("Enter", nil))
require.NoError(t, kb.Press("Enter", nil))
kb.Type("World!", nil)
require.NoError(t, kb.Type("World!", nil))
assert.Equal(t, "Hello\n\nWorld!", el.InputValue(nil))
})

Expand All @@ -197,7 +197,7 @@ func TestKeyboardPress(t *testing.T) {
require.NoError(t, err)
p.Focus("input", nil)

kb.Type("Hello World!", nil)
require.NoError(t, kb.Type("Hello World!", nil))
require.Equal(t, "Hello World!", el.InputValue(nil))

require.NoError(t, kb.Press("ArrowLeft", nil))
Expand Down

0 comments on commit 5132a18

Please sign in to comment.