2

I have been trying to integrate BrowserMob to my selenium tests. It works fine with website that work on http, but with https websites the browsers stop working and the HAR file doesn't contain any requests.

When navigating to a https site I get this error on the browser.

"There is something wrong with the proxy server or the address is incorrect."

Here is my code.

    public class Browsermob {

  BrowserMobProxy proxy = new BrowserMobProxyServer();

  @Test
  public void browsermobtest() {


    proxy.start(9091);

    // 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);
    System.setProperty("webdriver.chrome.driver", "C:/Users/Madis/Documents/chromedriver.exe");

    WebDriver driver = new ChromeDriver(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 "google.com"
    proxy.newHar("http://www.google.com/");

    // open google.com
    driver.get("https://www.google.ee/#gfe_rd=cr");
    driver.findElement(By.cssSelector("#gb_70")).click();



  }

  @AfterMethod
  public void Afterthetest() {

    // get the HAR data
    Har har = proxy.getHar();

    File harFile = new File("C:/Users/Madis/Documents/har.har");
    try {
      har.writeTo(harFile);
    } catch (IOException e) {
      e.printStackTrace();
    }

  }
}
Madis Kangro
  • 283
  • 3
  • 12

5 Answers5

3

You don't need to specify the sslProxy on the Selenium Proxy object. ClientUtil.createSeleniumProxy does this for you, and in most simple cases it chooses a suitable default value (using InetAddress.getLocalHost(); if that's working for HTTP, it will work for HTTPS as well).

A few things to keep in mind:

  1. You'll receive SSL warnings in the browser unless you either tell the browser to ignore cert errors (on Chrome, use the --ignore-certificate-errors command-line flag), or install the BMP CA in the browser's trust store (for Chrome on Windows, you must install it in the Windows trust store).
  2. Depending on your version of Chrome and OS, you may need to specify an alternate user-data-dir using a command line option. For example, --user-data-dir=/tmp/insecurechrome.
  3. BMP has its own source of trusted certificates (Java trust store + a recent list from Mozilla), so if you're trying to connect to internal websites with certificates issued by a private CA, you need to tell BMP to either trust the private CA or skip certificate validation using .setTrustAllServers(true).
  4. The proxy must be started using .start(...) before calling createSeleniumProxy().

Combining all these things, your code would look something like this:

BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.setTrustAllServers(true);
proxy.start(9091);

// get the Selenium proxy object
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
// NOTE: there is no call to .setSslProxy() here

// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
System.setProperty("webdriver.chrome.driver", "C:/Users/Madis/Documents/chromedriver.exe");

ChromeOptions options = new ChromeOptions();
options.addArgument("--ignore-certificate-errors");
// replace 'somedirectory' with a suitable temp dir on your filesystem
options.addArgument("--user-data-dir=somedirectory");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);

WebDriver driver = new ChromeDriver(capabilities);

// [...]
Jason Hoetger
  • 5,650
  • 2
  • 13
  • 14
  • I copy pasted this and it doesn't seem to work. Same issue as mine. Sites that use https won't load. – Madis Kangro Oct 11 '16 at 12:05
  • Thanks for your help. I managed to get it to work after getting all the debug logs for browsermob. Adding the google guava dependency fixed the issue for me. – Madis Kangro Jan 04 '17 at 12:45
1

I had this problem. After numerous trials, I got to know we have to add setmitmManager and upstream proxy if you are connected to corporate proxy. It worked for me.

Here is the example code.

BrowserMobProxy proxy = new BrowserMobProxyServer();
proxy.setTrustAllServers(true);

//Add below line if you are under corporate proxy. 
proxy.setChainedProxy(new InetSocketAddress("XXX.XXX.com", 8080)); 
proxy.setMitmManager(ImpersonatingMitmManager.builder().trustAllServers(true).build());
proxy.start(9091);

// 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);
System.setProperty("webdriver.chrome.driver","C:/Users/Madis/Documents/chromedriver.exe");


WebDriver driver = new ChromeDriver(capabilities);
// your code to start, get har
Rajesh
  • 21
  • 5
0

You're confusing the browser mob proxy object and the selenium proxy object.

Your proxy variable proxy is the actual proxy which your browser will connect to.

Your seleniumProxy variable is an object which represents your browser's proxy settings.

You are telling your browser to use "trustAllSSLCertificates" as the address for your proxy server, which is why you are getting an error. Instead, you should tell browsermob (proxy) to trustAllSSLCertificates, and your sslProxy needs to reference your browsermob proxy.

Start the proxy like so:

public void startProxy() {
      proxy = new BrowserMobProxyServer();
      proxy.setTrustAllServers(true);
      proxy.start(9091);
}

Start the driver like so:

public void startBrowserWithProxy() {
        Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
        seleniumProxy.setSslProxy("localhost:" + proxy.getPort());
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
        System.setProperty("webdriver.chrome.driver", "C:/Users/Madis/Documents/chromedriver.exe");
        WebDriver driver = new ChromeDriver(capabilities);
}
Harry King
  • 452
  • 3
  • 13
  • Still the same issue. Works fine on a http connection as soon I navigate to a https site then it stops working. – Madis Kangro Sep 07 '16 at 07:54
  • Apologies, I missed the ```proxy.start(9091);``` command from my original example. Were you starting the proxy anyway? – Harry King Sep 07 '16 at 11:25
  • Yes I started the proxy. It works fine when I use it on a website that doesn't use https. Do I need to install some certificates first? – Madis Kangro Sep 07 '16 at 12:27
  • Are you able to share a screenshot of the error? Chrome should be ignoring certificate errors by default when used with Selenium. – Harry King Sep 09 '16 at 08:06
0

I managed to get it to work. After adding log4j and debugging the browsermob logs the issue was caused by

Caught an exception on ClientToProxyConnection
 java.lang.NoSuchMethodError: com.google.common.net.HostAndPort.fromHost(Ljava/lang/String;)Lcom/google/common/net/HostAndPort;

In order to make it to work I had to add a dependency to my maven project. This fixed this issue and I was able to see the capture the traffic on https sites aswell http sites.

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
Madis Kangro
  • 283
  • 3
  • 12
  • Guava is already a dependency of BMP. What BMP dependency are you pulling in? – Jason Hoetger Jan 04 '17 at 19:17
  • @JasonHoetger I'm using net.lightbody.bmp browsermob-core 2.1.4 . The one shown in github currently. – Madis Kangro Jan 05 '17 at 09:13
  • browsermob-core already depends on guava 20. Perhaps you're including guava somewhere else in your pom? You could use `mvn dependency:tree` to be sure. Anyway, glad you got it working. – Jason Hoetger Jan 05 '17 at 11:18
  • @JasonHoetger I used mvn dependency tree and it shows guava only once. The one which I added. After removing it. It showed it under a different dependency which was version 16.0. I think that might have caused the issues I was having. – Madis Kangro Jan 05 '17 at 11:27
  • It sounds like your pom file was forcing the guava dependency to version 16. It may have been somewhere in your pom (including dependencyManagement) or in the parent pom. – Jason Hoetger Jan 06 '17 at 02:54
  • It was another dependency that was forcing it for some reason. Atleast it works now and I can continue. – Madis Kangro Jan 06 '17 at 07:46
0

I hade a lot for capabilities, options and etc but it did not work In my case, I changed exist dependency in pom

<artifactId>browsermob-core-littleproxy</artifactId>

to

<dependency>
       <groupId>net.lightbody.bmp</groupId>
       <artifactId>browsermob-core</artifactId>
       <version>2.1.5</version>
   </dependency>

and up "guava" version. After that everything became good