Repository navigation
Description
System info
- Playwright Version: [v1.35, 1.40]
- Operating System: [Ubuntu 22.04]
- Browser: [Chromium]
- Other info: Python 3.11.2
Scenario: I am testing an application that is to be deployed behind an AWS Application Load Balancer. The ABLB is configured to inject additional headers into all HTTP traffic that it services. I am attempting to use playwright to mimic that behavior by setting those additional headers on the Playwright. This works for regular HTTP requests, but does not inject the additional HTTP headers when the application makes a WebSocket connection to the server.
Source code
I've modified the playwright tests to demonstrate the lack of headers being sent:
$ git diff tests/*
diff --git a/tests/async/test_websocket.py b/tests/async/test_websocket.py
index eb90f95..f2f670b 100644
--- a/tests/async/test_websocket.py
+++ b/tests/async/test_websocket.py
@@ -24,6 +24,7 @@ from tests.server import Server
async def test_should_work(page: Page, ws_server: WebSocketServerServer) -> None:
+ await page.set_extra_http_headers({'HeHe-Sock': 'HaHa'})
value = await page.evaluate(
"""port => {
let cb;
diff --git a/tests/server.py b/tests/server.py
index 06e3446..c792d2a 100644
--- a/tests/server.py
+++ b/tests/server.py
@@ -273,6 +273,7 @@ class WebSocketProtocol(WebSocketServerProtocol):
pass
def onOpen(self) -> None:
+ print(self.http_request_data)
self.sendMessage(b"incoming")
def onMessage(self, payload: bytes, isBinary: bool) -> None:
Produces the following output:
$ python -m pytest tests/async/test_websocket.py -k work\[chromium
=========================================== test session starts =============================================================
platform linux -- Python 3.11.2, pytest-7.4.2, pluggy-1.0.0 -- /some/python3.12.2/directory/bin/python
cachedir: .pytest_cache
rootdir: /some/path/playwright-python
configfile: pyproject.toml
plugins: repeat-0.9.1, asyncio-0.21.1, flaky-3.7.0, xdist-3.3.1, rerunfailures-12.0, timeout-2.1.0, cov-4.1.0
asyncio: mode=Mode.AUTO
collected 18 items / 17 deselected / 1 selected
tests/async/test_websocket.py::test_should_work[chromium] b'GET /ws HTTP/1.1\r\nHost: localhost:50479\r\nConnection: Upgrade\r\nPragma: no-cache\r\nCache-Control: no-cache\r\nUser-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/120.0.6099.28 Safari/537.36\r\nUpgrade: websocket\r\nOrigin: null\r\nSec-WebSocket-Version: 13\r\nAccept-Encoding: gzip, deflate, br\r\nSec-WebSocket-Key: U77GHD/H1pmVcmytWUjsXg==\r\nSec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n\r\n'
PASSED
=========================================== 1 passed, 17 deselected, 1 warning in 1.02s =====================================
Expected
I expect the HTTP 101 requests ( made to upgrade a connection to a websocket ) to include the headers added to the page.
I have tried setting additional headers with the following APIs:
https://playwright.dev/python/docs/api/class-browser#browser-new-context
https://playwright.dev/python/docs/api/class-browsercontext#browser-context-set-extra-http-headers
https://playwright.dev/python/docs/api/class-page#page-set-extra-http-headers
These API docs imply that all connections will have these additional headers added to them.
Actual
In all my test cases, the extra headers are present on non-websocket requests.
The HTTP 101 requests made to upgrade a connection do not have the additional headers available on them.
Activity
mxschmitt commentedon Jan 11, 2024
I can repro with WebKit and Chromium. Passes in Firefox:
pavelfeldman commentedon Apr 22, 2024
pavelfeldman commentedon Aug 16, 2024
aletzal commentedon Aug 21, 2024
Hi!
It's important bug, blocks writing tests for projects with ws, any ideas how to fix it?
vEpiphyte commentedon Aug 21, 2024
Alexey (@aletzal) I've refiled this per Pavel Feldman (@pavelfeldman) 's note above as #32247
jbuettnerbild commentedon Dec 23, 2024
We find the problem described very annoying. We would be very happy if it were given a higher priority.
geminigeek commentedon Jan 28, 2025
this is important
adequatica commentedon Mar 20, 2025
It reproduces not only on playwright-python but a normal playwright, too.
In our project
extraHTTPHeadersare used for authorization, and this bug blocks the automation on an app with WSs.UPDATE: workaround: switch to Firefox (instead of Chromium) for cases with WSs.
Markz878 commentedon Apr 11, 2025
Same problem in Playwright for .NET, can't write tests that read ws connection authentication info from headers.
MurzNN commentedon Jul 10, 2026
This bug is still actual! And seems no workarounds for Chrome are possible?
Tried to intercept the WebSocket call manually like this:
But it doesn't work, the
page.route()intercepts only HTTP calls, not WS.And after a year of silence here, we collected 22 upvotes to this issue and no workarounds... And Chrome is still dominating Firefox, so switching to Firefox is not a good turn...
So, maybe someone still invented a working workaround for this issue?
test(websocket): cover `setExtraHTTPHeaders` and `locale` on the hand…