Skip to content

Commit

Permalink
fix: allow UA emulation when overriding (#462)
Browse files Browse the repository at this point in the history
* fix: allow UA emulation when overriding

* apply suggestions from code review

Co-authored-by: Dominique Clarke <doclarke71@gmail.com>

Co-authored-by: Dominique Clarke <doclarke71@gmail.com>
  • Loading branch information
vigneshshanmugam and dominiqueclarke committed Feb 22, 2022
1 parent ae9fc89 commit c1ab52f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
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'));
}
return userAgent + syntheticsIdentifier;
}

static async setNetworkConditions(
Expand Down

0 comments on commit c1ab52f

Please sign in to comment.