Skip to content

Commit

Permalink
Refactor locator.click parse
Browse files Browse the repository at this point in the history
This avoids an issue with using the goja runtime within a promise
outside of the main event loop thread, by moving the parsing of
the option to the main event loop thread.
  • Loading branch information
ankur22 committed Jan 22, 2024
1 parent ebbf9de commit b532f36
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
8 changes: 7 additions & 1 deletion browser/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,14 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping {
return lo.Clear(copts) //nolint:wrapcheck
},
"click": func(opts goja.Value) *goja.Promise {
popts, err := parseFrameClickOptions(vu.Context(), opts, lo.Timeout())

return k6ext.Promise(vu.Context(), func() (any, error) {
err := lo.Click(opts)
if err != nil {
return nil, err
}

err = lo.Click(popts)
return nil, err //nolint:wrapcheck
})
},
Expand Down
8 changes: 2 additions & 6 deletions common/locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,12 @@ func (l *Locator) Timeout() time.Duration {
}

// Click on an element using locator's selector with strict mode on.
func (l *Locator) Click(opts goja.Value) error {
func (l *Locator) Click(opts *FrameClickOptions) error {
l.log.Debugf("Locator:Click", "fid:%s furl:%q sel:%q opts:%+v", l.frame.ID(), l.frame.URL(), l.selector, opts)
_, span := TraceAPICall(l.ctx, l.frame.page.targetID.String(), "locator.click")
defer span.End()

copts := NewFrameClickOptions(l.frame.defaultTimeout())
if err := copts.Parse(l.ctx, opts); err != nil {
return fmt.Errorf("parsing click options: %w", err)
}
if err := l.click(copts); err != nil {
if err := l.click(opts); err != nil {
return fmt.Errorf("clicking on %q: %w", l.selector, err)
}

Expand Down
5 changes: 3 additions & 2 deletions tests/locator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ func TestLocator(t *testing.T) {
},
{
"Click", func(tb *testBrowser, p *common.Page) {
err := p.Locator("#link", nil).Click(nil)
l := p.Locator("#link", nil)
err := l.Click(common.NewFrameClickOptions(l.Timeout()))
require.NoError(t, err)
v := p.Evaluate(tb.toGojaValue(`() => window.result`))
require.True(t, tb.asGojaBool(v), "cannot not click the link")
Expand Down Expand Up @@ -260,7 +261,7 @@ func TestLocator(t *testing.T) {
},
{
"Click", func(l *common.Locator, tb *testBrowser) {
err := l.Click(timeout(tb))
err := l.Click(common.NewFrameClickOptions(100 * time.Millisecond))
if err != nil {
// TODO: remove panic and update tests when all locator methods return error.
panic(err)
Expand Down

0 comments on commit b532f36

Please sign in to comment.