/** * Created by zhangminjie on 15/3/16. */ public enum WebDriverManager { DRIVER; Logger log= LogManager.getLogger(WebDriverManager.class.getName()); private WebDriver Driver=null; private enum Configure{CONFIGPATH{ @Override public String toString() { return System.getProperty("user.dir") + "/src/main/resources/conf/config.xml"; } } } public enum BrowserType{CHROME,FIREFOX,IE,INTERNETEXPLORER,REMOTE_CHROME,REMOTE_FIREFOX,REMOTE_IE,REMOTE_INTERNETEXPLORER} public void setDriver(BrowserType browserType,URL url) { if(null==this.Driver){ switch (browserType) { case CHROME: this.Driver = new BrowserChromeImpl().getDriver(); break; case IE: case INTERNETEXPLORER: this.Driver = new BrowserIEImpl().getDriver(); break; case FIREFOX: this.Driver = new BrowserFirefoxImpl().getDriver(); break; case REMOTE_CHROME: this.Driver = new BrowserChromeImpl().getDriver(url); break; case REMOTE_FIREFOX: this.Driver = new BrowserFirefoxImpl().getDriver(url); break; case REMOTE_IE: case REMOTE_INTERNETEXPLORER: this.Driver = new BrowserIEImpl().getDriver(url); break; } } } public void setDriver() { Map<String,String> map=getBrowserType(); String browserType=map.get("browserType"); String url=map.get("url"); try { if (null == url) { setDriver(BrowserType.valueOf(browserType.toUpperCase()), null); } else { setDriver(BrowserType.valueOf(browserType.toUpperCase()), new URL(url)); } } catch (MalformedURLException e) { log.error("url错误", e); } catch (IllegalArgumentException e) { log.error("指定的浏览器类型不支持", e); } catch (NullPointerException e) { log.error("配置文件中,浏览器类型为空",e); } } public void quitDriver() { if (null != this.Driver) { this.Driver = null; } } public WebDriver getDriver() { return this.Driver; } private Map<String,String> getBrowserType() { Map<String, String> map = new HashMap<>(); try { XMLConfiguration xmlConfiguration = new XMLConfiguration(Configure.CONFIGPATH.toString()); xmlConfiguration.setExpressionEngine(new XPathExpressionEngine()); String browserType= xmlConfiguration.getString("//browser/@type"); map.put("browserType", browserType); String url= xmlConfiguration.getString("//browser/@url"); map.put("url", url); return map; } catch (ConfigurationException e) { log.error("没有发现配置文件", e); } catch (Exception e) { log.error(e); } return null; } }
时间: 2024-10-22 19:45:00