1package com.taf.automation.ui.support;2import com.taf.automation.ui.support.util.Utils;3import io.appium.java_client.AppiumDriver;4import io.appium.java_client.android.AndroidDriver;5import io.appium.java_client.ios.IOSDriver;6import org.apache.commons.lang3.StringUtils;7import org.openqa.selenium.Capabilities;8import org.openqa.selenium.Platform;9import org.openqa.selenium.Proxy;10import org.openqa.selenium.UnexpectedAlertBehaviour;11import org.openqa.selenium.WebDriver;12import org.openqa.selenium.chrome.ChromeDriver;13import org.openqa.selenium.chrome.ChromeOptions;14import org.openqa.selenium.firefox.FirefoxDriver;15import org.openqa.selenium.firefox.FirefoxDriverLogLevel;16import org.openqa.selenium.firefox.FirefoxOptions;17import org.openqa.selenium.firefox.FirefoxProfile;18import org.openqa.selenium.ie.InternetExplorerDriver;19import org.openqa.selenium.opera.OperaDriver;20import org.openqa.selenium.remote.BrowserType;21import org.openqa.selenium.remote.CapabilityType;22import org.openqa.selenium.remote.DesiredCapabilities;23import org.openqa.selenium.remote.RemoteWebDriver;24import org.openqa.selenium.safari.SafariDriver;25import org.openqa.selenium.safari.SafariOptions;26import java.lang.reflect.Constructor;27import java.net.MalformedURLException;28import java.net.URL;29public enum WebDriverTypeEnum {30 CHROME(BrowserType.CHROME, ChromeDriver.class),31 EDGE_LEGACY(BrowserType.EDGE, org.openqa.selenium.edge.EdgeDriver.class),32 EDGE(BrowserType.EDGE, com.microsoft.edge.seleniumtools.EdgeDriver.class),33 FIREFOX(BrowserType.FIREFOX, FirefoxDriver.class),34 IE(BrowserType.IE, InternetExplorerDriver.class),35 OPERA_BLINK(BrowserType.OPERA_BLINK, OperaDriver.class),36 SAFARI(BrowserType.SAFARI, SafariDriver.class),37 ANDROID(BrowserType.ANDROID, AndroidDriver.class),38 IPHONE(BrowserType.IPHONE, IOSDriver.class),39 IPAD(BrowserType.IPAD, IOSDriver.class),40 ;41 String driverName;42 Class<? extends WebDriver> driverClass;43 WebDriverTypeEnum(String driverName, Class<? extends WebDriver> driverClass) {44 this.driverClass = driverClass;45 this.driverName = driverName;46 }47 public String getDriverName() {48 return driverName;49 }50 public WebDriver getNewWebDriver() {51 TestProperties prop = Utils.deepCopy(TestProperties.getInstance());52 Utils.writeField(prop, "browserType", this);53 return getNewWebDriver(prop);54 }55 /**56 * Get a New Web Driver based on the passed Test Properties57 *58 * @param prop - Test Properties to be used to get the new WebDriver59 * @return WebDriver60 */61 public WebDriver getNewWebDriver(TestProperties prop) {62 return getNewWebDriver(prop, null);63 }64 /**65 * Get a New Web Driver based on the passed Test Properties & Desired Capabilities<BR>66 * <B>Notes: </B>67 * <OL>68 * <LI>The extra Desired Capabilities is applied first. The Test Properties Extra Capabilities is applied after</LI>69 * </OL>70 *71 * @param prop - Test Properties to be used to get the new WebDriver72 * @param extra - The extra Desired Capabilities (not applied to remote execution)73 * @return WebDriver74 */75 @SuppressWarnings("squid:S00112")76 public WebDriver getNewWebDriver(TestProperties prop, DesiredCapabilities extra) {77 DesiredCapabilities capabilities = new DesiredCapabilities();78 Proxy proxy = null;79 if (prop.getHttpProxy() != null) {80 proxy = new Proxy();81 proxy.setHttpProxy(prop.getHttpProxy());82 }83 if (prop.getHttpsProxy() != null) {84 if (proxy == null) {85 proxy = new Proxy();86 }87 proxy.setSslProxy(prop.getHttpsProxy());88 }89 if (proxy == null && prop.isBrowserMobProxy()) {90 prop.startBrowserMobProxyForThread();91 proxy = prop.getSeleniumProxyForThread();92 }93 if (proxy != null) {94 capabilities.setCapability(CapabilityType.PROXY, proxy);95 }96 capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);97 if (prop.getRemoteURL() != null && !prop.getBrowserType().isAppiumDriver()) {98 try {99 return new RemoteWebDriver(new URL(prop.getRemoteURL()), getRemoteCapabilities(prop, capabilities));100 } catch (MalformedURLException e) {101 throw new RuntimeException("Malformed URL!", e);102 }103 }104 // Note: Null desired capabilities will just skip105 capabilities.merge(extra);106 switch (prop.getBrowserType()) {107 case FIREFOX:108 return getFirefoxDriver(prop, capabilities);109 case CHROME:110 return getChromeDriver(prop, capabilities);111 case SAFARI:112 return getSafariDriver(prop, capabilities);113 case EDGE_LEGACY:114 return getEdgeLegacyDriver(prop, capabilities);115 case EDGE:116 return getEdgeChromiumDriver(prop, capabilities);117 case ANDROID:118 return getAndroidDriver(prop, capabilities);119 case IPAD:120 case IPHONE:121 return getIOSDriver(prop, capabilities);122 default:123 try {124 Constructor<? extends WebDriver> constructor = driverClass.getConstructor(Capabilities.class);125 return constructor.newInstance(capabilities);126 } catch (Exception e) {127 String message = "Failed to instantiate WebDriver: " + driverClass.getName() + "; Error: " + e.getMessage();128 throw new RuntimeException(message, e);129 }130 }131 }132 private DesiredCapabilities getRemoteCapabilities(TestProperties prop, DesiredCapabilities mergeCapabilities) {133 Platform platform = prop.getBrowserPlatform();134 if (platform == null) {135 platform = Platform.ANY;136 }137 DesiredCapabilities capabilities = new DesiredCapabilities();138 capabilities.setBrowserName(prop.getBrowserType().getDriverName());139 capabilities.setVersion(prop.getBrowserVersion());140 capabilities.setPlatform(platform);141 capabilities.merge(prop.getExtraCapabilities());142 if (prop.getBrowserType() == FIREFOX) {143 capabilities.setCapability(FirefoxOptions.FIREFOX_OPTIONS, getFirefoxOptions(prop, mergeCapabilities, false));144 } else if (prop.getBrowserType() == CHROME) {145 capabilities.setCapability(ChromeOptions.CAPABILITY, getChromeOptions(prop, mergeCapabilities));146 } else if (prop.getBrowserType() == EDGE) {147 capabilities.setCapability(com.microsoft.edge.seleniumtools.EdgeOptions.CAPABILITY, getEdgeChromiumOptions(prop, mergeCapabilities));148 } else if (prop.getBrowserType() == SAFARI) {149 capabilities.setCapability(SafariOptions.CAPABILITY, getSafariOptions(prop, mergeCapabilities));150 } else {151 capabilities.merge(mergeCapabilities);152 }153 return capabilities;154 }155 private FirefoxDriver getFirefoxDriver(TestProperties prop, DesiredCapabilities mergeCapabilities) {156 FirefoxOptions firefoxOptions = getFirefoxOptions(prop, mergeCapabilities, true);157 return new FirefoxDriver(firefoxOptions);158 }159 private FirefoxOptions getFirefoxOptions(TestProperties prop, DesiredCapabilities mergeCapabilities, boolean local) {160 FirefoxProfile profile = new FirefoxProfile();161 profile.setPreference("focusmanager.testmode", true);162 profile.setPreference("dom.max_chrome_script_run_time", 0);163 profile.setPreference("dom.max_script_run_time", 0);164 // Handle SSO on specified sites165 String uris = prop.getFirefoxNTLM_URIS();166 if (StringUtils.isNotBlank(uris)) {167 profile.setPreference("network.automatic-ntlm-auth.trusted-uris", uris);168 }169 profile.setAcceptUntrustedCertificates(true);170 profile.setAssumeUntrustedCertificateIssuer(false);171 if (prop.getUserAgent() != null) {172 profile.setPreference("general.useragent.override", prop.getUserAgent());173 }174 if (local) {175 setFirefoxLocalProperties(prop);176 }177 FirefoxOptions firefoxOptions = new FirefoxOptions();178 firefoxOptions.merge(mergeCapabilities);179 firefoxOptions.setProfile(profile);180 firefoxOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.IGNORE);181 firefoxOptions.setAcceptInsecureCerts(true);182 firefoxOptions.setLogLevel(FirefoxDriverLogLevel.WARN);183 firefoxOptions.addArguments("--log fatal");184 return firefoxOptions;185 }186 private void setFirefoxLocalProperties(TestProperties prop) {187 if (prop.getFirefoxBin() != null) {188 System.setProperty("webdriver.firefox.bin", prop.getFirefoxBin());189 }190 if (prop.noMarionette()) {191 System.setProperty("webdriver.firefox.marionette", "false");192 }193 System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "/dev/null");194 }195 private ChromeDriver getChromeDriver(TestProperties prop, DesiredCapabilities mergeCapabilities) {196 ChromeOptions chromeOptions = getChromeOptions(prop, mergeCapabilities);197 return new ChromeDriver(chromeOptions);198 }199 private ChromeOptions getChromeOptions(TestProperties prop, DesiredCapabilities mergeCapabilities) {200 ChromeOptions chromeOptions = new ChromeOptions();201 chromeOptions.addArguments("--ignore-certificate-errors");202 chromeOptions.addArguments("--disable-gpu");203 chromeOptions.addArguments("--dns-prefetch-disable");204 if (prop.getUserAgent() != null) {205 chromeOptions.addArguments("user-agent=" + prop.getUserAgent());206 }207 chromeOptions.merge(mergeCapabilities);208 chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.IGNORE);209 return chromeOptions;210 }211 private SafariDriver getSafariDriver(TestProperties prop, DesiredCapabilities mergeCapabilities) {212 SafariOptions options = getSafariOptions(prop, mergeCapabilities);213 return new SafariDriver(options);214 }215 @SuppressWarnings("squid:S1172")216 private SafariOptions getSafariOptions(TestProperties prop, DesiredCapabilities mergeCapabilities) {217 SafariOptions options = new SafariOptions();218 options.merge(mergeCapabilities);219 return options;220 }221 private org.openqa.selenium.edge.EdgeDriver getEdgeLegacyDriver(TestProperties prop, DesiredCapabilities mergeCapabilities) {222 org.openqa.selenium.edge.EdgeOptions options = getEdgeLegacyOptions(prop, mergeCapabilities);223 return new org.openqa.selenium.edge.EdgeDriver(options);224 }225 @SuppressWarnings("squid:S1172")226 private org.openqa.selenium.edge.EdgeOptions getEdgeLegacyOptions(TestProperties prop, DesiredCapabilities mergeCapabilities) {227 org.openqa.selenium.edge.EdgeOptions options = new org.openqa.selenium.edge.EdgeOptions();228 options.merge(mergeCapabilities);229 return options;230 }231 private com.microsoft.edge.seleniumtools.EdgeDriver getEdgeChromiumDriver(TestProperties prop, DesiredCapabilities mergeCapabilities) {232 com.microsoft.edge.seleniumtools.EdgeOptions options = getEdgeChromiumOptions(prop, mergeCapabilities);233 return new com.microsoft.edge.seleniumtools.EdgeDriver(options);234 }235 private com.microsoft.edge.seleniumtools.EdgeOptions getEdgeChromiumOptions(TestProperties prop, DesiredCapabilities mergeCapabilities) {236 com.microsoft.edge.seleniumtools.EdgeOptions options = new com.microsoft.edge.seleniumtools.EdgeOptions();237 options.addArguments("--ignore-certificate-errors");238 options.addArguments("--disable-gpu");239 options.addArguments("--dns-prefetch-disable");240 if (prop.getUserAgent() != null) {241 options.addArguments("user-agent=" + prop.getUserAgent());242 }243 options.merge(mergeCapabilities);244 options.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.IGNORE);245 return options;246 }247 private AndroidDriver getAndroidDriver(TestProperties prop, DesiredCapabilities capabilities) {248 capabilities.setBrowserName(null);249 capabilities.setPlatform(null);250 capabilities.setCapability(CapabilityType.PLATFORM_NAME, Platform.ANDROID);251 capabilities.merge(prop.getExtraCapabilities());252 return new AndroidDriver(getRemoteURL(prop), capabilities);253 }254 private IOSDriver getIOSDriver(TestProperties prop, DesiredCapabilities capabilities) {255 capabilities.setBrowserName(null);256 capabilities.setPlatform(null);257 capabilities.setCapability(CapabilityType.PLATFORM_NAME, Platform.IOS);258 capabilities.merge(prop.getExtraCapabilities());259 return new IOSDriver(getRemoteURL(prop), capabilities);260 }261 @SuppressWarnings("squid:S00112")262 private URL getRemoteURL(TestProperties prop) {263 try {264 return new URL(prop.getRemoteURL());265 } catch (Exception ex) {266 String message = "webdriver.remote.url property had invalid URL: " + prop.getRemoteURL();267 throw new RuntimeException(message, ex);268 }269 }270 public boolean isAppiumDriver() {271 return AppiumDriver.class.isAssignableFrom(driverClass);272 }273}...