Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 6577cfd

Browse files
committed
Refactor keyboard and add documentation
1 parent edf2acb commit 6577cfd

File tree

1 file changed

+63
-57
lines changed

1 file changed

+63
-57
lines changed

common/keyboard.go

Lines changed: 63 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"github.com/grafana/xk6-browser/keyboardlayout"
3333
)
3434

35-
// Ensure Keyboard implements the api.Keyboard interface
3635
var _ api.Keyboard = &Keyboard{}
3736

3837
const (
@@ -42,26 +41,76 @@ const (
4241
ModifierKeyShift
4342
)
4443

45-
// Keyboard represents a keyboard input device
44+
// Keyboard represents a keyboard input device.
45+
// Each Page has a publicly accessible Keyboard.
4646
type Keyboard struct {
47-
ctx context.Context
48-
session *Session
49-
modifiers int64
50-
pressedKeys map[int64]bool
51-
layoutName string
47+
ctx context.Context
48+
session *Session
49+
50+
modifiers int64 // like shift, alt, ctrl, ...
51+
pressedKeys map[int64]bool // tracks keys through down() and up()
52+
layoutName string // us by default
5253
}
5354

54-
// NewKeyboard creates a new keyboard
55+
// NewKeyboard return a new keyboard with a "us" layout.
5556
func NewKeyboard(ctx context.Context, session *Session) *Keyboard {
5657
return &Keyboard{
5758
ctx: ctx,
5859
session: session,
59-
modifiers: 0,
6060
pressedKeys: make(map[int64]bool),
6161
layoutName: "us",
6262
}
6363
}
6464

65+
// Down sends a key down message to a session target.
66+
func (k *Keyboard) Down(key string) {
67+
if err := k.down(key); err != nil {
68+
k6Throw(k.ctx, "cannot send key down: %w", err)
69+
}
70+
}
71+
72+
// Up sends a key up message to a session target.
73+
func (k *Keyboard) Up(key string) {
74+
if err := k.up(key); err != nil {
75+
k6Throw(k.ctx, "cannot send key up: %w", err)
76+
}
77+
}
78+
79+
// Press sends a key press message to a session target.
80+
// It delays the action if `Delay` option is specified.
81+
// A press message is consisting of successive key down and up messages.
82+
func (k *Keyboard) Press(key string, opts goja.Value) {
83+
kbdOpts := NewKeyboardOptions()
84+
if err := kbdOpts.Parse(k.ctx, opts); err != nil {
85+
k6Throw(k.ctx, "cannot parse keyboard options: %w", err)
86+
}
87+
if err := k.press(key, kbdOpts); err != nil {
88+
k6Throw(k.ctx, "cannot press key: %w", err)
89+
}
90+
}
91+
92+
// InsertText inserts a text without dispatching key events.
93+
func (k *Keyboard) InsertText(text string) {
94+
if err := k.insertText(text); err != nil {
95+
k6Throw(k.ctx, "cannot insert text: %w", err)
96+
}
97+
}
98+
99+
// Type sends a press message to a session target for each character in text.
100+
// It delays the action if `Delay` option is specified.
101+
//
102+
// It sends an insertText message if a character is not among
103+
// valid characters in the keyboard's layout.
104+
func (k *Keyboard) Type(text string, opts goja.Value) {
105+
kbdOpts := NewKeyboardOptions()
106+
if err := kbdOpts.Parse(k.ctx, opts); err != nil {
107+
k6Throw(k.ctx, "cannot parse keyboard options: %w", err)
108+
}
109+
if err := k.typ(text, kbdOpts); err != nil {
110+
k6Throw(k.ctx, "cannot type text: %w", err)
111+
}
112+
}
113+
65114
func (k *Keyboard) down(key string) error {
66115
keyInput := keyboardlayout.KeyInput(key)
67116
layout := keyboardlayout.GetKeyboardLayout(k.layoutName)
@@ -91,7 +140,7 @@ func (k *Keyboard) down(key string) error {
91140
WithUnmodifiedText(text).
92141
WithAutoRepeat(autoRepeat)
93142
if err := action.Do(cdp.WithExecutor(k.ctx, k.session)); err != nil {
94-
return fmt.Errorf("unable to key down: %w", err)
143+
return fmt.Errorf("cannot execute dispatch key event down %w", err)
95144
}
96145

97146
return nil
@@ -115,7 +164,7 @@ func (k *Keyboard) up(key string) error {
115164
WithCode(keyDef.Code).
116165
WithLocation(keyDef.Location)
117166
if err := action.Do(cdp.WithExecutor(k.ctx, k.session)); err != nil {
118-
return fmt.Errorf("unable to key up: %w", err)
167+
return fmt.Errorf("cannot execute dispatch key event up: %w", err)
119168
}
120169

121170
return nil
@@ -124,7 +173,7 @@ func (k *Keyboard) up(key string) error {
124173
func (k *Keyboard) insertText(text string) error {
125174
action := input.InsertText(text)
126175
if err := action.Do(cdp.WithExecutor(k.ctx, k.session)); err != nil {
127-
return fmt.Errorf("cannot send character: %w", err)
176+
return fmt.Errorf("cannot execute insert text: %w", err)
128177
}
129178
return nil
130179
}
@@ -216,7 +265,7 @@ func (k *Keyboard) press(key string, opts *KeyboardOptions) error {
216265
}
217266
}
218267
if err := k.down(key); err != nil {
219-
return fmt.Errorf("cannot down: %w", err)
268+
return fmt.Errorf("cannot do key down: %w", err)
220269
}
221270
return k.up(key)
222271
}
@@ -235,7 +284,7 @@ func (k *Keyboard) typ(text string, opts *KeyboardOptions) error {
235284
keyInput := keyboardlayout.KeyInput(c)
236285
if _, ok := layout.ValidKeys[keyInput]; ok {
237286
if err := k.press(string(c), opts); err != nil {
238-
return fmt.Errorf("cannot press: %w", err)
287+
return fmt.Errorf("cannot press key: %w", err)
239288
}
240289
continue
241290
}
@@ -245,46 +294,3 @@ func (k *Keyboard) typ(text string, opts *KeyboardOptions) error {
245294
}
246295
return nil
247296
}
248-
249-
// Down
250-
func (k *Keyboard) Down(key string) {
251-
if err := k.down(key); err != nil {
252-
k6Throw(k.ctx, "cannot press down key: %w", err)
253-
}
254-
}
255-
256-
// Press
257-
func (k *Keyboard) Press(key string, opts goja.Value) {
258-
kbdOpts := NewKeyboardOptions()
259-
if err := kbdOpts.Parse(k.ctx, opts); err != nil {
260-
k6Throw(k.ctx, "cannot parse options: %w", err)
261-
}
262-
if err := k.press(key, kbdOpts); err != nil {
263-
k6Throw(k.ctx, "cannot press key: %w", err)
264-
}
265-
}
266-
267-
// InsertText
268-
func (k *Keyboard) InsertText(text string) {
269-
if err := k.insertText(text); err != nil {
270-
k6Throw(k.ctx, "cannot insert text: %w", err)
271-
}
272-
}
273-
274-
// Type
275-
func (k *Keyboard) Type(text string, opts goja.Value) {
276-
kbdOpts := NewKeyboardOptions()
277-
if err := kbdOpts.Parse(k.ctx, opts); err != nil {
278-
k6Throw(k.ctx, "cannot parse options: %w", err)
279-
}
280-
if err := k.typ(text, kbdOpts); err != nil {
281-
k6Throw(k.ctx, "cannot type text: %w", err)
282-
}
283-
}
284-
285-
// Up
286-
func (k *Keyboard) Up(key string) {
287-
if err := k.up(key); err != nil {
288-
k6Throw(k.ctx, "cannot press up key: %w", err)
289-
}
290-
}

0 commit comments

Comments
 (0)