https://github.com/lightbody/browsermob-proxy里面有详细的代码实例如下:
// start the proxy
BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.start(0);
// get the Selenium proxy object
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
// start the browser up
WebDriver driver = new FirefoxDriver(capabilities);
// enable more detailed HAR capture, if desired (see CaptureType for the complete list)
proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
// create a new HAR with the label “yahoo.com”
proxy.newHar(“yahoo.com”);
// open yahoo.com
driver.get(“http://yahoo.com”);
// get the HAR data
Har har = proxy.getHar();
把网页URL http://yahoo.com换成自己的被测网站,如果得到代理服务器拒绝链接,可以尝试下面的解决方案:
加上:
seleniumProxy.setSslProxy(“localhost:” + proxy.getPort());
seleniumProxy.setHttpProxy(“localhost:” + proxy.getPort());
以及:按照上例中的方法创建一个自定义的profile,在浏览器中导入sslSupport的证书,拷贝出证书内容https://github.com/lightbody/browsermob-proxy/blob/master/browsermob-core/src/main/resources/sslSupport/ca-certificate-rsa.cer,然后导入。
我当前的环境配置中是工作的。当前环境配置详见 Web UI自动化测试框架搭建之一