Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detail procedure to generate a har file from a given url via command line tool

Tags:

har

Could anybody advise how to generate a har file from given a url via command line in linux? Detail tools used and guidelines are much appreciated.

Thanks

like image 745
imnew Avatar asked May 16 '12 05:05

imnew


People also ask

How do I create a HAR file on AWS?

In the AWS Management Console, reproduce the issue from your support case. Or, follow the steps that AWS Support advised in a local setup. In the Network Monitor, open the context menu (right-click) on any network request in the request list. Choose Save All As HAR, and then save the file.

How do I create a HAR file in Jira?

Providing Information to Support Complete the steps that trigger or demonstrate your issue. Click on Export as HAR (Floppy button next to the stop button on the Developer Tools) followed by Save As... to save the HAR file.


3 Answers

You can use phantomjs for this work.

phantomjs examples/netsniff.js "some_url" > out.har 

or take a look at the BrowserMob Proxy

like image 79
xdebug Avatar answered Oct 19 '22 20:10

xdebug


I have worked with PhantomJS to produce HAR files but they are not really reliable as opposed to the HAR files generated by actual browsers like Chrome, Firefox. Using selenium and BrowsermobProxy, you can generate HAR files directly from browsers with a python script such as this:

from browsermobproxy import Server
from selenium import webdriver
import json

server = Server("path/to/browsermob-proxy")
server.start()
proxy = server.create_proxy()
profile = webdriver.FirefoxProfile()
profile.set_proxy(self.proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)
proxy.new_har("http://stackoverflow.com", options={'captureHeaders': True})
driver.get("http://stackoverflow.com")    
result = json.dumps(proxy.har, ensure_ascii=False)
print result
proxy.stop()    
driver.quit()

If you are looking for a command line tool that headlessly generates HAR and performance data with Chrome and Firefox, have a look at Speedprofile.

like image 44
Paras Dahal Avatar answered Oct 19 '22 19:10

Paras Dahal


Phantomjs' har files are an abbreviated list of assets. In other words, when you visit a web page with Chrome or another browser, files load over a period of a few seconds.

But phantomjs takes an instantaneous snapshot of that website, before all the assets have had time to load.

It also excludes data and image files (because they're not part of the har spec)

You can work around this by modifying the netsniff.js example file.

I've forked that project and made those modifications at the link below. Please note that I've set the timer to wait 20 seconds before generating the har. I've also added a bit of error handling to ignore js errors. The error handling bit was added to deal with phantomjs creating invalid har files if it encountered an error. (I also commented out the function that excludes data/image files)

So this may not be exactly what you want. But it's a starting point for you or anyone else looking to use phantomjs.

After these changes, I went from consistently getting four asset files to about 25.

https://github.com/associatedpress/phantomjs/blob/netsniff-timer/examples/netsniff.js

like image 1
Bob Avatar answered Oct 19 '22 18:10

Bob