测试工程添加如下XML文件,并在POM中引入DOM4J的包:
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6</version>
</dependency>
XML结构:
<?xml version="1.0" encoding="UTF-8"?>
<testdata>
<data-node id="AdminPortalLoginPage">
<data key="UserName" value="admin"/>
<data key="PassWord" value="111111"/>
</data-node>
<data-node id="AddProductLibraryPage">
<data key="ProLibName" value="测试产品库"/>
<data key="PLDescription" value="测试专用"/>
</data-node>
</testdata>
在properties配置文件中维护测试数据文件地址:
#测试数据文件夹地址
testcase.testdata.path=test-data
使用DOM解析上述XML文件:
package ec.qa.autotest.ui.framework.init;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import ec.qa.autotest.ui.framework.utility.PropertiesUtil;
/**
* @author xin.wang
* 解析XML文件
*/
public class ParseDataXml {
/**
* XML文件节点名及属性名
*/
private static String testDataFielsuffix = ".xml";
private static String dataNodeTag = "data-node";
private static String dataNodeID = "id";
private static String dataTag = "data";
private static String dataKey = "key";
private static String dataValue = "value";
public static HashMap<String, String> getDataNode(String dataNodeIds) throws DocumentException {
if(null == dataNodeIds){
return null;
}
String[] dataNodeId = dataNodeIds.split(",");
HashSet<String> nodeSet = new HashSet<String>();
for (String str : dataNodeId) {
nodeSet.add(str);
}
HashMap<String, String> dataMap = new HashMap<String, String>();
ArrayList<File> fs = InitPropertiesUtil.getConfigFiles(new File(PropertiesUtil.getProValue("testcase.testdata.path")), testDataFielsuffix);
for (File f : fs) {
setDataNodeMapFromFile(f, nodeSet, dataMap);
}
return dataMap;
}
@SuppressWarnings("unchecked")
public static void setDataNodeMapFromFile(File f, HashSet<String> nodeSet, HashMap<String, String> dataMap)
throws DocumentException {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(f);
Element root = document.getRootElement();
List<Element> childList = root.elements(dataNodeTag);
for (Element e : childList) {
if (nodeSet.contains(e.attributeValue(dataNodeID))) {
List<Element> dataList = e.elements(dataTag);
for (Element de : dataList) {
dataMap.put(de.attributeValue(dataKey), de.attributeValue(dataValue));
}
nodeSet.remove(e.attributeValue(dataNodeID));
}
}
}
}
以上 setDataNodeMapFromFile 为解析XML文件主要逻辑,getDataNode 为获取数据节点方法
测试用例调用此方法获取指定ID的数据节点:
public LoginInfo getLoginInfo() throws DocumentException {
HashMap<String, String> dataMap = ParseDataXml.getDataNode("AdminPortalLoginPage");
info = new LoginInfo();
info.setName(dataMap.get("UserName"));
info.setPapassword(dataMap.get("PassWord"));
return info;
}
或者结合之前的自动注入方式 在测试用例中自动注入当前测试用例需要的数据节点:
如何实现自动注入详见:http://blog.csdn.net/wangxin1982314/article/details/50221641
@DataObject(DataNode = "AdminPortalLoginPage")
private HashMap<String, String> dataMap;
public LoginInfo getLoginInfo() {
info.setName(dataMap.get("UserName"));
info.setPapassword(dataMap.get("PassWord"));