Selenium and BrowserMob Proxy

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.

How the HAR files will look ?

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

from browsermobproxy import Server
                server = Server("C://browsermob-proxy.bat")
                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")
                driver.get("http://www.google.co.uk")
                proxy.har # returns a HAR JSON bl     



		har = 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()
		

BrowserMob Proxy 是一个基于 Java 的代理服务器,可以用来监控、分析和修改 HTTP(S) 流量。它提供了一个 REST API,可以方便地对代理进行配置和控制。同时,BrowserMob Proxy 还提供了一个 Python 客户端,可以与 Python 程序集成,方便地进行自动化测试、爬虫等任务。 使用 BrowserMob Proxy,您可以实现以下功能: - 拦截 HTTP(S) 请求和响应,对其进行修改和重定向; - 访问 HTTP(S) 流量的详细信息,如请求和响应头、请求参数、响应正文等; - 分析 HTTP(S) 流量,如计算请求和响应时间、统计请求次数、响应码等; - 生成 HAR(HTTP Archive)文件,记录 HTTP(S) 流量的详细信息,方便分析和共享。 BrowserMob Proxy 的 Python 客户端可以通过 pip 安装: ``` pip install browsermob-proxy ``` 安装完成后,您就可以在 Python 程序中使用 BrowserMob Proxy 了。例如,您可以使用以下代码启动代理服务器: ```python from browsermobproxy import Server server = Server("path/to/browsermob-proxy") server.start() proxy = server.create_proxy() ``` 其中,`path/to/browsermob-proxy` 是 BrowserMob Proxy 的安装路径。 启动代理服务器后,您可以将其配置到浏览器中使用,从而实现拦截和修改 HTTP(S) 流量的功能。例如,您可以使用以下代码启动 Chrome 浏览器,并配置其使用代理: ```python from selenium import webdriver chrome_options = webdriver.ChromeOptions() chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy)) driver = webdriver.Chrome(chrome_options=chrome_options) ``` 这样,您就可以在 Python 程序中使用 BrowserMob Proxy 来拦截、分析和修改 HTTP(S) 流量了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值