@@ -32,7 +32,6 @@ import (
32
32
"github.com/grafana/xk6-browser/keyboardlayout"
33
33
)
34
34
35
- // Ensure Keyboard implements the api.Keyboard interface
36
35
var _ api.Keyboard = & Keyboard {}
37
36
38
37
const (
@@ -42,26 +41,76 @@ const (
42
41
ModifierKeyShift
43
42
)
44
43
45
- // Keyboard represents a keyboard input device
44
+ // Keyboard represents a keyboard input device.
45
+ // Each Page has a publicly accessible Keyboard.
46
46
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
52
53
}
53
54
54
- // NewKeyboard creates a new keyboard
55
+ // NewKeyboard return a new keyboard with a "us" layout.
55
56
func NewKeyboard (ctx context.Context , session * Session ) * Keyboard {
56
57
return & Keyboard {
57
58
ctx : ctx ,
58
59
session : session ,
59
- modifiers : 0 ,
60
60
pressedKeys : make (map [int64 ]bool ),
61
61
layoutName : "us" ,
62
62
}
63
63
}
64
64
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
+
65
114
func (k * Keyboard ) down (key string ) error {
66
115
keyInput := keyboardlayout .KeyInput (key )
67
116
layout := keyboardlayout .GetKeyboardLayout (k .layoutName )
@@ -91,7 +140,7 @@ func (k *Keyboard) down(key string) error {
91
140
WithUnmodifiedText (text ).
92
141
WithAutoRepeat (autoRepeat )
93
142
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 )
95
144
}
96
145
97
146
return nil
@@ -115,7 +164,7 @@ func (k *Keyboard) up(key string) error {
115
164
WithCode (keyDef .Code ).
116
165
WithLocation (keyDef .Location )
117
166
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 )
119
168
}
120
169
121
170
return nil
@@ -124,7 +173,7 @@ func (k *Keyboard) up(key string) error {
124
173
func (k * Keyboard ) insertText (text string ) error {
125
174
action := input .InsertText (text )
126
175
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 )
128
177
}
129
178
return nil
130
179
}
@@ -216,7 +265,7 @@ func (k *Keyboard) press(key string, opts *KeyboardOptions) error {
216
265
}
217
266
}
218
267
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 )
220
269
}
221
270
return k .up (key )
222
271
}
@@ -235,7 +284,7 @@ func (k *Keyboard) typ(text string, opts *KeyboardOptions) error {
235
284
keyInput := keyboardlayout .KeyInput (c )
236
285
if _ , ok := layout .ValidKeys [keyInput ]; ok {
237
286
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 )
239
288
}
240
289
continue
241
290
}
@@ -245,46 +294,3 @@ func (k *Keyboard) typ(text string, opts *KeyboardOptions) error {
245
294
}
246
295
return nil
247
296
}
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