Skip to content

Commit f4fce2f

Browse files
committed
Merge pull request #20 from robertgates55/Adding_Proxy_Support
Added proxy support by adding a new system property
2 parents 95eb32d + 5af0fb3 commit f4fce2f

File tree

2 files changed

+87
-21
lines changed

2 files changed

+87
-21
lines changed

src/main/java/com/frameworkium/config/DriverType.java

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
package com.frameworkium.config;
22

3-
import com.frameworkium.capture.ScreenshotCapture;
4-
import com.frameworkium.listeners.CaptureListener;
5-
import com.frameworkium.listeners.EventListener;
3+
import static com.frameworkium.config.DriverSetup.useRemoteDriver;
4+
import static com.frameworkium.config.SystemProperty.APP_PATH;
5+
import static com.frameworkium.config.SystemProperty.GRID_URL;
6+
import static com.frameworkium.config.SystemProperty.MAXIMISE;
7+
import static com.frameworkium.config.SystemProperty.PROXY;
8+
69
import org.apache.logging.log4j.LogManager;
710
import org.apache.logging.log4j.Logger;
11+
import org.openqa.selenium.Proxy;
12+
import org.openqa.selenium.Proxy.ProxyType;
813
import org.openqa.selenium.WebDriver;
14+
import org.openqa.selenium.remote.CapabilityType;
915
import org.openqa.selenium.remote.DesiredCapabilities;
1016

11-
import static com.frameworkium.config.DriverSetup.useRemoteDriver;
12-
import static com.frameworkium.config.SystemProperty.*;
17+
import com.frameworkium.capture.ScreenshotCapture;
18+
import com.frameworkium.listeners.CaptureListener;
19+
import com.frameworkium.listeners.EventListener;
1320

1421
public abstract class DriverType {
1522

1623
protected WebDriverWrapper webDriverWrapper;
1724

1825
protected final static Logger logger = LogManager.getLogger(DriverType.class);
26+
private static final String HOSTNAME_OR_IP_AND_PORT_REGEX = "[\\dA-Za-z.:%-]+";
1927

2028
/**
2129
* Creates the Wrapped Driver object, and returns to the test
@@ -26,14 +34,73 @@ public void instantiate() {
2634
logger.info("Current Browser Selection: " + this);
2735

2836
DesiredCapabilities caps = getDesiredCapabilities();
37+
38+
Proxy currentProxy = getProxy();
39+
if (currentProxy != null) {
40+
caps.setCapability(CapabilityType.PROXY, currentProxy);
41+
}
42+
2943
logger.info("Caps: " + caps.toString());
3044

3145
WebDriverWrapper eventFiringWD = new WebDriverWrapper(getWebDriverObject(caps));
3246
eventFiringWD.register(new EventListener());
3347
if (ScreenshotCapture.isRequired()) {
3448
eventFiringWD.register(new CaptureListener());
3549
}
36-
webDriverWrapper = eventFiringWD;
50+
this.webDriverWrapper = eventFiringWD;
51+
}
52+
53+
/**
54+
* This method returns a proxy object with settings set by the system properties. If no valid proxy argument is set
55+
* then it returns null.
56+
*
57+
* @return A Selenium proxy object for the current system properties or null if no valid proxy settings
58+
*/
59+
public Proxy getProxy() {
60+
if (PROXY.isSpecified()) {
61+
Proxy proxy = new Proxy();
62+
String proxyString = PROXY.getValue().toLowerCase();
63+
switch (proxyString) {
64+
65+
case "system":
66+
proxy.setProxyType(ProxyType.SYSTEM);
67+
logger.info("Using system proxy");
68+
break;
69+
case "autodetect":
70+
proxy.setProxyType(ProxyType.AUTODETECT);
71+
logger.info("Using autodetected proxy");
72+
break;
73+
case "direct":
74+
proxy.setProxyType(ProxyType.DIRECT);
75+
logger.info("Using direct (no) proxy");
76+
break;
77+
default:
78+
proxy.setProxyType(ProxyType.MANUAL);
79+
if (verifyProxyAddress(proxyString)) {
80+
proxy.setHttpProxy(proxyString).setFtpProxy(proxyString).setSslProxy(proxyString);
81+
String logMessage = String
82+
.format("Set all protocols to use proxy with address %s", proxyString);
83+
logger.info(logMessage);
84+
} else {
85+
logger.error("Invalid proxy setting specified, acceptable values are: system, autodetect, direct or {hostname}:{port}. Tests will now use default setting for your browser");
86+
return null;
87+
}
88+
break;
89+
}
90+
return proxy;
91+
}
92+
return null;
93+
}
94+
95+
/**
96+
* This helper method verifies that a value is suitable for usage as a proxy address. Selenium expects values of the
97+
* format hostname:port or ip:port
98+
*
99+
* @param proxyAddress The proxy value to verify
100+
* @return true if value is acceptable as a proxy, false otherwise
101+
*/
102+
private boolean verifyProxyAddress(final String proxyAddress) {
103+
return proxyAddress.matches(HOSTNAME_OR_IP_AND_PORT_REGEX);
37104
}
38105

39106
/**
@@ -42,7 +109,7 @@ public void instantiate() {
42109
* @return - Initialised WebDriverWrapper
43110
*/
44111
public WebDriverWrapper getDriver() {
45-
return webDriverWrapper;
112+
return this.webDriverWrapper;
46113
}
47114

48115
/**
@@ -59,8 +126,8 @@ public static boolean isMobile() {
59126
*/
60127
public void maximiseBrowserWindow() {
61128
if (!MAXIMISE.isSpecified() || Boolean.parseBoolean(MAXIMISE.getValue())) {
62-
if((!useRemoteDriver() && !isNative()) || GRID_URL.isSpecified()) {
63-
webDriverWrapper.manage().window().maximize();
129+
if (!useRemoteDriver() && !isNative() || GRID_URL.isSpecified()) {
130+
this.webDriverWrapper.manage().window().maximize();
64131
}
65132
}
66133
}
@@ -69,13 +136,13 @@ public void maximiseBrowserWindow() {
69136
* Method to tear down the driver object, can be overiden
70137
*/
71138
public void tearDownDriver() {
72-
webDriverWrapper.getWrappedDriver().quit();
139+
this.webDriverWrapper.getWrappedDriver().quit();
73140
}
74141

75142
/**
76143
* Reset the browser based on whether it's been reset before
77144
*/
78-
public boolean resetBrowser(boolean requiresReset) {
145+
public boolean resetBrowser(final boolean requiresReset) {
79146
if (requiresReset) {
80147
tearDownDriver();
81148
instantiate();

src/main/java/com/frameworkium/config/SystemProperty.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.frameworkium.config;
22

3-
import org.yaml.snakeyaml.Yaml;
4-
53
import java.util.Map;
64

5+
import org.yaml.snakeyaml.Yaml;
6+
77
public enum SystemProperty {
88

99
BROWSER("browser"),
@@ -29,31 +29,30 @@ public enum SystemProperty {
2929
JIRA_RESULT_FIELDNAME("jiraResultFieldName"),
3030
JIRA_RESULT_TRANSITION("jiraResultTransition"),
3131
MAXIMISE("maximise"),
32-
RESOLUTION("resolution");
32+
RESOLUTION("resolution"),
33+
PROXY("proxy");
3334

3435
private String value;
3536
private static Map configMap = null;
3637

37-
SystemProperty(String key) {
38+
SystemProperty(final String key) {
3839
this.value = retrieveValue(key);
3940
}
4041

4142
public String getValue() {
42-
return value;
43+
return this.value;
4344
}
4445

4546
public boolean isSpecified() {
46-
return null != value && !value.isEmpty();
47+
return null != this.value && !this.value.isEmpty();
4748
}
4849

49-
private String retrieveValue(String key) {
50+
private String retrieveValue(final String key) {
5051
if (System.getProperty(key) != null) {
5152
return System.getProperty(key);
5253
}
5354
if (System.getProperty("config") != null && configMap == null) {
54-
configMap = (Map) new Yaml().load(
55-
ClassLoader.getSystemResourceAsStream(System.getProperty("config"))
56-
);
55+
configMap = (Map) new Yaml().load(ClassLoader.getSystemResourceAsStream(System.getProperty("config")));
5756
}
5857
if (configMap != null) {
5958
Object configValue = configMap.get(key);

0 commit comments

Comments
 (0)