Skip to content

[Bug]: page.routeWebSocket() called after page.goto() misses sockets the loaded page creates later (works only after a reload) #43105

Closed

Description

Version

1.63.0

Environment

  • OS: macOS 27.0.1 (arm64)
  • Node.js: 26.10.0
  • Browsers: Chromium 153.0.8010.12 and WebKit 26.6 (bundled); Firefox not tried

Steps to reproduce

In an empty folder: npm init -y, npm i -D @playwright/[email protected], npx playwright install chromium webkit. Then add these two files.

playwright.config.ts:

import { defineConfig, devices } from '@playwright/test';

export default defineConfig({
  reporter: 'list',
  projects: [
    { name: 'chromium', use: { ...devices['Desktop Chrome'] } },
    { name: 'webkit', use: { ...devices['Desktop Safari'] } },
  ],
});

route-after-goto.spec.ts (no server is needed: the page is served by page.route()):

import { test, expect } from '@playwright/test';

// The page opens its WebSocket only when the button is clicked. There is no real server.
const html = `<button>Connect</button><p id="msg">idle</p>
<script>
  document.querySelector('button').onclick = () => {
    const ws = new WebSocket('wss://example.test/live');
    ws.onmessage = (e) => { document.getElementById('msg').textContent = e.data; };
    ws.onclose = () => { document.getElementById('msg').textContent = 'closed'; };
  };
</script>`;

test.beforeEach(async ({ page }) => {
  await page.route('https://example.test/', (route) => route.fulfill({ contentType: 'text/html', body: html }));
});

test('control: routeWebSocket before goto', async ({ page }) => {
  await page.routeWebSocket('wss://example.test/live', (ws) => ws.send('mocked'));
  await page.goto('https://example.test/');
  await page.getByRole('button').click();
  await expect(page.locator('#msg')).toHaveText('mocked'); // passes
});

test('routeWebSocket after goto, socket created after the call', async ({ page }) => {
  await page.goto('https://example.test/');
  await page.routeWebSocket('wss://example.test/live', (ws) => ws.send('mocked'));
  await page.getByRole('button').click(); // the socket is created now, after routeWebSocket
  await expect(page.locator('#msg')).toHaveText('mocked'); // fails: 'closed' (not routed, went to the network)
});

test('routeWebSocket after goto, then reload', async ({ page }) => {
  await page.goto('https://example.test/');
  await page.routeWebSocket('wss://example.test/live', (ws) => ws.send('mocked'));
  await page.reload();
  await page.getByRole('button').click();
  await expect(page.locator('#msg')).toHaveText('mocked'); // passes
});

Run npx playwright test --workers=1.

Expected behavior

The second test passes: the socket is created by the click, after routeWebSocket() was called, so it is routed.

page.routeWebSocket(): "Note that only WebSockets
created after this method was called will be routed. It is recommended to call this method before navigating the page."

Actual behavior

The socket goes to the network (and closes, since example.test doesn't exist). After a reload the same route works.

Running 6 tests using 1 worker

  ✓  1 [chromium] › route-after-goto.spec.ts:17:5 › control: routeWebSocket before goto (112ms)
  ✘  2 [chromium] › route-after-goto.spec.ts:24:5 › routeWebSocket after goto, socket created after the call (5.1s)
  ✓  3 [chromium] › route-after-goto.spec.ts:31:5 › routeWebSocket after goto, then reload (121ms)
  ✓  4 [webkit] › route-after-goto.spec.ts:17:5 › control: routeWebSocket before goto (1.0s)
  ✘  5 [webkit] › route-after-goto.spec.ts:24:5 › routeWebSocket after goto, socket created after the call (5.2s)
  ✓  6 [webkit] › route-after-goto.spec.ts:31:5 › routeWebSocket after goto, then reload (459ms)


  1) [chromium] › route-after-goto.spec.ts:24:5 › routeWebSocket after goto, socket created after the call 

    Error: expect(locator).toHaveText(expected) failed

    Locator:  locator('#msg')
    Expected: "mocked"
    Received: "closed"
    Timeout:  5000ms

    Call log:
      - Expect "toHaveText" locator('#msg') with timeout 5000ms
      - waiting for locator('#msg')
        - locator resolved to <p id="msg">idle</p>
        - unexpected value "idle"
        13 × locator resolved to <p id="msg">closed</p>
           - unexpected value "closed"


      26 |   await page.routeWebSocket('wss://example.test/live', (ws) => ws.send('mocked'));
      27 |   await page.getByRole('button').click(); // the socket is created now, after routeWebSocket
    > 28 |   await expect(page.locator('#msg')).toHaveText('mocked'); // fails: 'closed' (not routed, went to the network)
         |                                      ^
      29 | });
      30 |
      31 | test('routeWebSocket after goto, then reload', async ({ page }) => {
        at /path/to/repro/route-after-goto.spec.ts:28:38

    Error Context: test-results/route-after-goto-routeWebS-a9263-cket-created-after-the-call-chromium/error-context.md

  2) [webkit] › route-after-goto.spec.ts:24:5 › routeWebSocket after goto, socket created after the call 

    Error: expect(locator).toHaveText(expected) failed

    Locator:  locator('#msg')
    Expected: "mocked"
    Received: "closed"
    Timeout:  5000ms

    Call log:
      - Expect "toHaveText" locator('#msg') with timeout 5000ms
      - waiting for locator('#msg')
        2 × locator resolved to <p id="msg">idle</p>
          - unexpected value "idle"
        12 × locator resolved to <p id="msg">closed</p>
           - unexpected value "closed"


      26 |   await page.routeWebSocket('wss://example.test/live', (ws) => ws.send('mocked'));
      27 |   await page.getByRole('button').click(); // the socket is created now, after routeWebSocket
    > 28 |   await expect(page.locator('#msg')).toHaveText('mocked'); // fails: 'closed' (not routed, went to the network)
         |                                      ^
      29 | });
      30 |
      31 | test('routeWebSocket after goto, then reload', async ({ page }) => {
        at /path/to/repro/route-after-goto.spec.ts:28:38

    Error Context: test-results/route-after-goto-routeWebS-a9263-cket-created-after-the-call-webkit/error-context.md

  2 failed
    [chromium] › route-after-goto.spec.ts:24:5 › routeWebSocket after goto, socket created after the call 
    [webkit] › route-after-goto.spec.ts:24:5 › routeWebSocket after goto, socket created after the call 
  4 passed (14.7s)

Additional context

Notes: the first routeWebSocket() on a page installs its in-page WebSocket mock with addInitScript() only, so
the document that is already loaded keeps the native WebSocket until the next navigation. Consistent with that, the
late route works when any other routeWebSocket() (even for an unrelated URL) was registered before goto().
Workaround: call routeWebSocket() before navigating, or reload after it. Either the mock could also be injected into
the current document, or the docs could say "sockets created by documents loaded after this call".

Activity

  1. github-actions commented on Oct 5, 2026

    @github-actions
    Contributor

    Hi, I'm the Playwright bot and I took a first look.

    Reproduced on 1.63.0 and the latest next build (1.64.0-alpha-2026-10-05) in Chromium, Firefox, and WebKit. A WebSocket created after page.routeWebSocket() is not routed when the first route is registered in an already-loaded document. This reproduces in both headed and headless mode, so it is not browser-specific. It looks like a real bug.

    The first WebSocket route installs the mock as an init script but does not inject it into the current document. This also explains why reloading works and why registering any WebSocket route before navigation makes a later route work.

    Minimal repro
    import { test, expect } from '@playwright/test';
    
    test('routes a socket created after routeWebSocket', {
      annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43105' },
    }, async ({ page }) => {
      await page.route('https://example.test/', route => route.fulfill({
        contentType: 'text/html',
        body: `<button>Connect</button><p>idle</p>
          <script>
            document.querySelector('button').onclick = () => {
              const ws = new WebSocket('wss://example.test/live');
              ws.onmessage = event => document.querySelector('p').textContent = event.data;
              ws.onclose = () => document.querySelector('p').textContent = 'closed';
            };
          </script>`,
      }));
    
      await page.goto('https://example.test/');
      await page.routeWebSocket('wss://example.test/live', ws => ws.send('mocked'));
      await page.getByRole('button').click();
      await expect(page.locator('p')).toHaveText('mocked'); // Receives "closed".
    });
    What I ran
    • Linux, Node.js 24.21.0
    • @playwright/test 1.63.0: Chromium 153.0.8010.12, Firefox 155.0, WebKit 26.6
    • @playwright/test 1.64.0-alpha-2026-10-05: Chromium 156.0.8078.4, Firefox 156.0, WebKit 26.6
    • Headed and headless: the late route fails in all three browsers
    • Controls on both Playwright versions: routing before navigation passes; routing after navigation followed by reload passes; registering an unrelated route before navigation and the matching route afterward passes
    • Current source checkout: a18dade

    Triaged on Linux by the Playwright bot - agent run

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions