<h3 dir="auto">System info</h3>
<ul dir="auto">
<li>Playwright Version: [v1.33.0]</li>
<li>Operating System: macOS 13.2</li>
<li>Browser: [All, Chromium, Firefox, WebKit]</li>
<li>Other info:</li>
</ul>
<h3 dir="auto">Source code</h3>
<p dir="auto"><strong>Link to the GitHub repository with the repro</strong></p>
<p dir="auto"><strong>Test file (self-contained)</strong></p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="    await test.step(&quot;demo1&quot;, async () =&gt; {
      await page.setExtraHTTPHeaders({ token: &quot;&quot;, secret: &quot;&quot; });
      await page.route(/(https:\/\/(.*))/, (route) =&gt; {
        const headers = route.request().headers();
        headers[&quot;token&quot;] = process.env.token ?? &quot;&quot;;
        headers[&quot;secret&quot;] = process.env.secret ?? &quot;&quot;;
        route.continue({ headers });
      });
      await page.goto(&quot;https://www.google.com&quot;);
    });"><pre class="notranslate">    <span class="pl-k">await</span> <span class="pl-s1">test</span><span class="pl-kos">.</span><span class="pl-en">step</span><span class="pl-kos">(</span><span class="pl-s">"demo1"</span><span class="pl-kos">,</span> <span class="pl-k">async</span> <span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
      <span class="pl-k">await</span> <span class="pl-s1">page</span><span class="pl-kos">.</span><span class="pl-en">setExtraHTTPHeaders</span><span class="pl-kos">(</span><span class="pl-kos">{</span> <span class="pl-c1">token</span>: <span class="pl-s">""</span><span class="pl-kos">,</span> <span class="pl-c1">secret</span>: <span class="pl-s">""</span> <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
      <span class="pl-k">await</span> <span class="pl-s1">page</span><span class="pl-kos">.</span><span class="pl-en">route</span><span class="pl-kos">(</span><span class="pl-pds"><span class="pl-c1">/</span><span class="pl-kos">(</span>https:<span class="pl-cce">\/</span><span class="pl-cce">\/</span><span class="pl-kos">(</span>.<span class="pl-c1">*</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-c1">/</span></span><span class="pl-kos">,</span> <span class="pl-kos">(</span><span class="pl-s1">route</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
        <span class="pl-k">const</span> <span class="pl-s1">headers</span> <span class="pl-c1">=</span> <span class="pl-s1">route</span><span class="pl-kos">.</span><span class="pl-en">request</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">headers</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
        <span class="pl-s1">headers</span><span class="pl-kos">[</span><span class="pl-s">"token"</span><span class="pl-kos">]</span> <span class="pl-c1">=</span> <span class="pl-s1">process</span><span class="pl-kos">.</span><span class="pl-c1">env</span><span class="pl-kos">.</span><span class="pl-c1">token</span> <span class="pl-c1">??</span> <span class="pl-s">""</span><span class="pl-kos">;</span>
        <span class="pl-s1">headers</span><span class="pl-kos">[</span><span class="pl-s">"secret"</span><span class="pl-kos">]</span> <span class="pl-c1">=</span> <span class="pl-s1">process</span><span class="pl-kos">.</span><span class="pl-c1">env</span><span class="pl-kos">.</span><span class="pl-c1">secret</span> <span class="pl-c1">??</span> <span class="pl-s">""</span><span class="pl-kos">;</span>
        <span class="pl-s1">route</span><span class="pl-kos">.</span><span class="pl-en">continue</span><span class="pl-kos">(</span><span class="pl-kos">{</span> headers <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
      <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
      <span class="pl-k">await</span> <span class="pl-s1">page</span><span class="pl-kos">.</span><span class="pl-en">goto</span><span class="pl-kos">(</span><span class="pl-s">"https://www.google.com"</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto"><strong>Steps</strong></p>
<ul dir="auto">
<li>[Run the test]</li>
<li>[...]</li>
</ul>
<p dir="auto"><strong>Expected</strong><br>
It should not display the route.continue outside of test step<br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/17619539/235927296-47adc0a7-a73d-49fd-b04b-44362f15e142.png"><img src="https://user-images.githubusercontent.com/17619539/235927296-47adc0a7-a73d-49fd-b04b-44362f15e142.png" alt="image" style="max-width: 100%;"></a></p>
<p dir="auto"><strong>Actual</strong><br>
<a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/17619539/235927129-9dd5cb04-a7d9-4184-9d15-6d938a94edf9.png"><img width="435" alt="image" src="https://user-images.githubusercontent.com/17619539/235927129-9dd5cb04-a7d9-4184-9d15-6d938a94edf9.png" style="max-width: 100%;"></a></p>
<h3 dir="auto">System info</h3>
<ul dir="auto">
<li>Playwright Version: v1.35.1</li>
<li>Operating System: macOS 13.4</li>
<li>Browser: Chromium</li>
<li>Other info:</li>
</ul>
<h3 dir="auto">Source code</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I provided exact source code that allows reproducing the issue locally.</li>
</ul>




<p dir="auto"><strong>Config file</strong></p>
<div class="highlight highlight-source-js notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// playwright.config.ts
import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'], },
    },
});"><pre class="notranslate"><span class="pl-c">// playwright.config.ts</span>
<span class="pl-k">import</span> <span class="pl-kos">{</span> <span class="pl-s1">defineConfig</span><span class="pl-kos">,</span> <span class="pl-s1">devices</span> <span class="pl-kos">}</span> <span class="pl-k">from</span> <span class="pl-s">'@playwright/test'</span><span class="pl-kos">;</span>

<span class="pl-k">export</span> <span class="pl-k">default</span> <span class="pl-en">defineConfig</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
  <span class="pl-c1">projects</span>: <span class="pl-kos">[</span>
    <span class="pl-kos">{</span>
      <span class="pl-c1">name</span>: <span class="pl-s">'chromium'</span><span class="pl-kos">,</span>
      <span class="pl-c1">use</span>: <span class="pl-kos">{</span> ...<span class="pl-s1">devices</span><span class="pl-kos">[</span><span class="pl-s">'Desktop Chrome'</span><span class="pl-kos">]</span><span class="pl-kos">,</span> <span class="pl-kos">}</span><span class="pl-kos">,</span>
    <span class="pl-kos">}</span><span class="pl-kos">,</span><span class="pl-kos"></span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto"><strong>Test file (self-contained)</strong></p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="import { mkdtemp, rm } from &quot;fs/promises&quot;;
import { tmpdir } from &quot;os&quot;;
import path from &quot;path&quot;;
import { test as base, chromium, BrowserContext } from &quot;@playwright/test&quot;;

const test = base.extend&lt;{
  downloadsPath: string;
  context: BrowserContext;
}&gt;({
  downloadsPath: async ({}, use) =&gt; {
    const downloadsPath = await mkdtemp(path.join(tmpdir(), &quot;playwright-downloads-path-&quot;));
    await use(downloadsPath);
    await rm(downloadsPath, { recursive: true });
  },
  context: async ({ channel, downloadsPath }, use) =&gt; {
    const context = await chromium.launchPersistentContext(&quot;&quot;, {
      headless: false,
      args: [
        &quot;--headless=new&quot;,
      ],
      downloadsPath,
      channel,
    });
    await use(context);
    await context.close();
  }
});

test(&quot;click to download&quot;, async ({ context, downloadsPath }) =&gt; {
  const page = await context.newPage();
  await page.goto(&quot;https://www.example.com/&quot;);
  await page.setContent(`
    &lt;a href=&quot;https://github.com/microsoft/playwright/archive/refs/tags/v1.35.1.tar.gz&quot;&gt;open&lt;/a&gt;
  `);
  const cdpSession = await context.newCDPSession(page);
  await cdpSession.send(&quot;Browser.setDownloadBehavior&quot;, {
    behavior: &quot;default&quot;,
    downloadPath: downloadsPath,
    eventsEnabled: true,
  });
  await cdpSession.detach();

  const [download] = await Promise.all([
    page.waitForEvent(&quot;download&quot;),
    page.click(&quot;a&quot;),
  ]);
  test.expect(await download.path()).toContain(downloadsPath);
  await download.saveAs(&quot;tarball.tar.gz&quot;);
});"><pre class="notranslate"><span class="pl-k">import</span> <span class="pl-kos">{</span> <span class="pl-s1">mkdtemp</span><span class="pl-kos">,</span> <span class="pl-s1">rm</span> <span class="pl-kos">}</span> <span class="pl-k">from</span> <span class="pl-s">"fs/promises"</span><span class="pl-kos">;</span>
<span class="pl-k">import</span> <span class="pl-kos">{</span> <span class="pl-s1">tmpdir</span> <span class="pl-kos">}</span> <span class="pl-k">from</span> <span class="pl-s">"os"</span><span class="pl-kos">;</span>
<span class="pl-k">import</span> <span class="pl-s1">path</span> <span class="pl-k">from</span> <span class="pl-s">"path"</span><span class="pl-kos">;</span>
<span class="pl-k">import</span> <span class="pl-kos">{</span> <span class="pl-s1">test</span> <span class="pl-k">as</span> <span class="pl-s1">base</span><span class="pl-kos">,</span> <span class="pl-s1">chromium</span><span class="pl-kos">,</span> <span class="pl-smi">BrowserContext</span> <span class="pl-kos">}</span> <span class="pl-k">from</span> <span class="pl-s">"@playwright/test"</span><span class="pl-kos">;</span>

<span class="pl-k">const</span> <span class="pl-s1">test</span> <span class="pl-c1">=</span> <span class="pl-s1">base</span><span class="pl-kos">.</span><span class="pl-en">extend</span><span class="pl-kos">&lt;</span><span class="pl-kos">{</span>
  <span class="pl-c1">downloadsPath</span>: <span class="pl-smi">string</span><span class="pl-kos">;</span>
  <span class="pl-c1">context</span>: <span class="pl-smi">BrowserContext</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">&gt;</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
  <span class="pl-en">downloadsPath</span>: <span class="pl-k">async</span> <span class="pl-kos">(</span><span class="pl-kos">{</span><span class="pl-kos">}</span><span class="pl-kos">,</span> <span class="pl-s1">use</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-k">const</span> <span class="pl-s1">downloadsPath</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-en">mkdtemp</span><span class="pl-kos">(</span><span class="pl-s1">path</span><span class="pl-kos">.</span><span class="pl-en">join</span><span class="pl-kos">(</span><span class="pl-en">tmpdir</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">,</span> <span class="pl-s">"playwright-downloads-path-"</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-k">await</span> <span class="pl-en">use</span><span class="pl-kos">(</span><span class="pl-s1">downloadsPath</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-k">await</span> <span class="pl-en">rm</span><span class="pl-kos">(</span><span class="pl-s1">downloadsPath</span><span class="pl-kos">,</span> <span class="pl-kos">{</span> <span class="pl-c1">recursive</span>: <span class="pl-c1">true</span> <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-kos">}</span><span class="pl-kos">,</span>
  <span class="pl-en">context</span>: <span class="pl-k">async</span> <span class="pl-kos">(</span><span class="pl-kos">{</span> channel<span class="pl-kos">,</span> downloadsPath <span class="pl-kos">}</span><span class="pl-kos">,</span> <span class="pl-s1">use</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
    <span class="pl-k">const</span> <span class="pl-s1">context</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-s1">chromium</span><span class="pl-kos">.</span><span class="pl-en">launchPersistentContext</span><span class="pl-kos">(</span><span class="pl-s">""</span><span class="pl-kos">,</span> <span class="pl-kos">{</span>
      <span class="pl-c1">headless</span>: <span class="pl-c1">false</span><span class="pl-kos">,</span>
      <span class="pl-c1">args</span>: <span class="pl-kos">[</span>
        <span class="pl-s">"--headless=new"</span><span class="pl-kos">,</span>
      <span class="pl-kos">]</span><span class="pl-kos">,</span>
      downloadsPath<span class="pl-kos">,</span>
      channel<span class="pl-kos">,</span>
    <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-k">await</span> <span class="pl-en">use</span><span class="pl-kos">(</span><span class="pl-s1">context</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
    <span class="pl-k">await</span> <span class="pl-s1">context</span><span class="pl-kos">.</span><span class="pl-en">close</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-kos">}</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-en">test</span><span class="pl-kos">(</span><span class="pl-s">"click to download"</span><span class="pl-kos">,</span> <span class="pl-k">async</span> <span class="pl-kos">(</span><span class="pl-kos">{</span> context<span class="pl-kos">,</span> downloadsPath <span class="pl-kos">}</span><span class="pl-kos">)</span> <span class="pl-c1">=&gt;</span> <span class="pl-kos">{</span>
  <span class="pl-k">const</span> <span class="pl-s1">page</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-s1">context</span><span class="pl-kos">.</span><span class="pl-en">newPage</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-k">await</span> <span class="pl-s1">page</span><span class="pl-kos">.</span><span class="pl-en">goto</span><span class="pl-kos">(</span><span class="pl-s">"https://www.example.com/"</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-k">await</span> <span class="pl-s1">page</span><span class="pl-kos">.</span><span class="pl-en">setContent</span><span class="pl-kos">(</span><span class="pl-s">`</span>
<span class="pl-s">    &lt;a href="https://github.com/microsoft/playwright/archive/refs/tags/v1.35.1.tar.gz"&gt;open&lt;/a&gt;</span>
<span class="pl-s">  `</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-k">const</span> <span class="pl-s1">cdpSession</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-s1">context</span><span class="pl-kos">.</span><span class="pl-en">newCDPSession</span><span class="pl-kos">(</span><span class="pl-s1">page</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-k">await</span> <span class="pl-s1">cdpSession</span><span class="pl-kos">.</span><span class="pl-en">send</span><span class="pl-kos">(</span><span class="pl-s">"Browser.setDownloadBehavior"</span><span class="pl-kos">,</span> <span class="pl-kos">{</span>
    <span class="pl-c1">behavior</span>: <span class="pl-s">"default"</span><span class="pl-kos">,</span>
    <span class="pl-c1">downloadPath</span>: <span class="pl-s1">downloadsPath</span><span class="pl-kos">,</span>
    <span class="pl-c1">eventsEnabled</span>: <span class="pl-c1">true</span><span class="pl-kos">,</span>
  <span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-k">await</span> <span class="pl-s1">cdpSession</span><span class="pl-kos">.</span><span class="pl-en">detach</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

  <span class="pl-k">const</span> <span class="pl-kos">[</span><span class="pl-s1">download</span><span class="pl-kos">]</span> <span class="pl-c1">=</span> <span class="pl-k">await</span> <span class="pl-smi">Promise</span><span class="pl-kos">.</span><span class="pl-en">all</span><span class="pl-kos">(</span><span class="pl-kos">[</span>
    <span class="pl-s1">page</span><span class="pl-kos">.</span><span class="pl-en">waitForEvent</span><span class="pl-kos">(</span><span class="pl-s">"download"</span><span class="pl-kos">)</span><span class="pl-kos">,</span>
    <span class="pl-s1">page</span><span class="pl-kos">.</span><span class="pl-en">click</span><span class="pl-kos">(</span><span class="pl-s">"a"</span><span class="pl-kos">)</span><span class="pl-kos">,</span>
  <span class="pl-kos">]</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-s1">test</span><span class="pl-kos">.</span><span class="pl-en">expect</span><span class="pl-kos">(</span><span class="pl-k">await</span> <span class="pl-s1">download</span><span class="pl-kos">.</span><span class="pl-en">path</span><span class="pl-kos">(</span><span class="pl-kos">)</span><span class="pl-kos">)</span><span class="pl-kos">.</span><span class="pl-en">toContain</span><span class="pl-kos">(</span><span class="pl-s1">downloadsPath</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
  <span class="pl-k">await</span> <span class="pl-s1">download</span><span class="pl-kos">.</span><span class="pl-en">saveAs</span><span class="pl-kos">(</span><span class="pl-s">"tarball.tar.gz"</span><span class="pl-kos">)</span><span class="pl-kos">;</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto"><strong>Steps</strong></p>
<ul dir="auto">
<li>Run the test</li>
</ul>
<p dir="auto"><strong>Expected</strong></p>
<p dir="auto">The test to pass</p>
<p dir="auto"><strong>Actual</strong></p>
<p dir="auto"><code class="notranslate">saveAs</code> fails because the file is actually downloaded to the real user's Download folder.</p>
<p dir="auto"><strong>Notes</strong></p>
<ul dir="auto">
<li>I use <code class="notranslate">Browser.setDownloadBehavior</code> with <code class="notranslate">default</code> behavior to work around <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="1520444596" data-permission-text="Title is private" data-url="https://github.com/microsoft/playwright/issues/19885" data-hovercard-type="issue" data-hovercard-url="/microsoft/playwright/issues/19885/hovercard" href="https://github.com/microsoft/playwright/issues/19885">#19885</a></li>
</ul>