Skip to content

Commit

Permalink
Update the control_or_meta test
Browse files Browse the repository at this point in the history
This test now mimics a known behaviour that works. It opens a link in a
new tab and ensures that there are two pages.
  • Loading branch information
ankur22 committed Oct 4, 2024
1 parent cd94b7a commit a106f45
Showing 1 changed file with 64 additions and 32 deletions.
96 changes: 64 additions & 32 deletions tests/keyboard_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package tests

import (
"context"
_ "embed"
"runtime"
"strings"
"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 @@ -85,40 +87,70 @@ func TestKeyboardPress(t *testing.T) {
t.Run("control_or_meta", func(t *testing.T) {
t.Parallel()

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

err := p.SetContent(`<input>`, nil)
require.NoError(t, err)
el, err := p.Query("input")
require.NoError(t, err)
require.NoError(t, p.Focus("input", nil))

inputVal := "abc"

// Type abc in input field
ss := strings.Split(inputVal, "")
for _, c := range ss {
require.NoError(t, kb.Press(c, nil))
// Navigate to page1
url := tb.staticURL("page1.html")
opts := &common.FrameGotoOptions{
Timeout: common.DefaultTimeout,
}

v, err := el.InputValue(nil)
require.NoError(t, err)
require.Equal(t, inputVal, v)

// Select the text in the input element.
require.NoError(t, kb.Down("ControlOrMeta"))
require.NoError(t, el.Dblclick(nil))
require.NoError(t, kb.Up("ControlOrMeta"))

// Delete the input field.
require.NoError(t, kb.Press("Backspace", nil))

// Input field should be empty after "cut" operation.
v, err = el.InputValue(nil)
require.NoError(t, err)
require.Equal(t, "", v)
_, 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.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) {
Expand Down

0 comments on commit a106f45

Please sign in to comment.