0

i'm using the browser-mob-proxy module and selenium for python in order to record some things that are sent by a website when you are using it.

What I am doing is creating a server using browser-mob-proxy, then creating a proxy for that server. I then create a HAR to record the data. I later use this data for something else.

I want to know if there is a way to reset the HAR file so that it is empty or if I will have to create a new HAR to store new data.

The proxy is assigned to the selenium browser which is using the chrome driver.

1 Answers1

0

I do this in my testing framework so that each test has its own HAR file for debugging purposes. Even when they use the same browser.

The command you are looking for is "new_har". This creates a new session and begins logging to a new HAR file. You can also specify a name for the session. I normally get the old HAR first and save it before clearing and starting a new session. But you don't have to do that if all you want to do is clear the proxy log.

Here is an example using the Python module.

from browsermobproxy import Server
server = Server("path/to/browsermob-proxy")
server.start()
proxy = server.create_proxy()

from selenium import webdriver
profile  = webdriver.FirefoxProfile()
profile.set_proxy(proxy.selenium_proxy())
driver = webdriver.Firefox(firefox_profile=profile)

proxy.new_har("google") # Start first session
driver.get("http://www.google.co.uk")
proxy.har # returns a HAR JSON blob for first session

proxy.new_har("Yahoo") # Start second session
driver.get("http://www.yahoo.co.uk")
proxy.har # returns a HAR JSON blob for second session

server.stop()
driver.quit()
Brian S.
  • 333
  • 1
  • 12