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

Refactor page.click option parsing #1175

Merged
merged 2 commits into from
Jan 23, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions browser/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,11 +475,16 @@ func mapPage(vu moduleVU, p *common.Page) mapping {
"addStyleTag": p.AddStyleTag,
"bringToFront": p.BringToFront,
"check": p.Check,
"click": func(selector string, opts goja.Value) *goja.Promise {
"click": func(selector string, opts goja.Value) (*goja.Promise, error) {
popts, err := parseFrameClickOptions(vu.Context(), opts, p.Timeout())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: s/popts/clickOpts

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do a mass renaming later if we need. Sticking with popts for now.

if err != nil {
return nil, err
}

return k6ext.Promise(vu.Context(), func() (any, error) {
err := p.Click(selector, opts)
err := p.Click(selector, popts)
return nil, err //nolint:wrapcheck
})
}), nil
},
"close": func(opts goja.Value) error {
vu.taskQueueRegistry.close(p.TargetID())
Expand Down
15 changes: 8 additions & 7 deletions common/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,15 +649,10 @@ func (p *Page) IsChecked(selector string, opts goja.Value) bool {
}

// Click clicks an element matching provided selector.
func (p *Page) Click(selector string, opts goja.Value) error {
func (p *Page) Click(selector string, opts *FrameClickOptions) error {
p.logger.Debugf("Page:Click", "sid:%v selector:%s", p.sessionID(), selector)

popts := NewFrameClickOptions(p.defaultTimeout())
if err := popts.Parse(p.ctx, opts); err != nil {
k6ext.Panic(p.ctx, "parsing click options %q: %w", selector, err)
}

return p.MainFrame().Click(selector, popts)
return p.MainFrame().Click(selector, opts)
}

// Close closes the page.
Expand Down Expand Up @@ -1195,6 +1190,12 @@ func (p *Page) TextContent(selector string, opts goja.Value) string {
return p.MainFrame().TextContent(selector, opts)
}

// Timeout will return the default timeout or the one set by the user.
// It's an internal method not to be exposed as a JS API.
func (p *Page) Timeout() time.Duration {
return p.defaultTimeout()
}

func (p *Page) Title() string {
p.logger.Debugf("Page:Title", "sid:%v", p.sessionID())

Expand Down
4 changes: 2 additions & 2 deletions tests/element_handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func TestElementHandleClickConcealedLink(t *testing.T) {
require.NoError(t, err)
require.Equal(t, wantBefore, clickResult())

err = p.Click("#concealed", nil)
err = p.Click("#concealed", common.NewFrameClickOptions(p.Timeout()))
require.NoError(t, err)
require.Equal(t, wantAfter, clickResult())
}
Expand All @@ -218,7 +218,7 @@ func TestElementHandleNonClickable(t *testing.T) {
require.NotNil(t, resp)
require.NoError(t, err)

err = p.Click("#non-clickable", nil)
err = p.Click("#non-clickable", common.NewFrameClickOptions(p.Timeout()))
require.Errorf(t, err, "element should not be clickable")
}

Expand Down
4 changes: 2 additions & 2 deletions tests/frame_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestWaitForFrameNavigationWithinDocument(t *testing.T) {
return err
}
click := func() error {
return p.Click(tc.selector, nil)
return p.Click(tc.selector, common.NewFrameClickOptions(p.Timeout()))
}
ctx, cancel := context.WithTimeout(tb.ctx, timeout)
defer cancel()
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestWaitForFrameNavigation(t *testing.T) {
return err
}
click := func() error {
return p.Click(`a`, nil)
return p.Click(`a`, common.NewFrameClickOptions(p.Timeout()))
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
Expand Down
5 changes: 3 additions & 2 deletions tests/frame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

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

Expand Down Expand Up @@ -60,7 +61,7 @@ func TestFrameDismissDialogBox(t *testing.T) {
require.NoError(t, err)

if tt == "beforeunload" {
err = p.Click("#clickHere", nil)
err = p.Click("#clickHere", common.NewFrameClickOptions(p.Timeout()))
require.NoError(t, err)
}

Expand Down Expand Up @@ -136,7 +137,7 @@ func TestFrameNoPanicNavigateAndClickOnPageWithIFrames(t *testing.T) {

err = tb.run(
ctx,
func() error { return p.Click(`a[href="/iframeSignIn"]`, nil) },
func() error { return p.Click(`a[href="/iframeSignIn"]`, common.NewFrameClickOptions(p.Timeout())) },
func() error { _, err := p.WaitForNavigation(nil); return err },
)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion tests/launch_options_slowmo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestBrowserOptionsSlowMo(t *testing.T) {
t.Parallel()
tb := newTestBrowser(t, withFileServer())
testPageSlowMoImpl(t, tb, func(_ *testBrowser, p *common.Page) {
err := p.Click("button", nil)
err := p.Click("button", common.NewFrameClickOptions(p.Timeout()))
assert.NoError(t, err)
})
})
Expand Down
2 changes: 1 addition & 1 deletion tests/lifecycle_wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func TestLifecycleWaitForNavigation(t *testing.T) {
return err
}
click := func() error {
return p.Click(`a`, nil)
return p.Click(`a`, common.NewFrameClickOptions(p.Timeout()))
}

ctx, cancel := context.WithTimeout(tb.ctx, 5*time.Second)
Expand Down
4 changes: 1 addition & 3 deletions tests/locator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,11 +474,9 @@ func TestLocatorShadowDOM(t *testing.T) {

tb := newTestBrowser(t, withFileServer())
p := tb.NewPage(nil)
opts := tb.toGojaValue(jsFrameBaseOpts{Timeout: "1000"})

_, err := p.Goto(tb.staticURL("shadow_dom_link.html"), nil)
require.NoError(t, err)

err = p.Click("#inner-link", opts)
err = p.Click("#inner-link", common.NewFrameClickOptions(time.Second))
require.NoError(t, err)
}
4 changes: 3 additions & 1 deletion tests/webvital_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

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

k6metrics "go.k6.io/k6/metrics"
)

Expand Down Expand Up @@ -61,7 +63,7 @@ func TestWebVitalMetric(t *testing.T) {
// also helps the web vital library to measure CLS.
err = browser.run(
ctx,
func() error { return page.Click("#clickMe", nil) },
func() error { return page.Click("#clickMe", common.NewFrameClickOptions(page.Timeout())) },
func() error { _, err := page.WaitForNavigation(nil); return err },
)
require.NoError(t, err)
Expand Down