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

Add ControlOrMeta helper key #1457

Merged
merged 3 commits into from
Oct 4, 2024
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
16 changes: 16 additions & 0 deletions common/keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package common
import (
"context"
"fmt"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -100,6 +101,8 @@ func (k *Keyboard) Type(text string, opts sobek.Value) error {
}

func (k *Keyboard) down(key string) error {
key = k.platformSpecificResolution(key)

keyInput := keyboardlayout.KeyInput(key)
if _, ok := k.layout.ValidKeys[keyInput]; !ok {
return fmt.Errorf("%q is not a valid key for layout %q", key, k.layoutName)
Expand Down Expand Up @@ -134,6 +137,8 @@ func (k *Keyboard) down(key string) error {
}

func (k *Keyboard) up(key string) error {
key = k.platformSpecificResolution(key)

keyInput := keyboardlayout.KeyInput(key)
if _, ok := k.layout.ValidKeys[keyInput]; !ok {
return fmt.Errorf("'%s' is not a valid key for layout '%s'", key, k.layoutName)
Expand Down Expand Up @@ -239,6 +244,17 @@ func (k *Keyboard) modifierBitFromKeyName(key string) int64 {
return 0
}

func (k *Keyboard) platformSpecificResolution(key string) string {
if key == "ControlOrMeta" {
if runtime.GOOS == "darwin" {
key = "Meta"
} else {
key = "Control"
}
}
return key
}

func (k *Keyboard) comboPress(keys string, opts *KeyboardOptions) error {
if opts.Delay > 0 {
if err := wait(k.ctx, opts.Delay); err != nil {
Expand Down
1 change: 1 addition & 0 deletions keyboardlayout/us.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func initUS() {
"Clear": true,
"Shift": true,
"Control": true,
"ControlOrMeta": true,
"Alt": true,
"Accept": true,
"ModeChange": true,
Expand Down
72 changes: 72 additions & 0 deletions tests/keyboard_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package tests

import (
"context"
_ "embed"
"runtime"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/grafana/xk6-browser/common"
"github.com/grafana/xk6-browser/keyboardlayout"
)

Expand Down Expand Up @@ -81,6 +84,75 @@ func TestKeyboardPress(t *testing.T) {
require.Equal(t, "+=@6AbC", v)
})

t.Run("control_or_meta", func(t *testing.T) {
t.Parallel()

tb := newTestBrowser(t, withFileServer())
p := tb.NewPage(nil)

// Navigate to page1
url := tb.staticURL("page1.html")
opts := &common.FrameGotoOptions{
Timeout: common.DefaultTimeout,
}
_, err := p.Goto(
url,
opts,
)
assert.NoError(t, err)

// Make sure the h1 header is "Page 1"
text, err := p.Locator("h1", nil).InnerText(nil)
assert.NoError(t, err)
assert.Equal(t, "Page 1", text)

ctx, cancel := context.WithTimeout(tb.context(), 5*time.Second)
defer cancel()

bc := tb.Browser.Context()
var newTab *common.Page

// We want to meta/control click the link so that it opens in a new tab.
// At the same time we will wait for a new page creation with WaitForEvent.
err = tb.run(ctx,
func() error {
var resp any
resp, err := bc.WaitForEvent("page", nil, 5*time.Second)
if err != nil {
return err
}

var ok bool
newTab, ok = resp.(*common.Page)
assert.True(t, ok)

return nil
},
func() error {
kb := p.GetKeyboard()
assert.NoError(t, kb.Down("ControlOrMeta"))
err = p.Locator(`a[href="page2.html"]`, nil).Click(common.NewFrameClickOptions(p.Timeout()))
assert.NoError(t, err)
assert.NoError(t, kb.Up("ControlOrMeta"))

return nil
},
)
assert.NoError(t, err)

// Wait for the new tab to complete loading.
assert.NoError(t, newTab.WaitForLoadState("load", nil))

// Make sure the newTab has a different h1 heading.
text, err = newTab.Locator("h1", nil).InnerText(nil)
assert.NoError(t, err)
assert.Equal(t, "Page 2", text)

// Make sure there are two pages open.
pp := bc.Pages()
assert.Len(t, pp, 2)
})

t.Run("meta", func(t *testing.T) {
t.Parallel()
t.Skip("FIXME") // See https://github.com/grafana/xk6-browser/issues/424
Expand Down
7 changes: 7 additions & 0 deletions tests/static/page1.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html lang="en">
<head></head>
<body>
<h1>Page 1</h1>
<a href="page2.html">Click Me</a>
</body>
</html>
6 changes: 6 additions & 0 deletions tests/static/page2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html lang="en">
<head></head>
<body>
<h1>Page 2</h1>
</body>
</html>
Loading