Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: page.route_web_socket doesn't intercept the ws connection #2686

Closed
nishirken opened this issue Dec 12, 2024 · 4 comments
Closed

[Bug]: page.route_web_socket doesn't intercept the ws connection #2686

nishirken opened this issue Dec 12, 2024 · 4 comments

Comments

@nishirken
Copy link

nishirken commented Dec 12, 2024

Version

1.49.1

Steps to reproduce

The test code uses a custom chromium path. TEST_BROWSER_PATH.

  1. Clone https://github.com/nishirken/websocket-test
  2. Install uv pip install uv
  3. Install deps uv sync && npm i
  4. Launch the client python -m http.server 8000
  5. Launch the server uv run server.py
  6. Launch the python test uv run test_websocket.py
  7. Launch the ts test npx ts-node test_websocket.ts

Expected behavior

The python and ts versions should behave the same.
The python version prints a message when the WebSocket route is established and sends a fake message to the client. (The ts version does it as expected)

Actual behavior

No logs and no messages in the python version.

Additional context

No response

Environment

- Operating System: Linux 6.11 NixOS 24.05 (Uakari) 24.05 (Uakari)
- CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1185G7 @ 3.00GHz
- Browser: Chromium
- Python Version: [3.11.10]
- Other info:
@nishirken nishirken changed the title [Bug]: Python's route_web_socket doesn't intercept the ws connection [Bug]: page.route_web_socket doesn't intercept the ws connection Dec 12, 2024
@mxschmitt
Copy link
Member

Thank you for the great bug report! Looks like this is about the URL matching - I created a PR with a fix in #2687. As a workaround could you try this?

await page.route_web_socket("ws://localhost:8080/", route_handler)

@nishirken
Copy link
Author

nishirken commented Dec 13, 2024

I've installed the version from github. It works with a string URL.
But a regex url doesn't work.
I've updated the example with a get param in the url.

await page.route_web_socket(r"^ws://.*", route_handler)

doesn't intercept the connection URL

"ws://localhost:8080?a=1"

@mxschmitt
Copy link
Member

r"^ws://.*" is just a str in Python - if you want to use a regex, you need to use:

    await page.route_web_socket(re.compile(r"^ws://.*"), route_handler)

@nishirken
Copy link
Author

It does work. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants