|
1 | 1 | package com.frameworkium.config; |
2 | 2 |
|
3 | | -import org.openqa.selenium.WebDriver; |
| 3 | +import com.frameworkium.config.drivers.*; |
| 4 | +import com.frameworkium.config.remotes.BrowserStack; |
| 5 | +import com.frameworkium.config.remotes.Sauce; |
| 6 | +import org.apache.logging.log4j.LogManager; |
| 7 | +import org.apache.logging.log4j.Logger; |
4 | 8 | import org.openqa.selenium.remote.DesiredCapabilities; |
5 | 9 |
|
6 | | -public interface DriverSetup { |
| 10 | +import static com.frameworkium.config.SystemProperty.*; |
7 | 11 |
|
8 | | - WebDriver getWebDriverObject(DesiredCapabilities desiredCapabilities); |
| 12 | +public class DriverSetup { |
9 | 13 |
|
10 | | - DesiredCapabilities getDesiredCapabilities(); |
| 14 | + private static final SupportedBrowsers DEFAULT_BROWSER = SupportedBrowsers.FIREFOX; |
| 15 | + |
| 16 | + /** |
| 17 | + * List of supported drivers |
| 18 | + */ |
| 19 | + private enum SupportedBrowsers { |
| 20 | + FIREFOX,CHROME,OPERA,IE,PHANTOMJS,SAFARI |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * List of supported remote grids |
| 25 | + */ |
| 26 | + private enum SupportedRemotes { |
| 27 | + SAUCE,BROWSERSTACK,GRID |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * List of supported platforms on remote grids |
| 32 | + */ |
| 33 | + public enum SupportedPlatforms { |
| 34 | + WINDOWS,OSX,IOS,ANDROID,NONE |
| 35 | + } |
| 36 | + |
| 37 | + protected final static Logger logger = LogManager.getLogger(DriverType.class); |
| 38 | + |
| 39 | + /** |
| 40 | + * Returns the driver type to the base test, which initialises it |
| 41 | + * |
| 42 | + * @return - Driver Type |
| 43 | + */ |
| 44 | + public static DriverType returnDesiredDriverType() { |
| 45 | + return initialiseDesiredDriverType(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Uses the paramaters given to determine which browser/remote/platform to use |
| 50 | + * |
| 51 | + * @return - The correct driver type based on parameters |
| 52 | + */ |
| 53 | + private static DriverType initialiseDesiredDriverType() throws NullPointerException { |
| 54 | + DriverType browserDriver = returnBrowserObject(); |
| 55 | + DesiredCapabilities browserDesiredCapabilities = browserDriver.getDesiredCapabilities(); |
| 56 | + if (useRemoteDriver()) { |
| 57 | + SupportedPlatforms platform = returnPlatformType(); |
| 58 | + switch(returnRemoteType()) { |
| 59 | + case SAUCE: |
| 60 | + return new SauceImpl(platform, browserDesiredCapabilities); |
| 61 | + case BROWSERSTACK: |
| 62 | + return new BrowserStackImpl(platform, browserDesiredCapabilities); |
| 63 | + case GRID: |
| 64 | + return new GridImpl(browserDesiredCapabilities); |
| 65 | + } |
| 66 | + } |
| 67 | + return browserDriver; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @return the browser driver type object based on the browser passed in |
| 72 | + */ |
| 73 | + private static DriverType returnBrowserObject() { |
| 74 | + switch (returnBrowserType()) { |
| 75 | + case FIREFOX: |
| 76 | + return new FirefoxImpl(); |
| 77 | + case CHROME: |
| 78 | + return new ChromeImpl(); |
| 79 | + case OPERA: |
| 80 | + return new OperaImpl(); |
| 81 | + case IE: |
| 82 | + return new InternetExporerImpl(); |
| 83 | + case PHANTOMJS: |
| 84 | + return new PhantomJSImpl(); |
| 85 | + case SAFARI: |
| 86 | + return new SafariImpl(); |
| 87 | + } |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Checks whether a remote driver is wanting to be used |
| 93 | + * |
| 94 | + * @return - True/False to whether to use a remote driver |
| 95 | + */ |
| 96 | + public static boolean useRemoteDriver() { |
| 97 | + return GRID_URL.isSpecified() || Sauce.isDesired() || BrowserStack.isDesired(); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Returns the platform type, if it's been given |
| 102 | + * |
| 103 | + * @return - Platform type |
| 104 | + */ |
| 105 | + private static SupportedPlatforms returnPlatformType() { |
| 106 | + if (SystemProperty.PLATFORM.isSpecified()) { |
| 107 | + return SupportedPlatforms.valueOf(SystemProperty.PLATFORM.getValue().toUpperCase()); |
| 108 | + } |
| 109 | + else { |
| 110 | + return SupportedPlatforms.NONE; |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Returns the browser type and returns default if not specified |
| 116 | + * |
| 117 | + * @return - Browser Type |
| 118 | + */ |
| 119 | + private static SupportedBrowsers returnBrowserType() { |
| 120 | + if (!SystemProperty.BROWSER.isSpecified()) { |
| 121 | + return DEFAULT_BROWSER; |
| 122 | + } |
| 123 | + else { |
| 124 | + return SupportedBrowsers.valueOf(SystemProperty.BROWSER.getValue().toUpperCase()); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Returns what type of remote driver has been specified to be used |
| 130 | + * |
| 131 | + * @return - Remote Driver Type |
| 132 | + */ |
| 133 | + private static SupportedRemotes returnRemoteType() { |
| 134 | + if (Sauce.isDesired()) { |
| 135 | + return SupportedRemotes.SAUCE; |
| 136 | + } |
| 137 | + else if (BrowserStack.isDesired()) { |
| 138 | + return SupportedRemotes.BROWSERSTACK; |
| 139 | + } |
| 140 | + else { |
| 141 | + return SupportedRemotes.GRID; |
| 142 | + } |
| 143 | + } |
11 | 144 | } |
0 commit comments