@@ -30,7 +30,6 @@ import (
30
30
"github.com/dop251/goja"
31
31
"github.com/grafana/xk6-browser/api"
32
32
"github.com/grafana/xk6-browser/keyboardlayout"
33
- k6common "go.k6.io/k6/js/common"
34
33
)
35
34
36
35
// Ensure Keyboard implements the api.Keyboard interface
@@ -54,14 +53,13 @@ type Keyboard struct {
54
53
55
54
// NewKeyboard creates a new keyboard
56
55
func NewKeyboard (ctx context.Context , session * Session ) * Keyboard {
57
- k := & Keyboard {
56
+ return & Keyboard {
58
57
ctx : ctx ,
59
58
session : session ,
60
59
modifiers : 0 ,
61
60
pressedKeys : make (map [int64 ]bool ),
62
61
layoutName : "us" ,
63
62
}
64
- return k
65
63
}
66
64
67
65
func (k * Keyboard ) down (key string ) error {
@@ -136,22 +134,22 @@ func (k *Keyboard) keyDefinitionFromKey(keyString keyboardlayout.KeyInput) keybo
136
134
srcKeyDef , ok := layout .Keys [keyString ]
137
135
// Find based on key value instead of code
138
136
if ! ok {
139
- for k , v := range layout .Keys {
140
- if v .Key != string (keyString ) {
137
+ for key , def := range layout .Keys {
138
+ if def .Key != string (keyString ) {
141
139
continue
142
140
}
143
- keyString , srcKeyDef = k , v
141
+ keyString , srcKeyDef = key , def
144
142
ok = true // don't look for a shift key below
145
143
}
146
144
}
147
145
// try to find with the shift key value
148
146
shift := k .modifiers & ModifierKeyShift
149
147
if ! ok {
150
- for k , v := range layout .Keys {
151
- if v .ShiftKey != string (keyString ) {
148
+ for key , def := range layout .Keys {
149
+ if def .ShiftKey != string (keyString ) {
152
150
continue
153
151
}
154
- keyString , srcKeyDef = k , v
152
+ keyString , srcKeyDef = key , def
155
153
}
156
154
shift = k .modifiers | ModifierKeyShift
157
155
}
@@ -195,26 +193,20 @@ func (k *Keyboard) keyDefinitionFromKey(keyString keyboardlayout.KeyInput) keybo
195
193
}
196
194
197
195
func (k * Keyboard ) modifierBitFromKeyName (key string ) int64 {
198
- if key == "Alt" {
196
+ switch key {
197
+ case "Alt" :
199
198
return ModifierKeyAlt
200
- }
201
- if key == "Control" {
199
+ case "Control" :
202
200
return ModifierKeyControl
203
- }
204
- if key == "Meta" {
201
+ case "Meta" :
205
202
return ModifierKeyMeta
206
- }
207
- if key == "Shift" {
203
+ case "Shift" :
208
204
return ModifierKeyShift
209
205
}
210
206
return 0
211
207
}
212
208
213
209
func (k * Keyboard ) press (key string , opts * KeyboardOptions ) error {
214
- err := k .down (key )
215
- if err != nil {
216
- return err
217
- }
218
210
if opts .Delay != 0 {
219
211
t := time .NewTimer (time .Duration (opts .Delay * time .Hour .Milliseconds ()))
220
212
select {
@@ -223,91 +215,76 @@ func (k *Keyboard) press(key string, opts *KeyboardOptions) error {
223
215
case <- t .C :
224
216
}
225
217
}
226
- err = k .up (key )
227
- if err != nil {
218
+ if err := k .down (key ); err != nil {
228
219
return err
229
220
}
230
- return nil
221
+ return k . up ( key )
231
222
}
232
223
233
224
func (k * Keyboard ) typ (text string , opts * KeyboardOptions ) error {
234
225
layout := keyboardlayout .GetKeyboardLayout (k .layoutName )
235
226
for _ , c := range text {
227
+ if opts .Delay != 0 {
228
+ t := time .NewTimer (time .Duration (opts .Delay * time .Hour .Milliseconds ()))
229
+ select {
230
+ case <- k .ctx .Done ():
231
+ t .Stop ()
232
+ case <- t .C :
233
+ }
234
+ }
236
235
keyInput := keyboardlayout .KeyInput (c )
237
236
if _ , ok := layout .ValidKeys [keyInput ]; ok {
238
- err := k .press (string (c ), opts )
239
- if err != nil {
240
- return nil
241
- }
242
- } else {
243
- if opts .Delay != 0 {
244
- t := time .NewTimer (time .Duration (opts .Delay * time .Hour .Milliseconds ()))
245
- select {
246
- case <- k .ctx .Done ():
247
- t .Stop ()
248
- case <- t .C :
249
- }
250
- }
251
- err := k .insertText (string (c ))
252
- if err != nil {
253
- return nil
237
+ if err := k .press (string (c ), opts ); err != nil {
238
+ return err
254
239
}
240
+ continue
241
+ }
242
+ if err := k .insertText (string (c )); err != nil {
243
+ return err
255
244
}
256
245
}
257
246
return nil
258
247
}
259
248
260
249
// Down
261
250
func (k * Keyboard ) Down (key string ) {
262
- rt := k6common .GetRuntime (k .ctx )
263
- err := k .down (key )
264
- if err != nil {
265
- k6common .Throw (rt , err )
251
+ if err := k .down (key ); err != nil {
252
+ k6Throw (k .ctx , "cannot press down key: %w" , err )
266
253
}
267
254
}
268
255
269
256
// Press
270
257
func (k * Keyboard ) Press (key string , opts goja.Value ) {
271
- rt := k6common .GetRuntime (k .ctx )
272
258
kbdOpts := NewKeyboardOptions ()
273
259
if err := kbdOpts .Parse (k .ctx , opts ); err != nil {
274
- k6common . Throw ( rt , fmt . Errorf ( "failed parsing options: %w" , err ) )
260
+ k6Throw ( k . ctx , "cannot parse options: %w" , err )
275
261
}
276
-
277
- err := k .press (key , kbdOpts )
278
- if err != nil {
279
- k6common .Throw (rt , err )
262
+ if err := k .press (key , kbdOpts ); err != nil {
263
+ k6Throw (k .ctx , "cannot press key: %w" , err )
280
264
}
281
265
}
282
266
283
267
// InsertText
284
268
func (k * Keyboard ) InsertText (text string ) {
285
- rt := k6common .GetRuntime (k .ctx )
286
- err := k .insertText (text )
287
- if err != nil {
288
- k6common .Throw (rt , err )
269
+ if err := k .insertText (text ); err != nil {
270
+ k6Throw (k .ctx , "cannot insert text: %w" , err )
289
271
}
290
272
}
291
273
292
274
// Type
293
275
func (k * Keyboard ) Type (text string , opts goja.Value ) {
294
- rt := k6common .GetRuntime (k .ctx )
295
276
kbdOpts := NewKeyboardOptions ()
296
277
if err := kbdOpts .Parse (k .ctx , opts ); err != nil {
297
- k6common . Throw ( rt , fmt . Errorf ( "failed parsing options: %w" , err ) )
278
+ k6Throw ( k . ctx , "cannot parse options: %w" , err )
298
279
}
299
-
300
- err := k .typ (text , kbdOpts )
301
- if err != nil {
302
- k6common .Throw (rt , err )
280
+ if err := k .typ (text , kbdOpts ); err != nil {
281
+ k6Throw (k .ctx , "cannot type text: %w" , err )
303
282
}
304
283
}
305
284
306
285
// Up
307
286
func (k * Keyboard ) Up (key string ) {
308
- rt := k6common .GetRuntime (k .ctx )
309
- err := k .up (key )
310
- if err != nil {
311
- k6common .Throw (rt , err )
287
+ if err := k .up (key ); err != nil {
288
+ k6Throw (k .ctx , "cannot press up key: %w" , err )
312
289
}
313
290
}
0 commit comments