我们做自动化测试时,会遇到使用xml存储数据,但是这些数据可以封装成一个类,进行数据的传递。以下通过一个实际的例子,展示给大家,请欣赏。
第一步:xml存储将要使用的数据
1
4
package com.test;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ParserXml {
@SuppressWarnings({ "rawtypes", "unchecked" })
public List parser3Xml(String fileName) {
File inputXml = new File(fileName);
List list=new ArrayList();
SAXReader saxReader = new SAXReader();
try {
Document document = saxReader.read(inputXml);
Element employees = document.getRootElement();
for (Iterator i = employees.elementIterator(); i.hasNext();) {
Element employee = (Element) i.next();
Map map = new HashMap();
Map tempMap = new HashMap();
for (Iterator j = employee.elementIterator(); j.hasNext();) {
Element node = (Element) j.next();
tempMap.put(node.getName(), node.getText());
}
map.put(employee.getName(), tempMap);
list.add(map);
}
} catch (DocumentException e) {
System.out.println(e.getMessage());
}
return list;
}
}
package com.test;
public class testMethod1 {
private String input;
private String button;
public void setInput(String input) {
this.input = input;
}
public String getInput() {
return input;
}
public void setButton(String button) {
this.button = button;
}
public String getButton() {
return button;
}
}
第四步:处理方法
package com.test;
import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class TestData {
@SuppressWarnings("rawtypes")
private static List l;
public TestData() {
this.getXmlData();
}
public void getXmlData(){
ParserXml p = new ParserXml();
l = p.parser3Xml(new File("src/TestData.xml").getAbsolutePath());
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List providerMethod(){
List oList = new ArrayList();;
l = (new ParserXml()).parser3Xml(new File("src/TestData.xml").getAbsolutePath());
for (int i = 0; i < l.size(); i++) {
Map m = (Map) l.get(i);
String key = (String)m.keySet().iterator().next();
Class
demo = null;
Object obj = null;
try{
demo = Class.forName("com.test." + key);
}catch(Exception e){
e.printStackTrace();
}
try{
obj = demo.newInstance();
}catch(Exception e){
e.printStackTrace();
}
Map
dm = (Map
) m.get(key);
for(Entry
entry: dm.entrySet()){
String key1 = entry.getKey();
String value = entry.getValue();
setter(obj,key1,value,String.class);
}
oList.add(obj);
}
return oList;
}
/**
* @param obj
* 操作的对象
* @param att
* 操作的属性
* @param value
* 设置的值
* @param type
* 参数的属性
* */
public static void setter(Object obj, String att, Object value,
Class
type) {
try {
Method method = obj.getClass().getMethod("set" + att, type);
method.invoke(obj, value);
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressWarnings("rawtypes")
public static void main(String[] args) {
List list = TestData.providerMethod();
for(int i=0;i