Repository navigation
Description
Hello:
I need some code sample on how to detect WebSocket (wss) requests by Playwright.
Basically, I can visit this URL with Chrome: https://www.websocket.org/echo.html
And click on “Connect” button to connect with the WebSocket server: wss://echo.websocket.org
Then type something in the message box, or using the default text in the message box “Rock it with HTML5 WebSocket”, then click on “Send” button, then I can see the messages on the “log” textbox, like this:
CONNECTED
SENT: Rock it with HTML5 WebSocket
RECEIVED: Rock it with HTML5 WebSocket
Then open Developer Tools from Chrome, on “Network” tab, I can see there is only one WS (WebSocket) request with the following headers:
Request URL:
wss://echo.websocket.org/?encoding=text
1. Request Method:
GET
2. Status Code:
101 Web Socket Protocol Handshake
However, I want to use Playwright in Node JS to simulate the above procedure, the following is my script and result (nothing shown up), and information about Node JS and NPM packages.
C:\playwright>type wss_echo.js
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch()
const context = await browser.newContext()
const page = await context.newPage()
await page.goto('https://www.websocket.org/echo.html')
await page.setViewportSize({ width: 1920, height: 937 })
await page.waitForSelector('.container #connect')
await page.click('.container #connect')
await page.waitForSelector('.container #sendMessage')
await page.click('.container #sendMessage')
await page.waitForSelector('.container #sendMessage')
await page.click('.container #sendMessage')
await page.waitForSelector('.container #sendMessage')
await page.click('.container #sendMessage')
await page.waitForSelector('.container #send')
await page.click('.container #send')
page.on('websocket', ws => {
console.log(WebSocket opened: ${ws.url()}>);
ws.on('framesent', event => console.log(event.payload));
ws.on('framereceived', event => console.log(event.payload));
ws.on('close', () => console.log('WebSocket closed'));
})
await browser.close()
})();
C:\playwright>node wss_echo.js
C:\playwright>
C:\playwright>npm list
[email protected] C:\playwright
+-- [email protected]
`-- [email protected]
C:\playwright>node --version
v16.4.0
C:\playwright>
In short, find WebSocket in Chrome with help of DevTools works, but using Playwright (Node JS), I don’t know how to sniff the WebSocket requests, by the way, I also want to get some useful information in the WSS request header, like: Sec-WebSocket-Key, for example: its value is: pqs8t5+Rpq+VJS5hegaLfg==
How I can get all those information by using Playwright? (My OS: Windows 10)
Thanks,
Activity
yury-s commentedon Jul 6, 2021
You are adding
websocketevent listener after all actions have been performed so the listener is never invoked. Just movepage.on('websocket', ws => {...block above firstpage.gotocall.zydjohnHotmail commentedon Jul 7, 2021
Hello:
Thanks for your reply, I changed my code, now it is working.
However, I need further information about the HTTP request headers. As I can use Chrome developer tools to convert the WebSocket request to Node.JS fetch:
fetch("wss://echo.websocket.org/?encoding=text", {
"headers": {
"accept-language": "en-US,en;q=0.9,fr;q=0.8,nl;q=0.7,zh-CN;q=0.6,zh;q=0.5,zh-TW;q=0.4",
"cache-control": "no-cache",
"pragma": "no-cache",
"sec-websocket-extensions": "permessage-deflate; client_max_window_bits",
"sec-websocket-key": "PVg+yYO5Q3EVgtPLLV2QXQ==",
"sec-websocket-version": "13"
},
"body": null,
"method": "GET",
"mode": "cors"
});
Let me know how I can use Playwright to get all the headers, actually, there is only one header I need to get is: "sec-websocket-key": "PVg+yYO5Q3EVgtPLLV2QXQ=="
If necessary, I can ask a new question!
Thanks,
mxschmitt commentedon Jul 7, 2021
We could probably add websocket.requestHeaders() Yury Semikhatsky (@yury-s) wdyt?
zydjohnHotmail commentedon Jul 7, 2021
Hello:
As I can't find any example code for this.
Could you please share your code for this?
Thanks,
yury-s commentedon Jul 7, 2021
Either this or fire request/response event pair for the web socket handshake to expose the headers/response info.
mxschmitt commentedon Jul 7, 2021
It's not possible yet, we are talking about a new API.
Created a feature request here: #7494
mxschmitt commentedon Jul 7, 2021
Closing this issue and we will track the feature request in #7494
Feel free to upvote it there.