Search code examples
javaseleniumbrowsermob

Selenium (Chrome) and BrowserMob doesn't work for https


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

  }
}

Solution

  • 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>