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

Commit

Permalink
Turn Browser.UserAgent to return err
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed May 30, 2024
1 parent 997f6fb commit 256a48c
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion browser/browser_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func mapBrowser(vu moduleVU) mapping { //nolint:funlen,cyclop
if err != nil {
return "", err
}
return b.UserAgent(), nil
return b.UserAgent() //nolint:wrapcheck
},
"version": func() (string, error) {
b, err := vu.browser()
Expand Down
2 changes: 1 addition & 1 deletion browser/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ type browserAPI interface {
NewContext(opts goja.Value) (*common.BrowserContext, error)
NewPage(opts goja.Value) (*common.Page, error)
On(string) (bool, error)
UserAgent() string
UserAgent() (string, error)
Version() string
}

Expand Down
8 changes: 4 additions & 4 deletions common/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,13 +628,13 @@ func (b *Browser) On(event string) (bool, error) {
}

// UserAgent returns the controlled browser's user agent string.
func (b *Browser) UserAgent() string {
func (b *Browser) UserAgent() (string, error) {
action := cdpbrowser.GetVersion()
_, _, _, ua, _, err := action.Do(cdp.WithExecutor(b.ctx, b.conn))
_, _, _, ua, _, err := action.Do(cdp.WithExecutor(b.ctx, b.conn)) //nolint:dogsled
if err != nil {
k6ext.Panic(b.ctx, "getting browser user agent: %w", err)
return "", fmt.Errorf("getting browser user agent: %w", err)
}
return ua
return ua, nil
}

// Version returns the controlled browser's version.
Expand Down
3 changes: 2 additions & 1 deletion tests/browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ func TestBrowserUserAgent(t *testing.T) {

// testBrowserVersion() tests the version already
// just look for "Headless" in UserAgent
ua := b.UserAgent()
ua, err := b.UserAgent()
require.NoError(t, err)
if prefix := "Mozilla/5.0"; !strings.HasPrefix(ua, prefix) {
t.Errorf("UserAgent should start with %q, but got: %q", prefix, ua)
}
Expand Down

0 comments on commit 256a48c

Please sign in to comment.