2

Is it possible to drive PhantomJS with WebDriver, but also when navigating through various pages, log the network traffic of each page using PhantomJS?

I can't seem to figure out a solution, or find one on the net.

1 Answer 1

2

Yes, certainly.

Start up a proxy server like Browsermob, set it as your Selenium proxy, and for the pages / URL patterns you're interested in, call:

proxy.newHar("<name>");

driver.get(url);

Har har = proxy.getHar();

as per the example on the previous link.

That HAR file will contain the entire content of every individual request and response associated with your page request, which you can then persist, visualise, or query via an API.

Obviously you could automate this, schedule the PhantomJS test runs, and either produce your own custom metrics with your own code; pipe the HAR into a JSON-savvy database for querying (say Elasticsearch) and visualisation, or just save the HARs for offline querying and diffing.

You can visualise the output by obtaining the HAR in string form and pasting into http://www.softwareishard.com/har/viewer/. It gives you an output almost identical to Chrome's Network tab, which goes to show that the data captured is pretty much the same.

1
  • 1
    Thanks, I got BMP setup today with WebDriverIO. From there I was able to collect the network requests I needed to use for some assertions.
    – Brandon
    Commented Mar 23, 2016 at 19:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.