Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 835
Julian Avatar asked Apr 29 '15 23:04

Julian


People also ask

How does BrowserMob proxy work?

What is BrowserMob Proxy? On that page its definition is as follows: BrowserMob Proxy allows you to manipulate HTTP requests and responses, capture HTTP content, and export performance data as a HAR file. BMP works well as a standalone proxy server, but it is especially useful when embedded in Selenium tests.

How do I instantiate ChromeDriver?

In order to instantiate the object of ChromeDriver, you can simply create the object with the help of below command. Webdriver driver = New ChromeDriver(); The main motto of the ChromeDriver is to launch Google Chrome. Without that, it is not possible to execute Selenium test scripts in Google Chrome browser.


1 Answers

ChromeDriver doesn't support the proxy caps directly. But it does support passing command line args to the chrome process. And setting the http proxy is one of chrome command line switches. It can be set as follows:

DesiredCapabilities caps = DesiredCapabilities.chrome();    
ArrayList<String> switches = new ArrayList<String>();    
switches.add("--proxy-server=localhost:8080");    
caps.setCapability("chrome.switches", switches);    
webDriver = new ChromeDriver(caps);    
like image 173
jpereira Avatar answered Sep 24 '22 06:09

jpereira