javaselenium-webdriverproxyselenium-chromedriverbrowsermob

Setting up browsermob proxy with ChromeDriver


I'm trying to set up browsermob to work in my selenium project. I was looking for a way to use ChromeOptions to set the proxy, but all sources tell me to use ChromeOptions for everything else, then convert it into DesiredCapabilities before instantiating a new ChromeDriver instance.

This is my code:

ChromeOptions options = new ChromeOptions();
// Setting some chrome features here

ProxyServer proxyServer = new ProxyServer(4444);
proxyServer.start();

Proxy proxy = proxyServer.seleniumProxy();

DesiredCapabilities capabilities = DesiredCapabilities.chrome();

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

WebDriver driver = new ChromeDriver(capabilities); // Error happens here

I'm using Webdriver version 2.44 from the maven repositories. This is the error I get:

java.lang.IllegalAccessError: tried to access field com.google.gson.JsonNull.INSTANCE from class org.openqa.selenium.remote.BeanToJsonConverter

Does anyone know the cause or any alternative solutions for hooking up a proxy to chromedriver?


Solution

  • If you're using an older version of browsermob-proxy, there might be some conflicts between Selenium's dependencies and BMP's. I'd recommend using the latest Selenium + building the latest BrowserMob Proxy from master.

    Once you have the latest versions, you should be able to use Chrome + BMP the "usual" way:

            BrowserMobProxy proxy = new BrowserMobProxyServer();
            proxy.start(); // can specify a port here if you like
    
            // get the selenium proxy object
            Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
    
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
    
            // if chromedriver isn't on your system path, you'll need to set this system property
            System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
            WebDriver driver = new ChromeDriver(capabilities);
    
            driver.get("https://www.google.com/");