From 35e42a94d4e7b75d2d2d02d678991780addd4d7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 8 May 2024 12:35:39 +0300 Subject: [PATCH] Update keyboard.type to return err --- browser/mapping_test.go | 2 +- common/keyboard.go | 8 ++++---- tests/keyboard_test.go | 12 ++++++------ 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/browser/mapping_test.go b/browser/mapping_test.go index 54bed2d93..9a82ba96f 100644 --- a/browser/mapping_test.go +++ b/browser/mapping_test.go @@ -528,7 +528,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. diff --git a/common/keyboard.go b/common/keyboard.go index ebcd8f73e..9bb95d3c8 100644 --- a/common/keyboard.go +++ b/common/keyboard.go @@ -6,7 +6,6 @@ import ( "strings" "time" - "github.com/grafana/xk6-browser/k6ext" "github.com/grafana/xk6-browser/keyboardlayout" "github.com/chromedp/cdproto/cdp" @@ -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 { diff --git a/tests/keyboard_test.go b/tests/keyboard_test.go index 603af1f40..dc7fa4dcd 100644 --- a/tests/keyboard_test.go +++ b/tests/keyboard_test.go @@ -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)) @@ -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)) }) @@ -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)) @@ -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)) }) @@ -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))