Page tree
Skip to end of metadata
Go to start of metadata

Last updated: Jan 28, 2020 13:07


Starting from Selenium version 3.141.59 the Apache HTTP client has been removed and replaced with OkHttpClient, here is an example on how to use OkHttpClient to connect via proxy.

Step-by-step guide

  1. Use the following code to create the MyHttpClientFactory class:

    import java.net.URL;
    import org.openqa.selenium.remote.http.HttpClient;
    import org.openqa.selenium.remote.http.HttpClient.Builder;
    import org.openqa.selenium.remote.internal.OkHttpClient;
    
    public class MyHttpClientFactory implements org.openqa.selenium.remote.http.HttpClient.Factory{
    
    final OkHttpClient okHttpClient;
    
    public MyHttpClientFactory(OkHttpClient okHttpClient) {
    this.okHttpClient = okHttpClient;
    }
    
    @Override
    public org.openqa.selenium.remote.http.HttpClient createClient(URL url) {
    // TODO Auto-generated method stub
    return (HttpClient) okHttpClient;
    }
    @Override
    public void cleanupIdleClients() {
    // TODO Auto-generated method stub
    
    }
    
    @Override
    public Builder builder() {
    return null;
    } 
    }



  2. For Selenium, use the following code to create the driver:

    import java.io.*;
    import java.net.InetSocketAddress;
    import java.net.MalformedURLException;
    import java.net.Proxy;
    import java.net.URL;
    import java.util.*;
    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.*;
    import org.openqa.selenium.html5.*;
    import org.openqa.selenium.logging.*;
    import org.openqa.selenium.remote.*;
    import org.openqa.selenium.remote.http.HttpClient.Factory;
    import org.openqa.selenium.remote.internal.OkHttpClient;
    import org.openqa.selenium.Cookie.Builder;
    import org.openqa.selenium.NoSuchElementException;
    
    import io.appium.java_client.*;
    import io.appium.java_client.android.*;
    import io.appium.java_client.ios.*;
    import okhttp3.*;
    import okhttp3.Response;
    
    public static RemoteWebDriver connectViaProxy(DesiredCapabilities caps) {
    String proxyHost = "127.0.0.1";
    int proxyPort = 8080;
    String host="yourCloud.perfectomobile.com";
    final String username = "username";
    final String password = "password";
    
    URL url;
    try {
    url = new URL("https://" + host + "/nexperience/perfectomobile/wd/hub");
    } catch (MalformedURLException e) {
    throw new RuntimeException(e.getMessage(), e);
    }
    
    Authenticator proxyAuthenticator = new Authenticator() {
    @Override
    public Request authenticate(Route route, Response response) throws IOException {
    String credential = Credentials.basic(username, password);
    return response.request().newBuilder()
    .header("Proxy-Authorization", credential)
    .build();
    }
    };
    
    okhttp3.OkHttpClient.Builder client = new okhttp3.OkHttpClient.Builder()
    .connectTimeout(60, TimeUnit.SECONDS)
    .writeTimeout(60, TimeUnit.SECONDS)
    .readTimeout(60, TimeUnit.SECONDS)
    .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
    .proxyAuthenticator(proxyAuthenticator);
    
    
    Factory factory = new MyHttpClientFactory(new OkHttpClient(client.build(), url));
    HttpCommandExecutor executor = new HttpCommandExecutor(new HashMap<String, CommandInfo>(), url, factory);
    return new RemoteWebDriver(executor, caps);
    }



3. For Appium, reuse the above code but modify the last two lines:


  • Use the following line to create the executor:

HttpCommandExecutor httpCommandExecutor = new HttpCommandExecutor(MobileCommand.commandRepository, url, factory);


  • Then use httpCommandExecutor to create and return the Android or iOS driver


    Here is an example for creating an Android driver:

        public static AndroidDriver<MobileElement> connectViaProxy(DesiredCapabilities caps) {
            String proxyHost = "127.0.0.1";
            int proxyPort = 8080;
            String host="yourCloud.perfectomobile.com";
            final String username = "username";
            final String password = "password";
            
            URL url;
            try {
                url = new URL("https://" + host + "/nexperience/perfectomobile/wd/hub");
            } catch (MalformedURLException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
    
            Authenticator proxyAuthenticator = new Authenticator() {
            @Override
              public Request authenticate(Route route, Response response) throws IOException {
                   String credential = Credentials.basic(username, password);
                   return response.request().newBuilder()
                       .header("Proxy-Authorization", credential)
                       .build();
              }
            };
    
            okhttp3.OkHttpClient.Builder client = new okhttp3.OkHttpClient.Builder()
                .connectTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
                .proxyAuthenticator(proxyAuthenticator);
            
    
            Factory factory = new MyHttpClientFactory(new OkHttpClient(client.build(), url));
            HttpCommandExecutor executor = new HttpCommandExecutor(MobileCommand.commandRepository, url, factory);
            return new AndroidDriver<>(executor, caps);
        }



For proxy configuration using Apache Http Client please refer to the following article - Java - Selenium/Appium Proxy Configuration