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

Commit 3d26136

Browse files
committed
Add shift key detection to keyboard
1 parent b3ddf70 commit 3d26136

File tree

2 files changed

+43
-28
lines changed

2 files changed

+43
-28
lines changed

common/keyboard.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,23 +132,31 @@ func (k *Keyboard) insertText(text string) error {
132132
}
133133

134134
func (k *Keyboard) keyDefinitionFromKey(keyString keyboardlayout.KeyInput) keyboardlayout.KeyDefinition {
135-
shift := k.modifiers & ModifierKeyShift
136-
keyDef := keyboardlayout.KeyDefinition{}
137135
layout := keyboardlayout.GetKeyboardLayout(k.layoutName)
138-
code := keyString
139136
srcKeyDef, ok := layout.Keys[keyString]
140-
137+
// Find based on key value instead of code
141138
if !ok {
142-
// Find based on key value instead of code
143139
for k, v := range layout.Keys {
144-
if v.Key == string(keyString) {
145-
code = k
146-
srcKeyDef = v
147-
break
140+
if v.Key != string(keyString) {
141+
continue
148142
}
143+
keyString, srcKeyDef = k, v
144+
ok = true // don't look for a shift key below
149145
}
150146
}
147+
// try to find with the shift key value
148+
shift := k.modifiers & ModifierKeyShift
149+
if !ok {
150+
for k, v := range layout.Keys {
151+
if v.ShiftKey != string(keyString) {
152+
continue
153+
}
154+
keyString, srcKeyDef = k, v
155+
}
156+
shift = k.modifiers | ModifierKeyShift
157+
}
151158

159+
var keyDef keyboardlayout.KeyDefinition
152160
if srcKeyDef.Key != "" {
153161
keyDef.Key = srcKeyDef.Key
154162
}
@@ -164,8 +172,8 @@ func (k *Keyboard) keyDefinitionFromKey(keyString keyboardlayout.KeyInput) keybo
164172
if srcKeyDef.KeyCode != 0 {
165173
keyDef.KeyCode = srcKeyDef.KeyCode
166174
}
167-
if code != "" {
168-
keyDef.Code = string(code)
175+
if keyString != "" {
176+
keyDef.Code = string(keyString)
169177
}
170178
if srcKeyDef.Location != 0 {
171179
keyDef.Location = srcKeyDef.Location
@@ -176,15 +184,13 @@ func (k *Keyboard) keyDefinitionFromKey(keyString keyboardlayout.KeyInput) keybo
176184
if srcKeyDef.Text != "" {
177185
keyDef.Text = srcKeyDef.Text
178186
}
179-
if shift != 0 && keyDef.ShiftKey != "" {
180-
keyDef.Text = keyDef.ShiftKey
187+
if shift != 0 && srcKeyDef.ShiftKey != "" {
188+
keyDef.Text = srcKeyDef.ShiftKey
181189
}
182-
183190
// If any modifiers besides shift are pressed, no text should be sent
184191
if k.modifiers & ^ModifierKeyShift != 0 {
185192
keyDef.Text = ""
186193
}
187-
188194
return keyDef
189195
}
190196

tests/page_input_value_test.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,36 @@ func testPageInputValue(t *testing.T, b api.Browser) {
5555
<textarea>hello3</textarea>
5656
`, nil)
5757

58-
value := p.InputValue("input", nil)
59-
assert.Equal(t, value, "hello1")
58+
got, want := p.InputValue("input", nil), "hello1"
59+
assert.Equal(t, got, want)
6060

61-
value = p.InputValue("select", nil)
62-
assert.Equal(t, value, "hello2")
61+
got, want = p.InputValue("select", nil), "hello2"
62+
assert.Equal(t, got, want)
6363

64-
value = p.InputValue("textarea", nil)
65-
assert.Equal(t, value, "hello3")
64+
got, want = p.InputValue("textarea", nil), "hello3"
65+
assert.Equal(t, got, want)
6666
}
6767

6868
// test for: https://github.com/grafana/xk6-browser/issues/132
6969
func testPageInputSpecialCharacters(t *testing.T, b api.Browser) {
70-
const want = "test@k6.io"
71-
7270
p := b.NewPage(nil)
7371
defer p.Close(nil)
7472

75-
p.SetContent(`<input id="username">`, nil)
76-
el := p.Query("#username")
77-
el.Type(want, nil)
73+
p.SetContent(`<input id="special">`, nil)
74+
el := p.Query("#special")
75+
76+
wants := []string{
77+
"test@k6.io",
78+
"<hello WoRlD \\/>",
79+
"{(hello world!)}",
80+
"!#$%^&*()+_|~±",
81+
`¯\_(ツ)_/¯`,
82+
}
83+
for _, want := range wants {
84+
el.Fill("", nil)
85+
el.Type(want, nil)
7886

79-
got := el.InputValue(nil)
80-
assert.Equal(t, want, got)
87+
got := el.InputValue(nil)
88+
assert.Equal(t, want, got)
89+
}
8190
}

0 commit comments

Comments
 (0)