读取config配置
在搭建自动化测试框架时,经常会使用config.properties文件存储配置,文件内容格式如下:
读取config.properties文件代码如下:
public class Putils { public static Properties readConfig(){ Properties pps = new Properties(); String PATH="/config.properties"; try { InputStream in=Putils.class.getResourceAsStream(PATH); pps.load(in); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return pps; } public String getProperties(String key){ //考虑命令行的方式的读取 Properties properties=Putils.readConfig(); String value=properties.getProperty(key, ""); return value; } }
调用方式:
public static WebDriver initDriver(){ Putils propertyUtils=new Putils(); String browserType=propertyUtils.getProperties("browserType"); if("ie".equals(browserType.trim())){ DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer(); ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true); driver = new InternetExplorerDriver(ieCapabilities); driver.manage().window().maximize(); }else if("chrome".equals(browserType.trim())){ ChromeOptions option = new ChromeOptions(); option.addArguments("-test-type"); //最大化浏览器窗口 option.addArguments("-start-maximized"); driver = new ChromeDriver(option); }else{ driver= new FirefoxDriver(); } driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); return driver; }