From 8918d0e4270c83a6878200ed5b1971ca8ea09b68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 12 Nov 2024 14:46:56 -0500 Subject: [PATCH 1/4] Remove Headless from user agent in headless mode --- common/browser.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/common/browser.go b/common/browser.go index d6e2d309a..902fc5d45 100644 --- a/common/browser.go +++ b/common/browser.go @@ -691,6 +691,19 @@ func (b *Browser) fetchVersion() (browserVersion, error) { return browserVersion{}, fmt.Errorf("getting browser version information: %w", err) } + // Adjust the user agent to remove the headless part. + // + // Including Headless might cause issues with some websites that treat headless + // browsers differently. Later on, [BrowserContext] will set the user agent to + // this user agent if not set by the user. This will force [FrameSession] to + // set the user agent to the browser's user agent. + // + // Doing this here provides a consistent user agent across all browser contexts. + // Also, it makes it consistent to query the user agent from the browser. + if b.browserOpts.Headless { + bv.userAgent = strings.ReplaceAll(bv.userAgent, "Headless", "") + } + return bv, nil } From f4943431a3be0c5bc3cebe52d606a50f79476f85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 12 Nov 2024 14:49:02 -0500 Subject: [PATCH 2/4] Force BrowserContext to use Browser.UserAgent --- common/browser_context.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/common/browser_context.go b/common/browser_context.go index 70de498e4..a1e2bccf8 100644 --- a/common/browser_context.go +++ b/common/browser_context.go @@ -118,6 +118,11 @@ func NewBrowserContext( if opts == nil { opts = DefaultBrowserContextOptions() } + // Always use the [Browser]'s user agent if it's not set by the user. + // Setting this forces [FrameSession] to set Chromium's user agent. + if strings.TrimSpace(opts.UserAgent) == "" { + opts.UserAgent = browser.UserAgent() + } b := BrowserContext{ BaseEventEmitter: NewBaseEventEmitter(ctx), From 0c4fe3c3d35d97a37d9f4abab4b62b8c988b0da6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 12 Nov 2024 15:00:38 -0500 Subject: [PATCH 3/4] Add useragent javascript example --- examples/useragent.js | 49 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 examples/useragent.js diff --git a/examples/useragent.js b/examples/useragent.js new file mode 100644 index 000000000..063398781 --- /dev/null +++ b/examples/useragent.js @@ -0,0 +1,49 @@ +import { browser } from 'k6/x/browser/async'; +import { check } from 'https://jslib.k6.io/k6-utils/1.5.0/index.js'; + +export const options = { + scenarios: { + ui: { + executor: 'shared-iterations', + options: { + browser: { + type: 'chromium', + }, + }, + }, + }, + thresholds: { + checks: ["rate==1.0"] + } +} + +export default async function() { + let context = await browser.newContext({ + userAgent: 'k6 test user agent', + }) + let page = await context.newPage(); + await check(page, { + 'user agent is set': async p => { + const userAgent = await p.evaluate(() => navigator.userAgent); + return userAgent.includes('k6 test user agent'); + } + }); + await page.close(); + await context.close(); + + context = await browser.newContext(); + check(context.browser(), { + 'user agent does not contain headless': b => { + return b.userAgent().includes('Headless') === false; + } + }); + + page = await context.newPage(); + await check(page, { + 'chromium user agent does not contain headless': async p => { + const userAgent = await p.evaluate(() => navigator.userAgent); + return userAgent.includes('Headless') === false; + } + }); + await page.close(); +} From b1e5b7c920202ae2552e3174c48ed49ab858612f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0nan=C3=A7=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 13 Nov 2024 10:15:16 -0500 Subject: [PATCH 4/4] Fix TestBrowserUserAgent for Headless removal --- tests/browser_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/browser_test.go b/tests/browser_test.go index dbec510c8..b80427131 100644 --- a/tests/browser_test.go +++ b/tests/browser_test.go @@ -220,13 +220,12 @@ func TestBrowserUserAgent(t *testing.T) { b := newTestBrowser(t) - // testBrowserVersion() tests the version already - // just look for "Headless" in UserAgent ua := b.UserAgent() if prefix := "Mozilla/5.0"; !strings.HasPrefix(ua, prefix) { t.Errorf("UserAgent should start with %q, but got: %q", prefix, ua) } - assert.Contains(t, ua, "Headless") + // We default to removing the "Headless" part of the user agent string. + assert.NotContains(t, ua, "Headless") } func TestBrowserCrashErr(t *testing.T) {