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

fix: allow UA emulation when overriding #462

Merged
merged 2 commits into from
Feb 22, 2022
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
8 changes: 4 additions & 4 deletions __tests__/core/gatherer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ describe('Gatherer', () => {
it('works with device emulation', async () => {
const driver = await Gatherer.setupDriver({
wsEndpoint,
playwrightOptions: { ...devices['Galaxy S9+'] },
playwrightOptions: { ...devices['iPhone 12 Pro Max'] },
});
expect(await driver.page.evaluate(() => navigator.userAgent)).toContain(
' Elastic/Synthetics'
);
const userAgent = await driver.page.evaluate(() => navigator.userAgent);
expect(userAgent).toContain('Elastic/Synthetics');
expect(userAgent).toContain('iPhone');
await Gatherer.dispose(driver);
await Gatherer.stop();
});
Expand Down
13 changes: 8 additions & 5 deletions src/core/gatherer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class Gatherer {
}
const context = await Gatherer.browser.newContext({
...playwrightOptions,
userAgent: await Gatherer.getUserAgent(),
userAgent: await Gatherer.getUserAgent(playwrightOptions?.userAgent),
});
await Gatherer.setNetworkConditions(context, networkConditions);

Expand All @@ -65,10 +65,13 @@ export class Gatherer {
return { browser: Gatherer.browser, context, page, client };
}

static async getUserAgent() {
const session = await Gatherer.browser.newBrowserCDPSession();
const { userAgent } = await session.send('Browser.getVersion');
return userAgent + ' Elastic/Synthetics';
static async getUserAgent(userAgent?: string) {
const syntheticsIdentifier = ' Elastic/Synthetics';
if (!userAgent) {
const session = await Gatherer.browser.newBrowserCDPSession();
({ userAgent } = await session.send('Browser.getVersion'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL

}
return userAgent + syntheticsIdentifier;
}

static async setNetworkConditions(
Expand Down