There may be situations where you want to connect to the Sauce Labs browser cloud through a proxy. For tests written in Java, you can use the proxyhost
settings to specify the URL and port for both http
and https
connections, as shown in this example:
System.getProperties().put("http.proxyHost", proxyHost);
System.getProperties().put("http.proxyPort", proxyPort);
System.getProperties().put("https.proxyHost", proxyHost);
System.getProperties().put("https.proxyPort", proxyPort);
Note that this will route all traffic coming from your test framework through the proxy. This may not be a desired condition for your testing environment if, for example, you are calling internal services from your test script which should not be routed through the proxy. In these cases, you can exclude such internal services by specifying their domain name (e.g. internameHostName) as follows:
System.getProperties().put("http.nonProxyHosts", "internalHostname");
NOTE: Unlike the other properties above, the http.nonProxyHosts
property affects both HTTP and HTTPS. In particular, setting the non-existant https.nonProxyHosts
has no effect.
If the proxy requires authentication, use the following code as well before the creation of the RemoteWebDriver:
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);
System.setProperty("https.proxyUser", authUser);
System.setProperty("https.proxyPassword", authPassword);