1.递归加载配置信息代码:(需要注意的是 不同的配置文件不要有相同的key,一般key的命名使用命名空间方式如以下命名方式:
#页面加载时间 initDriver.webdriver.pageloadtime = 20 #查找元素超时时间(全局) initDriver.webdriver.scanelementtime = 20
package ec.qa.autotest.ui.utility; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; import java.util.Properties; import ec.qa.autotest.ui.constants.CommonConstants; public class InitPropertiesUtil { /** * @author xin.wang * @throws 将config下的所有配置文件的内容加载到MAP中,方便快速获取配置文件的值 */ public InitPropertiesUtil() { PropertiesUtil.setKeyValueMap(initKeyValueMap(System.getProperty(CommonConstants.CONFIG_FOLDER_PATH_KEY))); } public HashMap<String, String> initKeyValueMap(String floderPath) { HashMap<String, String> proKeyValus = new HashMap<String, String>(); ArrayList<File> proFiles = getConfigFiles(new File(floderPath)); try { for (File f : proFiles) { setKeyValueToMap(f, proKeyValus); } if (proKeyValus.isEmpty()) { throw new Exception("have no properties files or properties files have no key-value"); } } catch (Exception e) { e.printStackTrace(); } return proKeyValus; } /** * @author xin.wang * @see 将所有properties文件的key,value存入到map中,实现properties值的跨文件读取和快速读取。 */ private void setKeyValueToMap(File f, HashMap<String, String> proKeyValues) throws Exception { Enumeration<?> en = geProObj(f.getAbsolutePath()).propertyNames(); while (en.hasMoreElements()) { String key = (String) en.nextElement(); String Property = geProObj(f.getAbsolutePath()).getProperty(key); proKeyValues.put(key, Property); } } /** * @author xin.wang * @param floderPath * 文件夹在工程中的路径 * @param files * 存放文件的列表对象 * @see 递归获取此文件夹下的所有配置文件 */ public ArrayList<File> getConfigFiles(File floderPath, ArrayList<File> files) { for (File f : floderPath.listFiles()) { if (f.isFile() && f.getName().endsWith(".properties")) { files.add(f); continue; } getConfigFiles(f, files); } return files; } public ArrayList<File> getConfigFiles(File floderPath){ ArrayList<File> files = new ArrayList<File>(); return getConfigFiles(floderPath,files); } /** * @author xin.wang * @see 初始化properties文件的对象 */ private Properties geProObj(String FilePath) throws Exception { Properties p = new Properties(); InputStream ins = null; try { ins = new BufferedInputStream(new FileInputStream(FilePath)); p.load(ins); ins.close(); } catch (Exception e) { throw new Exception("can not find the properties file : " + FilePath); } return p; } }
package ec.qa.autotest.ui.utility; import java.util.HashMap; /** * @author xin.wang * @see 获取配置文件的值 */ public class PropertiesUtil { private static HashMap<String, String> proKeyValus = null; public static void setKeyValueMap(HashMap<String, String> map){ PropertiesUtil.proKeyValus = map; } public static String getProValue(String Key){ return proKeyValus.get(Key); } public static HashMap<String, String> getProKVMap(){ return proKeyValus; } }
@BeforeSuite(alwaysRun = true) public void initTest() throws Exception { System.setProperty(CommonConstants.CONFIG_FOLDER_PATH_KEY, CommonConstants.CONFIG_FOLDER_PATH_VALUE); new InitPropertiesUtil(); browserType = PropertiesUtil.getProValue(PropertiesKeys.BROWSER_TYPE).toString(); }
4.使用PropertiesUtil工具类获取配置文件的健值实例:
@BeforeMethod(alwaysRun = true) public void initDriver(Method m) throws Exception { ConfigDriverParameters cp = new ConfigDriverParameters(); cp.setPageLoadTime(Long.parseLong(PropertiesUtil.getProValue(PropertiesKeys.ADMIN_PORTAL_PAGELOAD_TIME))); cp.setTargetWebSite(PropertiesUtil.getProValue(PropertiesKeys.ADMIN_PORTAL_AUTO_TEST_WEBSITE)); cp.setSerachElementTime(Long.parseLong(PropertiesUtil.getProValue(PropertiesKeys.IMPLICITLYWAIT_TIME))); cp.setTestMethod(m); configDriver(cp); }