Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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

    Code Block
    languagejava
    themeRDark
    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:

    Code Block
    languagejava
    themeRDark
    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);
    }



...