playwright icon indicating copy to clipboard operation
playwright copied to clipboard

How to intercept websocket communication?

Open theskhan opened this issue 6 years ago • 8 comments

I did not find any example/api using which I could intercept websocket communication.

I have a website connecting to remote server through websocket, I want to intercept and listen to the communication, how do I achieve that?

theskhan avatar Jun 14 '20 17:06 theskhan

Do you need to listen only or to intercept as well? I had web socket instrumentation in works, but was waiting for the customer.

pavelfeldman avatar Jun 14 '20 23:06 pavelfeldman

Actually both.
Would be great if you could share that ?

theskhan avatar Jun 15 '20 11:06 theskhan

I only had API for web socket frame inspection, not interception. If that would be helpful - I'll go back to it and finish my implementation for WebKit (I already had it working for Chromium and Firefox). But if you anyways need to mock responses, that's something that would be very hard to do in Chromium, so achieving it could be simpler via mocking the web socket API in the user land instead.

pavelfeldman avatar Jun 15 '20 15:06 pavelfeldman

hmm..

My need was to intercept the websocket communication, take action when specific message received, send the message back to the server through the same websocket.. something like man in the middle without disturbing the normal communication.

I looked into Selenium, but there was no easy way to do this. The only way I found is to monkey patch the low level networking APIs.

Something like this: https://stackoverflow.com/questions/57893407/capturing-websocket-messages-from-network-tab https://chromedevtools.github.io/devtools-protocol/tot/Network/#event-webSocketClosed

It would have been better if browser autmation frameworks provide this as built in feature with simpler APIs.

I had hope that Playwright would have had this feature, but sadly no.

theskhan avatar Jun 16 '20 12:06 theskhan

DevTools protocol you point at does not have web socket interception, just sniffing, so you can't do man in the middle. But you can totally do it in the userland via calling page.addInitScript and replacing WebSocket class with your own.

pavelfeldman avatar Jun 17 '20 17:06 pavelfeldman

Ok, thanks for clarifying. I think what you mention should work. I will try that. Thanks!

theskhan avatar Jun 18 '20 05:06 theskhan

@pavelfeldman I've found the pull request you mentioned #542 which is displayed as merged into master. However browsing master I cannot find the docs.

Being able to inspect websocket messages would be great for my use case. However I might need it to work with Firefox.

Edit: I digged a little deeper and found the revert in #961

TobiasPfeifer avatar Jun 18 '20 14:06 TobiasPfeifer

To sum up the issue:

  • the websocket feature is discussed here: https://github.com/microsoft/playwright/issues/2695
  • there's also a neat workaround for those who are interested

Let's continue discussion in #2695.

aslushnikov avatar Jun 24 '20 22:06 aslushnikov