From eb1d417c445298bf131e85a716f77008f236ad05 Mon Sep 17 00:00:00 2001 From: ankur22 Date: Mon, 7 Oct 2024 13:50:13 +0100 Subject: [PATCH] Add syncMapKeyboard This is to enable us to still work with the sync keyboard APIs with the Sobek changes removed from the business logic side of keyboard. --- browser/sync_keyboard_mapping.go | 33 ++++++++++++++++++++++++++++++++ browser/sync_page_mapping.go | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 browser/sync_keyboard_mapping.go 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)