...12import org.openqa.selenium.firefox.FirefoxOptions;13import org.openqa.selenium.ie.InternetExplorerDriver;14import org.openqa.selenium.ie.InternetExplorerOptions;15import org.openqa.selenium.remote.RemoteWebDriver;16import org.openqa.selenium.support.ThreadGuard;17import org.openqa.selenium.support.events.EventFiringWebDriver;1819import com.tab.af.actions.ShadowActions;20import com.tab.af.actions.WebActions;21import com.tab.af.reporting.ReportLibrary;22import com.tab.af.testbase.TestBase;2324import io.github.bonigarcia.wdm.WebDriverManager;2526public class DriverEngine {2728 private EventFiringWebDriver webDriver;29 private WebActions webActions;30 private ReportLibrary reportLibrary;31 private ShadowActions shadowActions;32 private String browser;33 private String browserVersion;34 private WebDriverEvents webDriverEvents = WebDriverEvents.getInstance();3536 public String getBrowser() {37 return browser;38 }3940 public void setBrowser(String browser) {41 this.browser = browser;42 }4344 public String getBrowserVersion() {45 return browserVersion;46 }4748 public void setBrowserVersion(String browserVersion) {49 this.browserVersion = browserVersion;50 }5152 public DriverEngine() {53 initialize();54 }5556 public void initialize() {57 initalizeWebActions();58 initalizeReportLibrary();59 initalizeShadowActions();60 }6162 private void initalizeWebActions() {63 webActions = new WebActions(this);64 }6566 private void initalizeReportLibrary() {67 reportLibrary = new ReportLibrary(this);68 }6970 private void initalizeShadowActions() {71 shadowActions = new ShadowActions(this);72 }7374 public WebActions getWebActions() {75 return webActions;76 }7778 public ReportLibrary getReportLibrary() {79 return reportLibrary;80 }8182 public EventFiringWebDriver getWebDriver() {83 return webDriver;84 }8586 public ShadowActions getShadowActions() {87 return shadowActions;88 }8990 public void setWebDriver(EventFiringWebDriver webDriver) {91 this.webDriver = webDriver;92 }9394 public void setUpWebDriver(String browserType, Capabilities caps, boolean remoteExecution) throws Exception {95 selectDriverType(browserType, caps, remoteExecution);96 }9798 private final void selectDriverType(String browserType, Capabilities caps, boolean remoteExecution)99 throws Exception {100 if (remoteExecution)101 setUpRemoteWebDriver(browserType, caps);102 else103 setUpLocalWebDriver(browserType, caps);104105 }106107 private void setUpRemoteWebDriver(String browser, Capabilities caps) throws Exception {108 try {109110 webDriver = new EventFiringWebDriver(ThreadGuard.protect(createRemoteDriver(caps)));111112 if (Boolean.parseBoolean(TestBase.props.getProperty("Selenium.Event.log"))) {113 webDriver.register(webDriverEvents);114 }115 } catch (MalformedURLException e) {116 System.out.println("setUpRemoteWebDriver, The url provided was malformed.");117 118 throw e;119 } catch (SessionNotCreatedException e) {120 System.out.println("setUpRemoteWebDriver, The session could not be created.");121 throw e;122 } catch (WebDriverException e) {123 System.out.println(124 "setUpRemoteWebDriver, The webdriver created on one thread was accessed by another thread or the session could not be created");125 throw e;126127 }128 }129130 private RemoteWebDriver createRemoteDriver(Capabilities caps) throws Exception {131 RemoteWebDriver remoteWebDriver;132133 String hubURL = TestBase.props.getProperty("grid.url");134 switch ("grid") {135 case "grid":136 remoteWebDriver = new RemoteWebDriver(new URL(hubURL), caps);137 break;138 default:139 System.out.println("an attempt to create a remote web driver for grid is failed");140 remoteWebDriver = new RemoteWebDriver(new URL(hubURL), caps);141 }142 return remoteWebDriver;143 }144145 private void setUpLocalWebDriver(String browserType, Capabilities caps) throws Exception {146 EventFiringWebDriver eventDriver;147 switch (browserType.toUpperCase()) {148 case "CHROME":149150 eventDriver = setUpLocalChromeDriver(caps);151152 break;153 case "FIREFOX":154 eventDriver = setUpLocalFirefoxDriver(caps);155156 break;157 case "IE":158 eventDriver = setUpLocalIeDriver(caps);159160 break;161 default:162 throw new IllegalArgumentException(browserType + " is not supported. Please choose another browser.");163 }164 webDriver = eventDriver;165 // #############166 if (Boolean.parseBoolean(TestBase.props.getProperty("Selenium.Event.log"))) {167 webDriver.register(webDriverEvents);168 }169 setBrowser(caps.getBrowserName());170 setBrowserVersion(caps.getVersion());171 }172173 private EventFiringWebDriver setUpLocalChromeDriver(Capabilities caps) throws Exception {174 EventFiringWebDriver webDriver;175 ChromeOptions options = (ChromeOptions) caps;176177 WebDriverManager.chromedriver().version(TestBase.props.getProperty("chrome.binary.version")).setup();178179 if (caps == null) {180 webDriver = new EventFiringWebDriver(ThreadGuard.protect(new ChromeDriver()));181 } else {182 webDriver = new EventFiringWebDriver(ThreadGuard.protect(new ChromeDriver(options)));183184 }185186 return webDriver;187 }188189 private EventFiringWebDriver setUpLocalFirefoxDriver(Capabilities caps) throws Exception {190 EventFiringWebDriver webDriver;191 FirefoxOptions options = (FirefoxOptions) caps;192 WebDriverManager.firefoxdriver().version(TestBase.props.getProperty("firefox.binary.version")).setup();193194 if (caps == null) {195 webDriver = new EventFiringWebDriver(ThreadGuard.protect(new FirefoxDriver()));196 }197198 else {199 webDriver = new EventFiringWebDriver(ThreadGuard.protect(new FirefoxDriver(options)));200 }201202 return webDriver;203 }204205 private EventFiringWebDriver setUpLocalIeDriver(Capabilities caps) throws Exception {206 EventFiringWebDriver webDriver;207 InternetExplorerOptions options = (InternetExplorerOptions) caps;208 WebDriverManager.iedriver().version(TestBase.props.getProperty("ie.binary.version")).arch32().setup();209 if (caps == null) {210 webDriver = new EventFiringWebDriver(ThreadGuard.protect(new InternetExplorerDriver()));211 } else {212 webDriver = new EventFiringWebDriver(ThreadGuard.protect(new InternetExplorerDriver(options)));213 }214 return webDriver;215 }216217}
...