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 locator.click parse #1164

Merged
merged 3 commits into from
Jan 22, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions browser/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"time"

"github.com/dop251/goja"

Expand Down Expand Up @@ -51,11 +52,15 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping {

return lo.Clear(copts) //nolint:wrapcheck
},
"click": func(opts goja.Value) *goja.Promise {
"click": func(opts goja.Value) (*goja.Promise, error) {
popts, err := parseFrameClickOptions(vu.Context(), opts, lo.Timeout())
if err != nil {
return nil, err
}

return k6ext.Promise(vu.Context(), func() (any, error) {
err := lo.Click(opts)
return nil, err //nolint:wrapcheck
})
return nil, lo.Click(popts) //nolint:wrapcheck
}), nil
},
"dblclick": lo.Dblclick,
"check": lo.Check,
Expand Down Expand Up @@ -83,6 +88,16 @@ func mapLocator(vu moduleVU, lo *common.Locator) mapping {
}
}

func parseFrameClickOptions(
ctx context.Context, opts goja.Value, defaultTimeout time.Duration,
) (*common.FrameClickOptions, error) {
copts := common.NewFrameClickOptions(defaultTimeout)
if err := copts.Parse(ctx, opts); err != nil {
return nil, fmt.Errorf("parsing click options: %w", err)
}
return copts, nil
}

// mapRequest to the JS module.
func mapRequest(vu moduleVU, r *common.Request) mapping {
rt := vu.Runtime()
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
Loading