11

I'm currently using Selenium Webdriver to do some validation on pages. The Webdriver is being driven by PhantomJS. I know that in PhantomJS, you can listen to the network using the example like the one below: (from https://github.com/ariya/phantomjs/wiki/Network-Monitoring).

var page = require('webpage').create();
page.onResourceRequested = function (request) {
    console.log('Request ' + JSON.stringify(request, undefined, 4));
};
page.onResourceReceived = function (response) {
    console.log('Receive ' + JSON.stringify(response, undefined, 4));
};
page.open(url);

How can I achieve this functionality within the Webdriver? Can I bind a function to the DesiredCapabilities?

Marcin
  • 48,559
  • 18
  • 128
  • 201
Matt Stern
  • 717
  • 11
  • 23

1 Answers1

0

What are you trying to achieve here? It is possible to inject javascript. So with that you could create a object that listens to the page and logs this into an object that you grab later on when you made some actions.

Ill give it a try but I'm not sure what phantomJS does.

browser.execute_script("    
var requests= [];
var received = [];
var page = require('webpage').create();
page.onResourceRequested = function (request) {
    requests.push('Request ' + JSON.stringify(request, undefined, 4));
};
page.onResourceReceived = function (response) {
    received.push('Receive ' + JSON.stringify(response, undefined, 4));
};
page.open(url);");

Later (if your still on the same page) to get the requests:

browser.execute_script("function(){return requests }()");

and the received connections:

browser.execute_script("function(){return received}");
HerrWalter
  • 622
  • 1
  • 5
  • 13