How to track WebSocket communication in Phantom.js?

Documentation

Phantom.js shows how to track HTTP communications: http://phantomjs.org/network-monitoring.html

However, it does not work for WebSockets. How can I track the WebSocket connection in Phantom.js?

+4
source share
2 answers

PhantomJS 1.x does not support WebSockets 1 so you cannot control them. If a page uses some backups for WebSockets, it page.onResourceRequestedand page.onResourceReceivedcan be used for registration of metadata messages. PhantomJS does not disclose factual data sent in any way.

WebSockets PhantomJS 2. , . . - WebSocket :

page.onInitialized = function(){
    page.evaluate(function(){
        (function(w){
            var oldWS = w.WebSocket;
            w.WebSocket = function(uri){
                this.ws = new oldWS(uri);
                ...
            };
            w.WebSocket.prototype.send = function(msg){
                w.callPhantom({type: "ws", sent: "msg"});
                this.ws.send(msg);
            };
            ...
        })(window);
    });
};
page.onCallback = function(data){
    console.log(JSON.stringify(data, undefined, 4));
};

1 , - - v1.9.6 , v1.9.0.

+8

WebSockets, page.onResourceRequested page.onResourceReceived . PhantomJS , .

Slimer JS . Slimer JS . phantom JS 1.x, 2.x.

https://github.com/ariya/phantomjs/issues/10158

slimerjs, XHR-, websocket. , - - (page.onInitialized), - , XHR, .

page.captureContent = [ /application\/javascript/ ]; 

page.onInitialized = function() {
    page.evaluate(function() {

        (function(w) {
            window.WebSocket = undefined; // we are explicitly disabling websockets for the page. so that the website will be using the fallback

        })(window);
    });
};

page.open(url, function(status) {

})
+1

Source: https://habr.com/ru/post/1568934/


All Articles