Skip to content

Commit

Permalink
Add proxy support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ardesco committed May 11, 2015
1 parent c137e77 commit 1e123b9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ You can even specify multiple threads (you can do it on a grid as well!):

- -Dthreads=2

You can also specify a proxy to use

- -DproxyEnabled=true
- -DproxyHost=localhost
- -DproxyPort=8080

If the tests fail screenshots will be saved in ${project.basedir}/target/screenshots

If you need to force a binary overwrite you can do:
Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
<seleniumGridURL/>
<platform/>
<browserVersion/>
<proxyEnabled>false</proxyEnabled>
<proxyHost/>
<proxyPort/>
</properties>

<dependencies>
Expand Down Expand Up @@ -130,6 +133,9 @@
<gridURL>${seleniumGridURL}</gridURL>
<desiredPlatform>${platform}</desiredPlatform>
<desiredBrowserVersion>${browserVersion}</desiredBrowserVersion>
<proxyEnabled>${proxyEnabled}</proxyEnabled>
<proxyHost>${proxyHost}</proxyHost>
<proxyPort>${proxyPort}</proxyPort>

<!--Set properties passed in by the driver binary downloader-->
<phantomjs.binary.path>${phantomjs.binary.path}</phantomjs.binary.path>
Expand Down
48 changes: 46 additions & 2 deletions src/test/java/com/lazerycode/selenium/config/DriverType.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lazerycode.selenium.config;

import org.openqa.selenium.Platform;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
Expand All @@ -14,14 +15,24 @@

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import static java.lang.Boolean.getBoolean;
import static org.openqa.selenium.Proxy.ProxyType.MANUAL;
import static org.openqa.selenium.remote.CapabilityType.PROXY;

public enum DriverType implements DriverSetup {

FIREFOX {
public DesiredCapabilities getDesiredCapabilities() {
return DesiredCapabilities.firefox();
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
if (proxyEnabled) {
capabilities.setCapability(PROXY, getSeleniumProxyDetails());
}
return capabilities;
}

public WebDriver getWebDriverObject(DesiredCapabilities capabilities) {
Expand All @@ -35,6 +46,9 @@ public DesiredCapabilities getDesiredCapabilities() {
HashMap<String, String> chromePreferences = new HashMap<String, String>();
chromePreferences.put("profile.password_manager_enabled", "false");
capabilities.setCapability("chrome.prefs", chromePreferences);
if (proxyEnabled) {
capabilities.setCapability(PROXY, getSeleniumProxyDetails());
}
return capabilities;
}

Expand All @@ -53,6 +67,9 @@ public DesiredCapabilities getDesiredCapabilities() {
capabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
capabilities.setCapability(InternetExplorerDriver.ENABLE_PERSISTENT_HOVERING, true);
capabilities.setCapability("requireWindowFocus", true);
if (proxyEnabled) {
capabilities.setCapability(PROXY, getSeleniumProxyDetails());
}
return capabilities;
}

Expand All @@ -69,6 +86,9 @@ public String getWebDriverSystemPropertyKey() {
public DesiredCapabilities getDesiredCapabilities() {
DesiredCapabilities capabilities = DesiredCapabilities.safari();
capabilities.setCapability("safari.cleanSession", true);
if (proxyEnabled) {
capabilities.setCapability(PROXY, getSeleniumProxyDetails());
}
return capabilities;
}

Expand All @@ -78,7 +98,11 @@ public WebDriver getWebDriverObject(DesiredCapabilities capabilities) {
},
OPERA {
public DesiredCapabilities getDesiredCapabilities() {
return DesiredCapabilities.operaBlink();
DesiredCapabilities capabilities = DesiredCapabilities.operaBlink();
if (proxyEnabled) {
capabilities.setCapability(PROXY, getSeleniumProxyDetails());
}
return capabilities;
}

public WebDriver getWebDriverObject(DesiredCapabilities capabilities) {
Expand All @@ -94,6 +118,16 @@ public String getWebDriverSystemPropertyKey() {
public DesiredCapabilities getDesiredCapabilities() {
DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
capabilities.setCapability("takesScreenshot", true);
final List<String> cliArguments = new ArrayList<String>();
cliArguments.add("--web-security=false");
cliArguments.add("--ssl-protocol=any");
cliArguments.add("--ignore-ssl-errors=true");
if (proxyEnabled) {
cliArguments.add("--proxy-type=http");
cliArguments.add("--proxy=" + proxyDetails);
} else {
cliArguments.add("--proxy-type=none");
}
return capabilities;
}

Expand All @@ -111,6 +145,16 @@ public String getWebDriverSystemPropertyKey() {
public static final boolean useRemoteWebDriver = Boolean.valueOf(System.getProperty("remoteDriver"));
private static final String operatingSystem = System.getProperties().getProperty("os.name").toUpperCase();
private static final String systemArchitecture = System.getProperties().getProperty("os.arch");
public static boolean proxyEnabled = getBoolean("proxyEnabled");
private static final String proxyDetails = String.format("%s:%s", System.getProperty("proxy"), System.getProperty("proxyPort"));

public Proxy getSeleniumProxyDetails() {
Proxy proxy = new Proxy();
proxy.setProxyType(MANUAL);
proxy.setHttpProxy(proxyDetails);
proxy.setSslProxy(proxyDetails);
return proxy;
}

public String getWebDriverSystemPropertyKey() {
return null;
Expand Down

0 comments on commit 1e123b9

Please sign in to comment.