0

I'm looking to use PhantomJS, via WebDriver/RemoteWebDriver, to monitor and test network activity throughout page loading. I've gotten it to the point where PhantomJS can do everything that Chrome or Firefox can—is loading pages, accessing the DOM, etc.

However, I'm not sure where to go from here in order to be able to access request and response HTTP headers the way I've seen in JavaScript PhantomJS examples. The GhostDriver Javadocs don't seem to mention anything about it.

Has anyone done this?

i-g
  • 69
  • 4
  • 22

3 Answers3

2

The WebDriver API doesn't expose HTTP request and response headers or status codes. PhantomJS may allow you to get them, but the WebDriver API doesn't expose them. It's a point of some contention among some users of the project, but it's not going to change, and that decision has good and sufficient reasons for being so. You might be able to manipulate PhantomJS's ability to retrieve that information (if it allows it) through clever use of WebDriver's executeScript() method, but I really don't know if that will work.

Community
  • 1
  • 1
JimEvans
  • 27,201
  • 7
  • 83
  • 108
1

Try using the Jaunt API. It's headless and supports retrieving the response header.

Features include (taken from the link):

HTML, XHTML, XML parsing. Protocols: HTTP, HTTPS, basic auth. Form fill-out via field labels/names/sequence. Automatic form permutation. File downloading/uploading. Saving complete web page (images, js, css, etc). Table data extraction. DOM navigation, search & search chaining. Regex-enabled querying. HTTP header/cookie manipulation. HTTP/HTTPS proxy support. Customizable caching & content handlers. Pagination discovery. 100% Java (no dependencies)

LittlePanda
  • 2,496
  • 1
  • 21
  • 33
1

Based on this answer, I was able to get this to work in Java and Selenium 3.4.

Create a resource file in your project; call it netlog.js:

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

Then from your Java code, enable the logging like this:

import org.apache.commons.io.IOUtils;
String script = IOUtils.toString(getClass().getResourceAsStream("/netlog.js"));
((PhantomJSDriver) driver).executePhantomJS(script);

All the logging will go to stderr.

There is some additional discussion on the PhantomJS site.

SiKing
  • 10,003
  • 10
  • 39
  • 90