I want to use browsermob to monitor the network connections when doing a GUI test with selenium. I have found some information and documentation here and here and here, but its absolutely unclear how to really use it.
In the documentation it reads:
server = Server("path/to/browsermob-proxy")
But what is that path? Where to find it?
Also I see
java -jar browsermob.jar --port 9090
but no explanation at all as to what this jar file is, if it is part of the browsermob installation, or something unrelated.
I would appreciate if someone can provide a COMPLETE and WORKING example on how to use browsermob, and what ALL I need to install...
from browsermobproxy import Server
import psutil
import time
for proc in psutil.process_iter():
# check whether the process name matches
if proc.name() == "browsermob-proxy":
proc.kill()
dict = {'port': 8090}
server = Server(path="./BrowserMobProxy/bin/browsermob-proxy", options=dict)
server.start()
time.sleep(1)
proxy = server.create_proxy()
time.sleep(1)
from selenium import webdriver
profile = webdriver.FirefoxProfile()
selenium_proxy = proxy.selenium_proxy()
profile.set_proxy(selenium_proxy)
driver = webdriver.Firefox(firefox_profile=profile)
proxy.new_har("google")
driver.get("http://www.google.co.uk")
print (proxy.har) # returns a HAR JSON blob
server.stop()
driver.quit()
Two things, if your code fails the process could be left open sometimes. So I added the code below for closing the duplicate instances.
import psutil
import time
for proc in psutil.process_iter():
# check whether the process name matches
if proc.name() == "browsermob-proxy":
proc.kill()
Also a sleep of 1 second before and after creating the proxy.
server.start()
time.sleep(1)
proxy = server.create_proxy()
time.sleep(1)
This helps in getting rid of some intermittent issues which can be faced due to the server taking some time to start.