Repository: elastic/synthetics
Pull request #462 (2022-02-22 21:18:40)

fix: allow UA emulation when overriding
+ When users are overriding the useragent with  `--playwright-options` the UA was being overwritten in the agent code. This PR fixes that by using the UA passed to the agent. 


apmmachine: 
## :green_heart: Build Succeeded


<!-- BUILD BADGES-->
> _the below badges are clickable and redirect to their specific view in the CI or DOCS_
[![Pipeline View](https://img.shields.io/badge/pipeline-pipeline%20-green)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-462/detail/PR-462/1//pipeline) [![Test View](https://img.shields.io/badge/test-test-green)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-462/detail/PR-462/1//tests) [![Changes](https://img.shields.io/badge/changes-changes-green)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-462/detail/PR-462/1//changes) [![Artifacts](https://img.shields.io/badge/artifacts-artifacts-yellow)](https://apm-ci.elastic.co/blue/organizations/jenkins/apm-agent-rum%2Felastic-synthetics%2FPR-462/detail/PR-462/1//artifacts) [![preview](https://img.shields.io/badge/docs-preview-yellowgreen)](http://synthetics_462.docs-preview.app.elstc.co/diff)  [![preview](https://img.shields.io/badge/elastic-observability-blue)](https://ci-stats.elastic.co/app/apm/services/apm-ci/transactions/view?rangeFrom=2022-02-22T21:08:56.159Z&rangeTo=2022-02-22T21:28:56.159Z&transactionName=apm-agent-rum/elastic-synthetics/PR-{number}&transactionType=job&latencyAggregationType=avg&traceId=6cfafdb283f4147dfcb15b741046bd75&transactionId=08cf7b085070f64a)

<!-- BUILD SUMMARY-->
<details><summary>Expand to view the summary</summary>
<p>

#### Build stats




* Start Time: 2022-02-22T21:18:56.159+0000


* Duration: 22 min 0 sec




#### Test stats :test_tube:

| Test         | Results                         |
| ------------ | :-----------------------------: |
| Failed       | 0  |
| Passed       | 147  |
| Skipped      | 3 |
| Total        | 150   |


</p>
</details>

<!-- TEST RESULTS IF ANY-->


<!-- STEPS ERRORS IF ANY -->












## :robot: GitHub comments

To re-run your PR in the CI, just comment with:



- `/test` : Re-trigger the build.




<!--COMMENT_GENERATED_WITH_ID_comment.id-->

Patch:
diff --git a/src/core/gatherer.ts b/src/core/gatherer.ts
index 86f369b..fbdeda1 100644
--- a/src/core/gatherer.ts
+++ b/src/core/gatherer.ts
@@ -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);
 
@@ -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(


Test patch:
diff --git a/__tests__/core/gatherer.test.ts b/__tests__/core/gatherer.test.ts
index 3b8740a..cbb638f 100644
--- a/__tests__/core/gatherer.test.ts
+++ b/__tests__/core/gatherer.test.ts
@@ -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();
     });