Repository: elastic/synthetics
Pull request #352 (2021-07-21 23:12:49)

fix: capture correct url for navigations
+ When there are iframes and new windows that exists as part of the page lifecycle, we capture the wrong page URL as we are using the page object from previous session. 
+ part of #253 

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-352/detail/PR-352/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-352/detail/PR-352/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-352/detail/PR-352/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-352/detail/PR-352/1//artifacts) [![preview](https://img.shields.io/badge/docs-preview-yellowgreen)](http://synthetics_352.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=2021-07-21T23:03:04.909Z&rangeTo=2021-07-21T23:23:04.909Z&transactionName=apm-agent-rum/elastic-synthetics/PR-{number}&transactionType=unknown&latencyAggregationType=avg&traceId=c8f7730ebeb7a2cc80be1fec0fd9726a&transactionId=d410df05d7fe4f37)

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

#### Build stats




* Start Time: 2021-07-21T23:13:04.909+0000


* Duration: 14 min 37 sec


* Commit: ff02ee0cfd881c1bfb7fc13f5506a345950245b5



#### Test stats :test_tube:

| Test         | Results                         |
| ------------ | :-----------------------------: |
| Failed       | 0  |
| Passed       | 121  |
| Skipped      | 0 |
| Total        | 121   |


#### Trends :test_tube:


![Image of Build Times](https://apm-ci.elastic.co/job/apm-agent-rum/job/elastic-synthetics/job/PR-352/buildTimeGraph/png)


![Image of Tests](https://apm-ci.elastic.co/job/apm-agent-rum/job/elastic-synthetics/job/PR-352/1/testReport/history/countGraph/png?start=0&amp;end=25)


</p>
</details>

<!-- TEST RESULTS IF ANY-->


<!-- STEPS ERRORS IF ANY -->












<!--COMMENT_GENERATED_WITH_ID_comment.id-->
andrewvc: LGTM

Patch:
diff --git a/src/core/runner.ts b/src/core/runner.ts
index fc39159..6b6f8b7 100644
--- a/src/core/runner.ts
+++ b/src/core/runner.ts
@@ -230,9 +230,9 @@ export default class Runner extends EventEmitter {
       if (!data.url && req.isNavigationRequest()) {
         data.url = req.url();
       }
-      driver.page.off('request', captureUrl);
+      driver.context.off('request', captureUrl);
     };
-    driver.page.on('request', captureUrl);
+    driver.context.on('request', captureUrl);
     try {
       pluginManager.onStep(step);
       await step.callback();


Test patch:
diff --git a/__tests__/core/runner.test.ts b/__tests__/core/runner.test.ts
index e37cb18..5973895 100644
--- a/__tests__/core/runner.test.ts
+++ b/__tests__/core/runner.test.ts
@@ -243,6 +243,38 @@ describe('runner', () => {
     ]);
   });
 
+  it('run steps - new window navigation', async () => {
+    const j1 = journey('j1', async ({ page, context }) => {
+      step('visit test page', async () => {
+        await page.goto(server.TEST_PAGE);
+        await page.setContent(
+          '<a target=_blank rel=noopener href="/popup.html">popup</a>'
+        );
+      });
+      step('click popup', async () => {
+        const [page1] = await Promise.all([
+          context.waitForEvent('page'),
+          page.click('a'),
+        ]);
+        await page1.waitForLoadState();
+      });
+    });
+    const context = await Runner.createContext({});
+    await runner.registerJourney(j1, context);
+    const result = await runner.runSteps(j1, context, {});
+    await Gatherer.stop();
+    expect(result).toEqual([
+      {
+        status: 'succeeded',
+        url: server.TEST_PAGE,
+      },
+      {
+        status: 'succeeded',
+        url: server.PREFIX + '/popup.html',
+      },
+    ]);
+  });
+
   it('run steps - accumulate results', async () => {
     const error = new Error('broken step 2');
     const j1 = journey('j1', async ({ page }) => {