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 APIs that are traced #1259

Merged
merged 3 commits into from
Apr 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
12 changes: 9 additions & 3 deletions browser/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,9 +692,15 @@ func mapPage(vu moduleVU, p *common.Page) mapping {
"pause": p.Pause,
"pdf": p.Pdf,
"press": p.Press,
"reload": func(opts goja.Value) *goja.Object {
r := mapResponse(vu, p.Reload(opts))
return rt.ToValue(r).ToObject(rt)
"reload": func(opts goja.Value) (*goja.Object, error) {
resp, err := p.Reload(opts)
if err != nil {
return nil, err //nolint:wrapcheck
}

r := mapResponse(vu, resp)

return rt.ToValue(r).ToObject(rt), nil
},
"route": p.Route,
"screenshot": func(opts goja.Value) (*goja.ArrayBuffer, error) {
Expand Down
4 changes: 2 additions & 2 deletions common/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,12 +557,12 @@ func (b *Browser) NewContext(opts goja.Value) (*BrowserContext, error) {
browserContextID, err := action.Do(cdp.WithExecutor(b.ctx, b.conn))
b.logger.Debugf("Browser:NewContext", "bctxid:%v", browserContextID)
if err != nil {
k6ext.Panic(b.ctx, "creating browser context ID %s: %w", browserContextID, err)
return nil, fmt.Errorf("creating browser context ID %s: %w", browserContextID, err)
}

browserCtxOpts := NewBrowserContextOptions()
if err := browserCtxOpts.Parse(b.ctx, opts); err != nil {
k6ext.Panic(b.ctx, "parsing newContext options: %w", err)
return nil, fmt.Errorf("parsing newContext options: %w", err)
}

browserCtx, err := NewBrowserContext(b.ctx, b, browserContextID, browserCtxOpts, b.logger)
Expand Down
19 changes: 9 additions & 10 deletions common/locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,26 +509,25 @@ func (l *Locator) press(key string, opts *FramePressOptions) error {

// Type text on the element found that matches the locator's
// selector with strict mode on.
func (l *Locator) Type(text string, opts goja.Value) {
func (l *Locator) Type(text string, opts goja.Value) error {
l.log.Debugf(
"Locator:Type", "fid:%s furl:%q sel:%q text:%q opts:%+v",
l.frame.ID(), l.frame.URL(), l.selector, text, opts,
)
_, span := TraceAPICall(l.ctx, l.frame.page.targetID.String(), "locator.type")
defer span.End()

var err error
defer func() { panicOrSlowMo(l.ctx, err) }()

copts := NewFrameTypeOptions(l.frame.defaultTimeout())
if err = copts.Parse(l.ctx, opts); err != nil {
err = fmt.Errorf("parsing type options: %w", err)
return
if err := copts.Parse(l.ctx, opts); err != nil {
return fmt.Errorf("parsing type options: %w", err)
}
if err = l.typ(text, copts); err != nil {
err = fmt.Errorf("typing %q in %q: %w", text, l.selector, err)
return
if err := l.typ(text, copts); err != nil {
return fmt.Errorf("typing %q in %q: %w", text, l.selector, err)
}

applySlowMo(l.ctx)

return nil
}

func (l *Locator) typ(text string, opts *FrameTypeOptions) error {
Expand Down
12 changes: 6 additions & 6 deletions common/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,7 @@ func (p *Page) QueryAll(selector string) ([]*ElementHandle, error) {
}

// Reload will reload the current page.
func (p *Page) Reload(opts goja.Value) *Response { //nolint:funlen,cyclop
func (p *Page) Reload(opts goja.Value) (*Response, error) { //nolint:funlen,cyclop
p.logger.Debugf("Page:Reload", "sid:%v", p.sessionID())
_, span := TraceAPICall(p.ctx, p.targetID.String(), "page.reload")
defer span.End()
Expand All @@ -1044,7 +1044,7 @@ func (p *Page) Reload(opts goja.Value) *Response { //nolint:funlen,cyclop
p.timeoutSettings.navigationTimeout(),
)
if err := parsedOpts.Parse(p.ctx, opts); err != nil {
k6ext.Panic(p.ctx, "parsing reload options: %w", err)
return nil, fmt.Errorf("parsing reload options: %w", err)
}

timeoutCtx, timeoutCancelFn := context.WithTimeout(p.ctx, parsedOpts.Timeout)
Expand All @@ -1070,7 +1070,7 @@ func (p *Page) Reload(opts goja.Value) *Response { //nolint:funlen,cyclop

action := cdppage.Reload()
if err := action.Do(cdp.WithExecutor(p.ctx, p.session)); err != nil {
k6ext.Panic(p.ctx, "reloading page: %w", err)
return nil, fmt.Errorf("reloading page: %w", err)
}

wrapTimeoutError := func(err error) error {
Expand All @@ -1090,7 +1090,7 @@ func (p *Page) Reload(opts goja.Value) *Response { //nolint:funlen,cyclop
select {
case <-p.ctx.Done():
case <-timeoutCtx.Done():
k6ext.Panic(p.ctx, "%w", wrapTimeoutError(timeoutCtx.Err()))
return nil, wrapTimeoutError(timeoutCtx.Err())
case data := <-ch:
event = data.(*NavigationEvent)
}
Expand All @@ -1106,12 +1106,12 @@ func (p *Page) Reload(opts goja.Value) *Response { //nolint:funlen,cyclop
select {
case <-lifecycleEvtCh:
case <-timeoutCtx.Done():
k6ext.Panic(p.ctx, "%w", wrapTimeoutError(timeoutCtx.Err()))
return nil, wrapTimeoutError(timeoutCtx.Err())
}

applySlowMo(p.ctx)

return resp
return resp, nil
}

// Route is not implemented.
Expand Down
8 changes: 7 additions & 1 deletion tests/locator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,13 @@ func TestLocator(t *testing.T) {
},
},
{
"Type", func(l *common.Locator, tb *testBrowser) { l.Type("a", timeout(tb)) },
"Type", func(l *common.Locator, tb *testBrowser) {
err := l.Type("a", timeout(tb))
if err != nil {
// TODO: remove panic and update tests when all locator methods return error.
panic(err)
}
},
},
{
"TextContent", func(l *common.Locator, tb *testBrowser) { l.TextContent(timeout(tb)) },
Expand Down
Loading