2

My goal is to connect to a website using PhantomJS and print incoming network traffic to an output file in HAR format.

Using the netsniff.js example, I can record all network traffic when first loading a site but I do not know how to capture further incoming traffic.

For example if you go to the front page of stack exchange, the stack sites will intermittently change and a new .png file will be received (this can be seen by looking in the network tab of Chrome developer tools or something similar for other browsers). I would like a way of capturing the HAR for all of these indefinitely. How is this possible?

rwolst
  • 10,604
  • 13
  • 44
  • 67
  • I was going to say it is as simple as removing the `Phantom.exit()` call, and it will keep on listening. But I did some quick tests with both PhantomJS and SlimerJS, but got nothing come through. Perhaps something else is needed, or perhaps the websites I chose never do another Ajax request... – Darren Cook Mar 23 '14 at 01:22
  • Looking at netsniff.js, I don't think removing `Phantom.exit()` will do much because by the time it reaches that point in the code there is nothing more for the program to do. Exiting simply returns you back to the command line. – rwolst Mar 24 '14 at 16:45

1 Answers1

0

Using netsniff.js, add a setTimeout wrapper around creating the HAR and outputting it.

setTimeout(function(){
    har = createHAR(page.address, page.title, page.startTime, page.resources);
    console.log(JSON.stringify(har, undefined, 4));
    phantom.exit();
},10000); /* pause 10 seconds to let things run after onLoad */
Donald E. Foss
  • 163
  • 1
  • 8