Skip to content

Commit

Permalink
Merge pull request #20 from robertgates55/Adding_Proxy_Support
Browse files Browse the repository at this point in the history
Added proxy support by adding a new system property
  • Loading branch information
jleeh committed Sep 1, 2015
2 parents 95eb32d + 5af0fb3 commit f4fce2f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 21 deletions.
89 changes: 78 additions & 11 deletions src/main/java/com/frameworkium/config/DriverType.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package com.frameworkium.config;

import com.frameworkium.capture.ScreenshotCapture;
import com.frameworkium.listeners.CaptureListener;
import com.frameworkium.listeners.EventListener;
import static com.frameworkium.config.DriverSetup.useRemoteDriver;
import static com.frameworkium.config.SystemProperty.APP_PATH;
import static com.frameworkium.config.SystemProperty.GRID_URL;
import static com.frameworkium.config.SystemProperty.MAXIMISE;
import static com.frameworkium.config.SystemProperty.PROXY;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.Proxy.ProxyType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import static com.frameworkium.config.DriverSetup.useRemoteDriver;
import static com.frameworkium.config.SystemProperty.*;
import com.frameworkium.capture.ScreenshotCapture;
import com.frameworkium.listeners.CaptureListener;
import com.frameworkium.listeners.EventListener;

public abstract class DriverType {

protected WebDriverWrapper webDriverWrapper;

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

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

DesiredCapabilities caps = getDesiredCapabilities();

Proxy currentProxy = getProxy();
if (currentProxy != null) {
caps.setCapability(CapabilityType.PROXY, currentProxy);
}

logger.info("Caps: " + caps.toString());

WebDriverWrapper eventFiringWD = new WebDriverWrapper(getWebDriverObject(caps));
eventFiringWD.register(new EventListener());
if (ScreenshotCapture.isRequired()) {
eventFiringWD.register(new CaptureListener());
}
webDriverWrapper = eventFiringWD;
this.webDriverWrapper = eventFiringWD;
}

/**
* This method returns a proxy object with settings set by the system properties. If no valid proxy argument is set
* then it returns null.
*
* @return A Selenium proxy object for the current system properties or null if no valid proxy settings
*/
public Proxy getProxy() {
if (PROXY.isSpecified()) {
Proxy proxy = new Proxy();
String proxyString = PROXY.getValue().toLowerCase();
switch (proxyString) {

case "system":
proxy.setProxyType(ProxyType.SYSTEM);
logger.info("Using system proxy");
break;
case "autodetect":
proxy.setProxyType(ProxyType.AUTODETECT);
logger.info("Using autodetected proxy");
break;
case "direct":
proxy.setProxyType(ProxyType.DIRECT);
logger.info("Using direct (no) proxy");
break;
default:
proxy.setProxyType(ProxyType.MANUAL);
if (verifyProxyAddress(proxyString)) {
proxy.setHttpProxy(proxyString).setFtpProxy(proxyString).setSslProxy(proxyString);
String logMessage = String
.format("Set all protocols to use proxy with address %s", proxyString);
logger.info(logMessage);
} else {
logger.error("Invalid proxy setting specified, acceptable values are: system, autodetect, direct or {hostname}:{port}. Tests will now use default setting for your browser");
return null;
}
break;
}
return proxy;
}
return null;
}

/**
* This helper method verifies that a value is suitable for usage as a proxy address. Selenium expects values of the
* format hostname:port or ip:port
*
* @param proxyAddress The proxy value to verify
* @return true if value is acceptable as a proxy, false otherwise
*/
private boolean verifyProxyAddress(final String proxyAddress) {
return proxyAddress.matches(HOSTNAME_OR_IP_AND_PORT_REGEX);
}

/**
Expand All @@ -42,7 +109,7 @@ public void instantiate() {
* @return - Initialised WebDriverWrapper
*/
public WebDriverWrapper getDriver() {
return webDriverWrapper;
return this.webDriverWrapper;
}

/**
Expand All @@ -59,8 +126,8 @@ public static boolean isMobile() {
*/
public void maximiseBrowserWindow() {
if (!MAXIMISE.isSpecified() || Boolean.parseBoolean(MAXIMISE.getValue())) {
if((!useRemoteDriver() && !isNative()) || GRID_URL.isSpecified()) {
webDriverWrapper.manage().window().maximize();
if (!useRemoteDriver() && !isNative() || GRID_URL.isSpecified()) {
this.webDriverWrapper.manage().window().maximize();
}
}
}
Expand All @@ -69,13 +136,13 @@ public void maximiseBrowserWindow() {
* Method to tear down the driver object, can be overiden
*/
public void tearDownDriver() {
webDriverWrapper.getWrappedDriver().quit();
this.webDriverWrapper.getWrappedDriver().quit();
}

/**
* Reset the browser based on whether it's been reset before
*/
public boolean resetBrowser(boolean requiresReset) {
public boolean resetBrowser(final boolean requiresReset) {
if (requiresReset) {
tearDownDriver();
instantiate();
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/com/frameworkium/config/SystemProperty.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.frameworkium.config;

import org.yaml.snakeyaml.Yaml;

import java.util.Map;

import org.yaml.snakeyaml.Yaml;

public enum SystemProperty {

BROWSER("browser"),
Expand All @@ -29,31 +29,30 @@ public enum SystemProperty {
JIRA_RESULT_FIELDNAME("jiraResultFieldName"),
JIRA_RESULT_TRANSITION("jiraResultTransition"),
MAXIMISE("maximise"),
RESOLUTION("resolution");
RESOLUTION("resolution"),
PROXY("proxy");

private String value;
private static Map configMap = null;

SystemProperty(String key) {
SystemProperty(final String key) {
this.value = retrieveValue(key);
}

public String getValue() {
return value;
return this.value;
}

public boolean isSpecified() {
return null != value && !value.isEmpty();
return null != this.value && !this.value.isEmpty();
}

private String retrieveValue(String key) {
private String retrieveValue(final String key) {
if (System.getProperty(key) != null) {
return System.getProperty(key);
}
if (System.getProperty("config") != null && configMap == null) {
configMap = (Map) new Yaml().load(
ClassLoader.getSystemResourceAsStream(System.getProperty("config"))
);
configMap = (Map) new Yaml().load(ClassLoader.getSystemResourceAsStream(System.getProperty("config")));
}
if (configMap != null) {
Object configValue = configMap.get(key);
Expand Down

0 comments on commit f4fce2f

Please sign in to comment.