Use Java RemoteWebDriver with a proxy

In many cases, the machine running the RemoteWebDriver connects to the network through a proxy. Learn how to configure the RemoteWebDriver to direct requests to the proxy when working with Java. The configuration involves setting the Java properties proxy host and port for HTTP and proxy host and port for HTTPS. Often, the values for HTTP and HTTPS are the same.

To configure Java RemoteWebDriver with a proxy:

  1. Set the following properties before the RemoteWebDriver is created.

    Copy
    System.getProperties().put("http.proxyHost", proxyHost);
    System.getProperties().put("http.proxyPort", proxyPort);
    System.getProperties().put("https.proxyHost", proxyHost);
    System.getProperties().put("https.proxyPort", proxyPort);

    For example:

    Copy
    System.getProperties().put("http.proxyHost", "proxy.perfectomobile.com");
    System.getProperties().put("http.proxyPort", "8080");
    System.getProperties().put("https.proxyHost", "proxy.perfectomobile.com");
    System.getProperties().put("https.proxyPort", "8080");
    Important: Do not use Selenium Proxy because it is not relevant for this flow. Selenium Proxy is used for the communication between Selenium and the tested application. When working with Perfecto, the connection to the application (that is to the device) doesn't involve a proxy.
  2. If the proxy requires authentication, also use the following code before the creation of the RemoteWebDriver.

    Copy
    import java.net.Authenticator;
    import java.net.PasswordAuthentication;
    final String authUser = "user";
    final String authPassword = "password"
    Authenticator.setDefault(new Authenticator() {
        public PasswordAuthentication getPasswordAuthentication() {
           return new PasswordAuthentication(authUser, authPassword.toCharArray());
        }
    } );
    System.setProperty("http.proxyUser", authUser); System.setProperty("http.proxyPassword", authPassword);