Skip to content

Commit

Permalink
Update browser.userAgent to return cached data
Browse files Browse the repository at this point in the history
  • Loading branch information
inancgumus committed May 30, 2024
1 parent 6a60e26 commit ab0d640
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 11 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() //nolint:wrapcheck
return b.UserAgent(), nil
},
"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, error)
UserAgent() string
Version() string
}

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

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

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

// testBrowserVersion() tests the version already
// just look for "Headless" in UserAgent
ua, err := b.UserAgent()
require.NoError(t, err)
ua := b.UserAgent()
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 ab0d640

Please sign in to comment.