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 18, 2024
1 parent 2d63aa8 commit b1cbbf6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 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
2 changes: 1 addition & 1 deletion tests/locator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,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 b1cbbf6

Please sign in to comment.