使用Selenium和Browsermob代理

时间:2015-05-13 10:00:15

标签: browsermob

我正在使用带有selenium webdriver的Browsermob代理来捕获HAR日志。我能够生成日志但是没有捕获pageTimings。休息所有数据都被捕获。知道这里有什么问题吗?

我是否需要使用此呼叫等待一些时间:

  

PUT / proxy / [port] / wait - 等到所有请求都被发送

enter image description here 在简短回答后更新:

  1. 从git中获取最新快照,构建它。
  2. 从分发模块启动代理 - ' Main'程序
  3. 捕获日志
  4. 在每个页面源更改后,我正在调用强制代理通过PUTing到/ proxy / {port} / har / pageRef
  5. 来结束页面
  6. 使用页面引用生成日志,但仍未填充pageTimings。
  7. 更新了截图:

    enter image description here

    使用小代理后日志出错:

    [ERROR 2015-05-18T10:29:00,288 org.littleshoot.proxy.impl.ClientToProxyConnection] (LittleProxy-ClientToProxyWorker-1) (AWAITING_INITIAL) [id: 0x1961ff09, /0:0:0:0:0:0:0:1:63598 => /0:0:0:0:0:0:0:1:8445]: Caught an exception on ClientToProxyConnection java.io.IOException: An existing connection was forcibly closed by the remote host
        at sun.nio.ch.SocketDispatcher.read0(Native Method) ~[?:1.8.0_45]
        at sun.nio.ch.SocketDispatcher.read(Unknown Source) ~[?:1.8.0_45]
        at sun.nio.ch.IOUtil.readIntoNativeBuffer(Unknown Source) ~[?:1.8.0_45]
        at sun.nio.ch.IOUtil.read(Unknown Source) ~[?:1.8.0_45]
        at sun.nio.ch.SocketChannelImpl.read(Unknown Source) ~[?:1.8.0_45]
        at io.netty.buffer.UnpooledUnsafeDirectByteBuf.setBytes(UnpooledUnsafeDirectByteBuf.java:447) ~[netty-all-4.0.27.Final.jar:4.0.27.Final]
        at io.netty.buffer.AbstractByteBuf.writeBytes(AbstractByteBuf.java:881) ~[netty-all-4.0.27.Final.jar:4.0.27.Final]
        at io.netty.buffer.WrappedByteBuf.writeBytes(WrappedByteBuf.java:641) ~[netty-all-4.0.27.Final.jar:4.0.27.Final]
        at io.netty.channel.socket.nio.NioSocketChannel.doReadBytes(NioSocketChannel.java:241) ~[netty-all-4.0.27.Final.jar:4.0.27.Final]
        at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:119) [netty-all-4.0.27.Final.jar:4.0.27.Final]
        at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:511) [netty-all-4.0.27.Final.jar:4.0.27.Final]
        at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:468) [netty-all-4.0.27.Final.jar:4.0.27.Final]
        at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:382) [netty-all-4.0.27.Final.jar:4.0.27.Final]
        at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:354) [netty-all-4.0.27.Final.jar:4.0.27.Final]
        at io.netty.util.concurrent.SingleThreadEventExecutor$2.run(SingleThreadEventExecutor.java:111) [netty-all-4.0.27.Final.jar:4.0.27.Final]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_45]
    

2 个答案:

答案 0 :(得分:1)

简短回答:如果您使用的是2.1.0-beta-1或更高版本(请参阅github page),您可以强制代理通过PUTing结束页面proxy / {port} / har / pageRef。这将填充HAR中的pageTimings对象。 (它也将开始一个新的页面。)

更新:HAR的“创建者”部分应指示正在使用的littleproxy模块:

"log":{
   "version":"1.2",
   "creator":{
      "name":"BrowserMob Proxy",
      "version":"2.1.0-beta-1-littleproxy",
      "comment":""
   },

以下是结束第一页后填充的pageTimings部分的示例:

  "pages":[
     {
        "id":"Page 0",
        "startedDateTime":"2015-05-16T12:37:48.406-07:00",
        "title":"Page 0",
        "pageTimings":{
           "onLoad":89648,
           "comment":""
        },
        "comment":""
     },
     {
        "id":"Page 1",
        "startedDateTime":"2015-05-16T12:39:18.054-07:00",
        "title":"Page 1",
        "pageTimings":{
           "comment":""
        },
        "comment":""
     }
  ],

长答案:与网络浏览器不同,BrowserMob Proxy实际上并不知道“页面”是什么。它只能查看单个请求和响应,因此您需要在页面开始和结束时明确告知BMP。 Java interface提供newPage()方法,REST API提供/proxy/{port}/har/pageRef端点。但是,由于BMP只是一个代理,因此请求可能来自多个浏览器/客户端同时存在多个页面,而BMP无法知道哪个请求属于哪个页面。根据您的使用情况,页面计时信息可能没有意义。

答案 1 :(得分:0)

它对我有用:

BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.start(0);
HashSet<CaptureType> enable = new HashSet<CaptureType>();
enable.add(CaptureType.REQUEST_HEADERS);
enable.add(CaptureType.REQUEST_CONTENT);
enable.add(CaptureType.RESPONSE_HEADERS);
proxy.enableHarCaptureTypes(enable);
HashSet<CaptureType> disable = new HashSet<CaptureType>();
disable.add(CaptureType.REQUEST_COOKIES);
disable.add(CaptureType.RESPONSE_COOKIES);
proxy.disableHarCaptureTypes(disable);

//get the Selenium proxy object
Proxy selProxy = ClientUtil.createSeleniumProxy(proxy);

capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, selProxy);
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);

WebDriver driver = new FirefoxDriver(new FirefoxBinary(),profile,capabilities);


driver.get(url);

Har har = proxy.getHar();