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

Commit d57dba3

Browse files
committed
Refactor parse our of page.click
This helps avoid using the goja runtime when it comes to parsing the options. The options parsing has been moved to the mapping layer and so has been moved back on to the main goja thread, which will mitigate the possibility of a panic which could occur when multiple goroutines try to work with the goja runtime (which is not thread safe). There is another issue to tackle more of the goja refactoring in
1 parent 73563ba commit d57dba3

9 files changed

+23
-22
lines changed

browser/mapping.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,16 @@ func mapPage(vu moduleVU, p *common.Page) mapping {
475475
"addStyleTag": p.AddStyleTag,
476476
"bringToFront": p.BringToFront,
477477
"check": p.Check,
478-
"click": func(selector string, opts goja.Value) *goja.Promise {
478+
"click": func(selector string, opts goja.Value) (*goja.Promise, error) {
479+
popts, err := parseFrameClickOptions(vu.Context(), opts, p.Timeout())
480+
if err != nil {
481+
return nil, err
482+
}
483+
479484
return k6ext.Promise(vu.Context(), func() (any, error) {
480-
err := p.Click(selector, opts)
485+
err := p.Click(selector, popts)
481486
return nil, err //nolint:wrapcheck
482-
})
487+
}), nil
483488
},
484489
"close": func(opts goja.Value) error {
485490
vu.taskQueueRegistry.close(p.TargetID())

common/page.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -649,15 +649,10 @@ func (p *Page) IsChecked(selector string, opts goja.Value) bool {
649649
}
650650

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

655-
popts := NewFrameClickOptions(p.defaultTimeout())
656-
if err := popts.Parse(p.ctx, opts); err != nil {
657-
k6ext.Panic(p.ctx, "parsing click options %q: %w", selector, err)
658-
}
659-
660-
return p.MainFrame().Click(selector, popts)
655+
return p.MainFrame().Click(selector, opts)
661656
}
662657

663658
// Close closes the page.

tests/element_handle_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func TestElementHandleClickConcealedLink(t *testing.T) {
199199
require.NoError(t, err)
200200
require.Equal(t, wantBefore, clickResult())
201201

202-
err = p.Click("#concealed", nil)
202+
err = p.Click("#concealed", common.NewFrameClickOptions(p.Timeout()))
203203
require.NoError(t, err)
204204
require.Equal(t, wantAfter, clickResult())
205205
}
@@ -218,7 +218,7 @@ func TestElementHandleNonClickable(t *testing.T) {
218218
require.NotNil(t, resp)
219219
require.NoError(t, err)
220220

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

tests/frame_manager_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestWaitForFrameNavigationWithinDocument(t *testing.T) {
4949
return err
5050
}
5151
click := func() error {
52-
return p.Click(tc.selector, nil)
52+
return p.Click(tc.selector, common.NewFrameClickOptions(p.Timeout()))
5353
}
5454
ctx, cancel := context.WithTimeout(tb.ctx, timeout)
5555
defer cancel()
@@ -105,7 +105,7 @@ func TestWaitForFrameNavigation(t *testing.T) {
105105
return err
106106
}
107107
click := func() error {
108-
return p.Click(`a`, nil)
108+
return p.Click(`a`, common.NewFrameClickOptions(p.Timeout()))
109109
}
110110
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
111111
defer cancel()

tests/frame_test.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/stretchr/testify/assert"
1010
"github.com/stretchr/testify/require"
1111

12+
"github.com/grafana/xk6-browser/common"
1213
"github.com/grafana/xk6-browser/env"
1314
)
1415

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

6263
if tt == "beforeunload" {
63-
err = p.Click("#clickHere", nil)
64+
err = p.Click("#clickHere", common.NewFrameClickOptions(p.Timeout()))
6465
require.NoError(t, err)
6566
}
6667

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

137138
err = tb.run(
138139
ctx,
139-
func() error { return p.Click(`a[href="/iframeSignIn"]`, nil) },
140+
func() error { return p.Click(`a[href="/iframeSignIn"]`, common.NewFrameClickOptions(p.Timeout())) },
140141
func() error { _, err := p.WaitForNavigation(nil); return err },
141142
)
142143
require.NoError(t, err)

tests/launch_options_slowmo_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestBrowserOptionsSlowMo(t *testing.T) {
3131
t.Parallel()
3232
tb := newTestBrowser(t, withFileServer())
3333
testPageSlowMoImpl(t, tb, func(_ *testBrowser, p *common.Page) {
34-
err := p.Click("button", nil)
34+
err := p.Click("button", common.NewFrameClickOptions(p.Timeout()))
3535
assert.NoError(t, err)
3636
})
3737
})

tests/lifecycle_wait_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func TestLifecycleWaitForNavigation(t *testing.T) {
175175
return err
176176
}
177177
click := func() error {
178-
return p.Click(`a`, nil)
178+
return p.Click(`a`, common.NewFrameClickOptions(p.Timeout()))
179179
}
180180

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

tests/locator_test.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -474,11 +474,9 @@ func TestLocatorShadowDOM(t *testing.T) {
474474

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

479478
_, err := p.Goto(tb.staticURL("shadow_dom_link.html"), nil)
480479
require.NoError(t, err)
481-
482-
err = p.Click("#inner-link", opts)
480+
err = p.Click("#inner-link", common.NewFrameClickOptions(time.Second))
483481
require.NoError(t, err)
484482
}

tests/webvital_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88
"github.com/stretchr/testify/assert"
99
"github.com/stretchr/testify/require"
1010

11+
"github.com/grafana/xk6-browser/common"
12+
1113
k6metrics "go.k6.io/k6/metrics"
1214
)
1315

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

0 commit comments

Comments
 (0)