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

Commit 1f096a9

Browse files
committed
Refactor keyboard
1 parent 3d26136 commit 1f096a9

File tree

1 file changed

+40
-63
lines changed

1 file changed

+40
-63
lines changed

common/keyboard.go

Lines changed: 40 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/dop251/goja"
3131
"github.com/grafana/xk6-browser/api"
3232
"github.com/grafana/xk6-browser/keyboardlayout"
33-
k6common "go.k6.io/k6/js/common"
3433
)
3534

3635
// Ensure Keyboard implements the api.Keyboard interface
@@ -54,14 +53,13 @@ type Keyboard struct {
5453

5554
// NewKeyboard creates a new keyboard
5655
func NewKeyboard(ctx context.Context, session *Session) *Keyboard {
57-
k := &Keyboard{
56+
return &Keyboard{
5857
ctx: ctx,
5958
session: session,
6059
modifiers: 0,
6160
pressedKeys: make(map[int64]bool),
6261
layoutName: "us",
6362
}
64-
return k
6563
}
6664

6765
func (k *Keyboard) down(key string) error {
@@ -136,22 +134,22 @@ func (k *Keyboard) keyDefinitionFromKey(keyString keyboardlayout.KeyInput) keybo
136134
srcKeyDef, ok := layout.Keys[keyString]
137135
// Find based on key value instead of code
138136
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) {
141139
continue
142140
}
143-
keyString, srcKeyDef = k, v
141+
keyString, srcKeyDef = key, def
144142
ok = true // don't look for a shift key below
145143
}
146144
}
147145
// try to find with the shift key value
148146
shift := k.modifiers & ModifierKeyShift
149147
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) {
152150
continue
153151
}
154-
keyString, srcKeyDef = k, v
152+
keyString, srcKeyDef = key, def
155153
}
156154
shift = k.modifiers | ModifierKeyShift
157155
}
@@ -195,26 +193,20 @@ func (k *Keyboard) keyDefinitionFromKey(keyString keyboardlayout.KeyInput) keybo
195193
}
196194

197195
func (k *Keyboard) modifierBitFromKeyName(key string) int64 {
198-
if key == "Alt" {
196+
switch key {
197+
case "Alt":
199198
return ModifierKeyAlt
200-
}
201-
if key == "Control" {
199+
case "Control":
202200
return ModifierKeyControl
203-
}
204-
if key == "Meta" {
201+
case "Meta":
205202
return ModifierKeyMeta
206-
}
207-
if key == "Shift" {
203+
case "Shift":
208204
return ModifierKeyShift
209205
}
210206
return 0
211207
}
212208

213209
func (k *Keyboard) press(key string, opts *KeyboardOptions) error {
214-
err := k.down(key)
215-
if err != nil {
216-
return err
217-
}
218210
if opts.Delay != 0 {
219211
t := time.NewTimer(time.Duration(opts.Delay * time.Hour.Milliseconds()))
220212
select {
@@ -223,91 +215,76 @@ func (k *Keyboard) press(key string, opts *KeyboardOptions) error {
223215
case <-t.C:
224216
}
225217
}
226-
err = k.up(key)
227-
if err != nil {
218+
if err := k.down(key); err != nil {
228219
return err
229220
}
230-
return nil
221+
return k.up(key)
231222
}
232223

233224
func (k *Keyboard) typ(text string, opts *KeyboardOptions) error {
234225
layout := keyboardlayout.GetKeyboardLayout(k.layoutName)
235226
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+
}
236235
keyInput := keyboardlayout.KeyInput(c)
237236
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
254239
}
240+
continue
241+
}
242+
if err := k.insertText(string(c)); err != nil {
243+
return err
255244
}
256245
}
257246
return nil
258247
}
259248

260249
// Down
261250
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)
266253
}
267254
}
268255

269256
// Press
270257
func (k *Keyboard) Press(key string, opts goja.Value) {
271-
rt := k6common.GetRuntime(k.ctx)
272258
kbdOpts := NewKeyboardOptions()
273259
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)
275261
}
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)
280264
}
281265
}
282266

283267
// InsertText
284268
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)
289271
}
290272
}
291273

292274
// Type
293275
func (k *Keyboard) Type(text string, opts goja.Value) {
294-
rt := k6common.GetRuntime(k.ctx)
295276
kbdOpts := NewKeyboardOptions()
296277
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)
298279
}
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)
303282
}
304283
}
305284

306285
// Up
307286
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)
312289
}
313290
}

0 commit comments

Comments
 (0)