您的位置:首页 > 产品设计 > UI/UE

Web UI自动化测试框架 使用递归一次性加载配置文件信息(.properties)

2015-12-08 18:17 711 查看
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;
}

}


2.获取配置文件键值的工具类:(初始化此工具类中key-value的map的过程在上面类的 构造方法中):

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;
}
}


3 在测试用例集启动的时候初始化 InitPropertiesUtil :

@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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: