11package 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+
69import org .apache .logging .log4j .LogManager ;
710import org .apache .logging .log4j .Logger ;
11+ import org .openqa .selenium .Proxy ;
12+ import org .openqa .selenium .Proxy .ProxyType ;
813import org .openqa .selenium .WebDriver ;
14+ import org .openqa .selenium .remote .CapabilityType ;
915import 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
1421public 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 ();
0 commit comments