http://assertselenium.com/2012/11/02/performance-data-collection-using-browsermob-proxy-and-selenium/

BrowserMob Proxy with Selenium

BrowserMob Proxy is a simple utility that makes it easy to capture performance data from browsers, typically written using automation toolkits such as Selenium and Watir.

BrowserMob Proxy can capture performance data for web apps (via the HAR format). Used to collect the performance data from the client

side.

What is a HAR format ?

HAR(HTTP Archive) is a online tool visualizing. HAR is produced by the HTTP tracking tools, these files captures the details of the client/server communication and this can be used for analysis like Page Load Performance.

proxy is programmatically controlled via a REST interface or by being embedded directly inside Java-based programs and unit tests.It can control HTTP trafic for,

blacklisting and whitelisting certain URL patterns

simulating various bandwidth and latency

remapping DNS lookups

flushing DNS caching

controlling DNS and request timeouts

automatic BASIC authorization

How to Use BrowserMob Proxy?

Step 1: Download the BrowserMob Proxy

Step 2: Note Down the path of  `browsermob-proxy.bat`for Windows in the bin directory

Step 3: Programming

The Python Implementation

frombrowsermobproxyimportServerserver=Server("C://browsermob-proxy.bat")server.start()proxy=server.create_proxy()fromseleniumimportwebdriver

profile=webdriver.FirefoxProfile()profile.set_proxy(proxy.selenium_proxy())driver=webdriver.Firefox(firefox_profile=profile)proxy.new_har("google")driver.get("http://www.google.co.uk")proxy.har# returns a HAR JSON blhar = proxy.har

file_object = open("C:\\test_baidu.har", "w")

file_object.write(str(har))#dict object to string object

file_object.close()

server.stop()

driver.quit()

更多推荐