Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect keyboard key code on down and up #807

Merged
merged 4 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions common/keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ func (k *Keyboard) keyDefinitionFromKey(key keyboardlayout.KeyInput) keyboardlay
}

var keyDef keyboardlayout.KeyDefinition
keyDef.Code = srcKeyDef.Code
if srcKeyDef.Key != "" {
keyDef.Key = srcKeyDef.Key
}
Expand All @@ -194,9 +195,6 @@ func (k *Keyboard) keyDefinitionFromKey(key keyboardlayout.KeyInput) keyboardlay
if srcKeyDef.KeyCode != 0 {
keyDef.KeyCode = srcKeyDef.KeyCode
}
if key != "" {
keyDef.Code = string(key)
}
if srcKeyDef.Location != 0 {
keyDef.Location = srcKeyDef.Location
}
Expand Down
108 changes: 108 additions & 0 deletions common/keyboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/grafana/xk6-browser/k6ext/k6test"
"github.com/grafana/xk6-browser/keyboardlayout"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -76,3 +77,110 @@ func TestKeyboardPress(t *testing.T) {
assert.Panics(t, func() { k.Press("", nil) })
})
}

func TestKeyDefinitionCode(t *testing.T) {
var (
vu = k6test.NewVU(t)
k = NewKeyboard(vu.Context(), nil)
)

tests := []struct {
key keyboardlayout.KeyInput
expectedCodes []string
}{
{key: "Escape", expectedCodes: []string{"Escape"}},
{key: "F1", expectedCodes: []string{"F1"}},
{key: "F2", expectedCodes: []string{"F2"}},
{key: "F3", expectedCodes: []string{"F3"}},
{key: "F4", expectedCodes: []string{"F4"}},
{key: "F5", expectedCodes: []string{"F5"}},
{key: "F6", expectedCodes: []string{"F6"}},
{key: "F7", expectedCodes: []string{"F7"}},
{key: "F8", expectedCodes: []string{"F8"}},
{key: "F9", expectedCodes: []string{"F9"}},
{key: "F10", expectedCodes: []string{"F10"}},
{key: "F11", expectedCodes: []string{"F11"}},
{key: "F12", expectedCodes: []string{"F12"}},
{key: "`", expectedCodes: []string{"Backquote"}},
{key: "-", expectedCodes: []string{"Minus", "NumpadSubtract"}},
{key: "=", expectedCodes: []string{"Equal"}},
{key: "\\", expectedCodes: []string{"Backslash"}},
{key: "Backspace", expectedCodes: []string{"Backspace"}},
{key: "Tab", expectedCodes: []string{"Tab"}},
{key: "q", expectedCodes: []string{"KeyQ"}},
{key: "w", expectedCodes: []string{"KeyW"}},
{key: "e", expectedCodes: []string{"KeyE"}},
{key: "r", expectedCodes: []string{"KeyR"}},
{key: "t", expectedCodes: []string{"KeyT"}},
{key: "y", expectedCodes: []string{"KeyY"}},
{key: "u", expectedCodes: []string{"KeyU"}},
{key: "i", expectedCodes: []string{"KeyI"}},
{key: "o", expectedCodes: []string{"KeyO"}},
{key: "p", expectedCodes: []string{"KeyP"}},
{key: "[", expectedCodes: []string{"BracketLeft"}},
{key: "]", expectedCodes: []string{"BracketRight"}},
{key: "CapsLock", expectedCodes: []string{"CapsLock"}},
{key: "a", expectedCodes: []string{"KeyA"}},
{key: "s", expectedCodes: []string{"KeyS"}},
{key: "d", expectedCodes: []string{"KeyD"}},
{key: "f", expectedCodes: []string{"KeyF"}},
{key: "g", expectedCodes: []string{"KeyG"}},
{key: "h", expectedCodes: []string{"KeyH"}},
{key: "j", expectedCodes: []string{"KeyJ"}},
{key: "k", expectedCodes: []string{"KeyK"}},
{key: "l", expectedCodes: []string{"KeyL"}},
{key: ";", expectedCodes: []string{"Semicolon"}},
{key: "'", expectedCodes: []string{"Quote"}},
{key: "Shift", expectedCodes: []string{"ShiftLeft", "ShiftRight"}},
{key: "z", expectedCodes: []string{"KeyZ"}},
{key: "x", expectedCodes: []string{"KeyX"}},
{key: "c", expectedCodes: []string{"KeyC"}},
{key: "v", expectedCodes: []string{"KeyV"}},
{key: "b", expectedCodes: []string{"KeyB"}},
{key: "n", expectedCodes: []string{"KeyN"}},
{key: "m", expectedCodes: []string{"KeyM"}},
{key: ",", expectedCodes: []string{"Comma"}},
{key: "/", expectedCodes: []string{"Slash", "NumpadDivide"}},
{key: "Control", expectedCodes: []string{"ControlLeft", "ControlRight"}},
{key: "Meta", expectedCodes: []string{"MetaLeft", "MetaRight"}},
{key: "Alt", expectedCodes: []string{"AltLeft", "AltRight"}},
{key: " ", expectedCodes: []string{"Space"}},
{key: "AltGraph", expectedCodes: []string{"AltGraph"}},
{key: "ConTextMenu", expectedCodes: []string{"ConTextMenu"}},
{key: "PrintScreen", expectedCodes: []string{"PrintScreen"}},
{key: "ScrollLock", expectedCodes: []string{"ScrollLock"}},
{key: "Pause", expectedCodes: []string{"Pause"}},
{key: "PageUp", expectedCodes: []string{"PageUp"}},
{key: "PageDown", expectedCodes: []string{"PageDown"}},
{key: "Insert", expectedCodes: []string{"Insert"}},
{key: "Delete", expectedCodes: []string{"Delete"}},
{key: "Home", expectedCodes: []string{"Home"}},
{key: "End", expectedCodes: []string{"End"}},
{key: "ArrowLeft", expectedCodes: []string{"ArrowLeft"}},
{key: "ArrowUp", expectedCodes: []string{"ArrowUp"}},
{key: "ArrowRight", expectedCodes: []string{"ArrowRight"}},
{key: "ArrowDown", expectedCodes: []string{"ArrowDown"}},
{key: "NumLock", expectedCodes: []string{"NumLock"}},
{key: "*", expectedCodes: []string{"NumpadMultiply"}},
{key: "7", expectedCodes: []string{"Numpad7", "Digit7"}},
{key: "8", expectedCodes: []string{"Numpad8", "Digit8"}},
{key: "9", expectedCodes: []string{"Numpad9", "Digit9"}},
{key: "4", expectedCodes: []string{"Numpad4", "Digit4"}},
{key: "5", expectedCodes: []string{"Numpad5", "Digit5"}},
{key: "6", expectedCodes: []string{"Numpad6", "Digit6"}},
{key: "+", expectedCodes: []string{"NumpadAdd"}},
{key: "1", expectedCodes: []string{"Numpad1", "Digit1"}},
{key: "2", expectedCodes: []string{"Numpad2", "Digit2"}},
{key: "3", expectedCodes: []string{"Numpad3", "Digit3"}},
{key: "0", expectedCodes: []string{"Numpad0", "Digit0"}},
{key: ".", expectedCodes: []string{"NumpadDecimal", "Period"}},
{key: "Enter", expectedCodes: []string{"NumpadEnter", "Enter"}},
}

for _, tt := range tests {
t.Run(string(tt.key), func(t *testing.T) {
kd := k.keyDefinitionFromKey(tt.key)
assert.Contains(t, tt.expectedCodes, kd.Code)
})
}
}
Loading