How can I set proxy to selenium remote web driver?

1k views Asked by At

I'm trying to execute aws device farm sample behind corporate proxy. At first I faced a proxy problem when connecting aws. I managed to solve this one by setting aws Proxy Configuration. my another question on this matter

Now I'm facing another proxy problem. When creating remotewebdriver unknown host name error occurred. This is my source code.

@Before
public void setUp() {
    try {
        ProxyConfiguration.Builder proxyConfig = ProxyConfiguration.builder();
        proxyConfig.endpoint(new URI("<YOUR PROXY URL>"));
        proxyConfig.username("<YOUR USER ID>");
        proxyConfig.password("YOUR PASSWORD");
        ApacheHttpClient.Builder httpClientBuilder =
                ApacheHttpClient.builder()
                                .proxyConfiguration(proxyConfig.build());

        String myARN = "<YOUR ARN>";
        DeviceFarmClient client  = DeviceFarmClient.builder()
                .credentialsProvider(DefaultCredentialsProvider.create())
                .region(Region.US_WEST_2)
                .httpClientBuilder(httpClientBuilder)
                .overrideConfiguration(ClientOverrideConfiguration.builder().build())
                .build();
        CreateTestGridUrlRequest request = CreateTestGridUrlRequest.builder()
                .expiresInSeconds(300)        // 5 minutes
                .projectArn(myARN)
                .build();
        URL testGridUrl = null;
        CreateTestGridUrlResponse response = client.createTestGridUrl(request);
        testGridUrl = new URL(response.url());
        driver = new RemoteWebDriver(testGridUrl, DesiredCapabilities.chrome()); //error occurred at this line.
    } catch (Exception e) {
        e.printStackTrace();
    }
}

It's the error log.

UnknownHostException: testgrid-devicefarm.us-west-2.amazonaws.com

I tryed some measures to soleve this problem, but none of these works well. Below are some of my trials.

  1. use browsermob library
DesiredCapabilities cap = DesiredCapabilities.chrome();
BrowserMobProxy bmp = new BrowserMobProxyServer();
bmp.setChainedProxy(
        new InetSocketAddress("<PROXY HOST>", Integer.valueOf("<PROXY PORT>")));
bmp.chainedProxyAuthorization("<USER>", "<PASSWORD>", AuthType.BASIC);
bmp.setTrustAllServers(true);
bmp.start();
Proxy proxy = ClientUtil.createSeleniumProxy(bmp);
cap.setCapability(CapabilityType.PROXY, proxy);
driver = new RemoteWebDriver(testGridUrl, cap);
  1. use browserup instead of browsermob

It also didn't work well and same error (UnknownHostException) was occurred.

  1. use Authenticator.setDefault

It also didn't work well.

Authenticator.setDefault(new Authenticator() {
    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(proxyUserName, proxyPassword.toCharArray());
    }
} );
System.setProperty("http.proxyUser", proxyUserName);
System.setProperty("http.proxyPassword", proxyPassword);

The error message is..

java.io.IOException: Failed to authenticate with proxy

How can I address this issue?

Thanks.

1

There are 1 answers

0
Leo Meirelles On

The Selenium client running in your computer needs to connect to AWS Device Farm using the corporate proxy.

The proxy for your webdriver connection need to be set.

The example in Javascript is below.

const driver = await new Builder()
  .usingWebDriverProxy(process.env.HTTPS_PROXY || '')
  .usingServer(urlString)
  .withCapabilities(...