1package com.gslab.unicorn.driver.web;2import com.gslab.unicorn.enums.BROWSER;3import com.gslab.unicorn.exceptions.UnicornIOException;4import com.gslab.unicorn.selenium.*;5import com.gslab.unicorn.utils.CommonUtils;6import com.gslab.unicorn.utils.Config;7import io.github.bonigarcia.wdm.DriverManagerType;8import org.openqa.selenium.Alert;9import org.openqa.selenium.Proxy;10import org.openqa.selenium.WebDriver;11import org.openqa.selenium.chrome.ChromeDriver;12import org.openqa.selenium.chrome.ChromeOptions;13import org.openqa.selenium.firefox.FirefoxDriver;14import org.openqa.selenium.firefox.FirefoxDriverLogLevel;15import org.openqa.selenium.firefox.FirefoxOptions;16import org.openqa.selenium.firefox.FirefoxProfile;17import org.openqa.selenium.firefox.internal.ProfilesIni;18import org.openqa.selenium.ie.InternetExplorerDriver;19import org.openqa.selenium.ie.InternetExplorerOptions;20import org.openqa.selenium.remote.CapabilityType;21import org.openqa.selenium.safari.SafariDriver;22import org.openqa.selenium.safari.SafariOptions;23import org.openqa.selenium.support.ui.ExpectedConditions;24import org.openqa.selenium.support.ui.WebDriverWait;25import java.io.File;26import java.io.IOException;27import java.net.HttpURLConnection;28import java.net.URL;29import java.util.concurrent.TimeUnit;30/**31 * @author vivek_Lande32 * ©Copyright 2018 Great Software Laboratory. All rights reserved33 * <p>34 * <p>35 * This class intent to initialize WebDriver as OS type and browser type.36 * Created by Vivek_Lande37 */38public class WebDriverManager implements WebDriverManagerInterface {39 private static org.openqa.selenium.WebDriver driver;40 private String browserType = null;41 public SeleniumCheckboxInterface seleniumCheckbox;42 public SeleniumDialogInterface seleniumDialog;43 public SeleniumDropdownInterface seleniumDropdown;44 public SeleniumElementsInterface seleniumElements;45 public SeleniumPage seleniumPage;46 public SeleniumRadioInterface seleniumRadio;47 public SeleniumTextboxInterface seleniumTextbox;48 public SeleniumWindowInterface seleniumWindow;49 public static org.openqa.selenium.WebDriver getDriver() {50 return driver;51 }52 private static Proxy createZapProxyConfigurationForWebDriver() {53 Proxy proxy = new Proxy();54 proxy.setHttpProxy(Config.getZapProxy());55 proxy.setSslProxy(Config.getZapProxy());56 return proxy;57 }58 /**59 * This will return the WebDriver instance.60 *61 * @param browser browser type type of browser62 * @return WebDriver63 * @throws Exception64 */65 @Override66 public WebDriver initWebDriver(BROWSER browser) throws Exception {67 ClassLoader classLoader = getClass().getClassLoader();68 String driverPath = classLoader.getResource("drivers").getPath() + File.separator + getDriverPath(browser);69 switch (browser) {70 case FIREFOX:71 setBrowserType("FIREFOX");72 // System.setProperty("webdriver.gecko.driver", driverPath);73 FirefoxProfile profile = new FirefoxProfile();74 profile.setPreference("browser.download.folderList", 2);75 profile.setPreference("browser.download.useDownloadDir", true);76 profile.setPreference("browser.download.dir", "/home/unicorn"); // Download location77 profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/zip,application/exp");78 profile.setPreference("browser.helperApps.alwaysAsk.force", false);79 profile.setPreference("browser.download.manager.showWhenStarting", false);80 FirefoxOptions firefoxOptions = new FirefoxOptions();81 firefoxOptions.setAcceptInsecureCerts(true);82 firefoxOptions.setProfile(profile);83 firefoxOptions.setCapability("marionette", true);84 firefoxOptions.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);85 firefoxOptions.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);86 firefoxOptions.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);87 firefoxOptions.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);88 firefoxOptions.setLogLevel(FirefoxDriverLogLevel.TRACE);89 if (Config.isToPerformPenetrationTesting()) {90 firefoxOptions.setProxy(createZapProxyConfigurationForWebDriver());91 }92 io.github.bonigarcia.wdm.WebDriverManager.getInstance(DriverManagerType.FIREFOX).setup();93 driver = new FirefoxDriver(firefoxOptions);94 break;95 case CHROME:96 setBrowserType("CHROME");97 // System.setProperty("webdriver.chrome.driver", driverPath);98 ChromeOptions chromeOptions = new ChromeOptions();99 chromeOptions.setAcceptInsecureCerts(true);100 chromeOptions.setCapability("marionette", true);101 chromeOptions.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);102 chromeOptions.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);103 chromeOptions.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);104 chromeOptions.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);105 if (Config.isToPerformPenetrationTesting()) {106 chromeOptions.setProxy(createZapProxyConfigurationForWebDriver());107 }108 io.github.bonigarcia.wdm.WebDriverManager.getInstance(DriverManagerType.CHROME).setup();109 driver = new ChromeDriver(chromeOptions);110 break;111 case IE:112 setBrowserType("IE");113 //System.setProperty("webdriver.ie.driver", driverPath);114 InternetExplorerOptions internetExplorerOptions = new InternetExplorerOptions();115 internetExplorerOptions.setCapability("marionette", true);116 internetExplorerOptions.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);117 internetExplorerOptions.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);118 internetExplorerOptions.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);119 internetExplorerOptions.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);120 if (Config.isToPerformPenetrationTesting()) {121 internetExplorerOptions.setProxy(createZapProxyConfigurationForWebDriver());122 }123 io.github.bonigarcia.wdm.WebDriverManager.getInstance(DriverManagerType.IEXPLORER).setup();124 driver = new InternetExplorerDriver(internetExplorerOptions);125 break;126 case SAFARI:127 setBrowserType("SAFARI");128 SafariOptions safariOptions = new SafariOptions();129 safariOptions.setCapability("marionette", true);130 safariOptions.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);131 safariOptions.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true);132 safariOptions.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);133 safariOptions.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);134 if (Config.isToPerformPenetrationTesting()) {135 safariOptions.setProxy(createZapProxyConfigurationForWebDriver());136 }137 //WebDriverManager.getInstance(DriverManagerType);138 driver = new SafariDriver(safariOptions);139 break;140 }141 initializeAllUtilityClasses();142 return driver;143 }144 /**145 * Maximize the opened browser window146 */147 @Override148 public void maximizeWindow() {149 getDriver().manage().window().maximize();150 }151 /**152 * Open url in the browser window153 *154 * @param url the url155 */156 @Override157 public void openUrl(String url) {158 getDriver().get(url);159 }160 @Override161 public void quit() throws Exception {162 try {163 if (getBrowserType().equalsIgnoreCase("ie")) {164 Runtime.getRuntime().exec("taskkill /f /fi \"pid gt 0\" /im IEDriverServer.exe");165 Runtime.getRuntime().exec("taskkill /f /fi \"pid gt 0\" /im iexplore.exe");166 }167 } catch (IOException e) {168 throw new UnicornIOException("failed to quite IE instance!", e);169 }170 getDriver().quit();171 }172 /**173 * Refresh the current browser page174 */175 public void refreshPage() {176 getDriver().navigate().refresh();177 }178 /**179 * Close the current browser window180 *181 * @throws Exception182 */183 public void close() throws Exception {184 try {185 if (getBrowserType().equalsIgnoreCase("ie")) {186 Runtime.getRuntime().exec("taskkill /f /fi \"pid gt 0\" /im IEDriverServer.exe");187 Runtime.getRuntime().exec("taskkill /f /fi \"pid gt 0\" /im iexplore.exe");188 }189 } catch (IOException e) {190 throw new UnicornIOException("failed to close IE instance!", e);191 }192 getDriver().close();193 }194 /**195 * Return the type of browser has current webdriver instance196 *197 * @return browser type198 */199 public String getBrowserType() {200 return browserType;201 }202 private void setBrowserType(String browserType) {203 this.browserType = browserType;204 }205 /**206 * Accept the alert popup207 *208 * @return message having on the alert209 */210 public String acceptAlert() {211 Alert alert = driver.switchTo().alert();212 String alertText = alert.getText();213 alert.accept();214 return alertText;215 }216 /**217 * delete all cookies of the session218 */219 public void deleteAllCookies() {220 driver.manage().deleteAllCookies();221 }222 /**223 * This will switch to the alert window224 *225 * @return Alert instance226 */227 public Alert switchToAlert() {228 return driver.switchTo().alert();229 }230 /**231 * This method returns driver location as per os type detected232 *233 * @return os type234 */235 private String getDriverPath(BROWSER browserType) throws Exception {236 /* OSInfo.OSType*/237 String osType = CommonUtils.getOSType();238 String directoryName = null;239 switch (osType) {240 case /*WINDOWS*/ "windows":241 switch (browserType) {242 case FIREFOX:243 directoryName = "geckodriver-v0.20.0-win32" + File.separator + "geckodriver.exe";244 break;245 case CHROME:246 directoryName = "chromedriver_win32" + File.separator + "chromedriver.exe";247 break;248 case IE:249 directoryName = "IEDriverServer_Win32_3.11.0" + File.separator + "IEDriverServer.exe";250 break;251 case SAFARI:252 directoryName = "" + File.separator + "";253 }254 break;255 case /*MACOSX*/ "mac":256 switch (browserType) {257 case FIREFOX: // not required as of now. by default supported258 break;259 case CHROME:260 directoryName = "chromedriver_mac64" + File.separator + "chromedriver";261 break;262 case IE:263 // not applicable264 break;265 }266 break;267 case /*LINUX*/ "linux":268 switch (browserType) {269 case FIREFOX:270 directoryName = "geckodriver-v0.20.0-linux32" + File.separator + "geckodriver";271 break;272 case CHROME:273 directoryName = "chromedriver_linux32" + File.separator + "chromedriver";274 break;275 case IE:276 // not applicable277 break;278 }279 break;280 default:281 break;282 }283 return directoryName;284 }285 private void initializeAllUtilityClasses() {286 seleniumCheckbox = new SeleniumCheckbox();287 seleniumDialog = new SeleniumDialog();288 seleniumDropdown = new SeleniumDropdown();289 seleniumElements = new SeleniumElements();290 seleniumPage = new SeleniumPage();291 seleniumRadio = new SeleniumRadio();292 seleniumTextbox = new SeleniumTextbox();293 seleniumWindow = new SeleniumWindow();294 }295}...