diff --git a/browser/sync_keyboard_mapping.go b/browser/sync_keyboard_mapping.go new file mode 100644 index 000000000..84adfc630 --- /dev/null +++ b/browser/sync_keyboard_mapping.go @@ -0,0 +1,33 @@ +package browser + +import ( + "fmt" + + "github.com/grafana/sobek" + + "github.com/grafana/xk6-browser/common" +) + +func syncMapKeyboard(vu moduleVU, kb *common.Keyboard) mapping { + return mapping{ + "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 { + return fmt.Errorf("parsing keyboard options: %w", err) + } + + return kb.Press(key, kbdOpts) //nolint:wrapcheck + }, + "type": func(text string, opts sobek.Value) error { + kbdOpts := common.NewKeyboardOptions() + if err := kbdOpts.Parse(vu.Context(), opts); err != nil { + return fmt.Errorf("parsing keyboard options: %w", err) + } + + return kb.Type(text, kbdOpts) //nolint:wrapcheck + }, + "insertText": kb.InsertText, + } +} diff --git a/browser/sync_page_mapping.go b/browser/sync_page_mapping.go index 58b0362c9..b4455ccbd 100644 --- a/browser/sync_page_mapping.go +++ b/browser/sync_page_mapping.go @@ -103,7 +103,7 @@ func syncMapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop "isEnabled": p.IsEnabled, "isHidden": p.IsHidden, "isVisible": p.IsVisible, - "keyboard": rt.ToValue(p.GetKeyboard()).ToObject(rt), + "keyboard": syncMapKeyboard(vu, p.GetKeyboard()), "locator": func(selector string, opts sobek.Value) *sobek.Object { ml := syncMapLocator(vu, p.Locator(selector, opts)) return rt.ToValue(ml).ToObject(rt)