Selenium and BrowserMob Proxy

本文介绍如何使用BrowserMob Proxy与Selenium配合来捕获浏览器的性能数据。BrowserMob Proxy可以收集web应用程序的性能数据并通过HAR格式输出,便于进行页面加载性能等分析。文章还提供了使用Python实现的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值