Skip to content

[Bug]: WebSocket route does not handle full URLs in Playwright #33085

@richardforjoejnr

Description

@richardforjoejnr

Version

1.48.0

Steps to reproduce

Pre-req: Ensure you have playwright repo already setup

  1. Use page.routeWebSocket() to intercept a WebSocket URL
  2. Add the below test and run it
test('intercepted websocket', async ({ page }) => {
        await page.routeWebSocket('wss://echo.websocket.org/', ws => {
            ws.onMessage(message => {
                if (message === 'Hello chatbot') {
                    ws.send('Intercepted Hello chatbot');  // Intercept and modify the response.
                } else {
                    ws.send(message);  // Echo the received message otherwise.
                }
            });
        });

        // Navigate to the WebSocket page
        await page.goto('https://echo.websocket.org/.ws');

        // Interact with the page's UI to send a WebSocket message
        await page.getByRole('button', { name: 'Pause Messaging' }).click();  // Clicking a button.
        await page.locator('#content').fill('Hello chatbot');  // Filling input with a message.
        await page.getByRole('button', { name: 'Send Message' }).click();  // Sending the message.

        // Assert that the console has the expected text
        await expect(page.locator('#console div')).toHaveText(['attempting to connect']);
    });

Expected behavior

I was expecting to see "Intercepted Hello chatbot" shown

I was also expecting the following to work but it didn't

await page.routeWebSocket('/echo.websocket.org/'...

Actual behavior

Observe that nothing happened. Looks like no intercept occured. This was posted in discord and the following was spotted

mxschmitt —
do you mind filing a bug on GitHub?
Looks like its parsed as glob and the wss:// confuses the matching logic.
to make it work you can do:
await page.routeWebSocket(/echo.websocket.org/, ws => {

This worked after making the above change

Additional context

No response

Environment

System:
OS: macOS 14.6.1
CPU: (12) arm64 Apple M3 Pro
Memory: 56.78 MB / 18.00 GB
Binaries:
Node: 20.14.0 - ~/.nvm/versions/node/v20.14.0/bin/node
npm: 10.7.0 - ~/.nvm/versions/node/v20.14.0/bin/npm
IDEs:
VSCode: 1.93.1 - /usr/local/bin/code
Languages:
Bash: 3.2.57 - /bin/bash
npmPackages:
@playwright/test: ^1.48.0 => 1.48.0

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions