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

Remove Headless from user agent #1536

Merged
merged 4 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 13 additions & 0 deletions common/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 5 additions & 0 deletions common/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
ankur22 marked this conversation as resolved.
Show resolved Hide resolved
}

b := BrowserContext{
BaseEventEmitter: NewBaseEventEmitter(ctx),
Expand Down
49 changes: 49 additions & 0 deletions examples/useragent.js
Original file line number Diff line number Diff line change
@@ -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();
}
Loading