Browser-mob Proxy不会使用chrome驱动程序捕获网络流量

时间:2017-10-26 12:09:04

标签: java selenium selenium-webdriver browsermob-proxy

我正在使用此代码将浏览器mob代理与maven依赖项集成 net.lightbody.bmp browsermob-core 2.1.5

它根本没有捕获网络请求,我收到了这种har文件:

{
   "log":{
      "version":"1.2",
      "creator":{
         "name":"BrowserMob Proxy",
         "version":"2.1.0-beta-6-littleproxy",
         "comment":""
      },
      "pages":[
         {
            "id":"11",
            "startedDateTime":"2017-10-26T17:28:42.501+05:30",
            "title":"11",
            "pageTimings":{
               "comment":""
            },
            "comment":""
         }
      ],
      "entries":[],
      "comment":""
   }
}
package lenskart.tests;

import java.io.File;
import java.net.UnknownHostException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;

import net.lightbody.bmp.BrowserMobProxyServer;
import net.lightbody.bmp.client.ClientUtil;
import net.lightbody.bmp.mitm.manager.ImpersonatingMitmManager;
import net.lightbody.bmp.proxy.CaptureType;

public class ProxyTestClass {

    @Test
    public static void main() throws Exception {
        // TODO Auto-generated method stub

        BrowserMobProxyServer browserMobProxy = new BrowserMobProxyServer();
        browserMobProxy.setTrustAllServers(true);
        browserMobProxy.setMitmManager(ImpersonatingMitmManager.builder().trustAllServers(true).build());
        browserMobProxy.start(0);

        System.out.println("Port Started On: " + browserMobProxy.getPort());
        System.setProperty("webdriver.chrome.driver", "/Users/pankaj.katiyar/Desktop/Automation/Lenskart_Automation/tpt/drivers/chromedriver");

        browserMobProxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT, CaptureType.RESPONSE_HEADERS);

        WebDriver driver = getDriver_CapProxy(browserMobProxy);

        driver.get("http://www.lenskart.com");
        driver.navigate().to("http://www.google.com");

        driver.quit();

        browserMobProxy.stop();

        browserMobProxy.newHar("11");
        browserMobProxy.getHar().writeTo(new File("/Users/pankaj.katiyar/Desktop/Automation/Lenskart_Automation/har"));
        ;
        System.out.println("Loaded browser ");
    }


    public static WebDriver getDriver_CapProxy(BrowserMobProxyServer browserMobProxy) throws UnknownHostException {
        Proxy proxy = ClientUtil.createSeleniumProxy(browserMobProxy);
        proxy.setHttpProxy("localhost:" + browserMobProxy.getPort());

        DesiredCapabilities cap = new DesiredCapabilities();

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--ignore-certificate-errors");

        cap.setCapability(ChromeOptions.CAPABILITY, options);
        cap.setCapability(CapabilityType.PROXY, proxy);

        WebDriver driver = new ChromeDriver(options);

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        return driver;
    }
}

2 个答案:

答案 0 :(得分:3)

我运行了您的代码,只需要进行两项更改,更改

self.chosenFolderData

WebDriver driver = new ChromeDriver(options);

在导航

之前移动WebDriver driver = new ChromeDriver(cap);
browserMobProxy.newHar("11");

在代码中休息一切正常。一旦你这样做,Har就生成了很好,如下面的截图

所示

Generated HAR

答案 1 :(得分:2)

您的代码存在两个问题。

首先将selenium代理分配给功能,但实例永远不会被使用。所以要么assign the options to the capabilities,要么直接将selenium代理分配给选项:

Proxy seleniumProxy  = ClientUtil.createSeleniumProxy(browserMobProxy);

ChromeOptions options = new ChromeOptions();
options.addArguments("--ignore-certificate-errors");
options.setCapability(CapabilityType.PROXY, seleniumProxy);

WebDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

其次,你应该在录音结束时调用browserMobProxy.newHar,它应该在开头:

browserMobProxy.newHar("11");

driver.get("http://www.lenskart.com");
driver.navigate().to("http://www.google.com");

browserMobProxy.getHar().writeTo(new File("/Users/pankaj.katiyar/Desktop/Automation/Lenskart_Automation/har"));

driver.quit();
browserMobProxy.stop();