转载地址:http://blog.csdn.net/zqilu/article/details/12854899
将robotium中testcase一条一条执行
首先需要建xml文件将testcase加入到文件中如
<?xml version="1.0" encoding="UTF-8"?>
<Project>
<PackageName name="com.tmall.wireless.test">
<Class name="MainTest">
<method>
<include name="testAInfomation"></include>
<include name="testBTip"></include>
<include name="testCDrag"></include>
<include name="testDAttention"></include>
</method>
</Class>
<Class>
<method>
<include name="testABtest"></include>
</method>
</Class>
</PackageName>
<PackageName name="test">
<Class name="testSearch">
<method>
<include name="testASearch"></include>
<include name="testBshortcut"></include>
</method>
</Class>
</PackageName>
</Project>
然后新建java文件读取这个xml文件
public class ReadXml{
public static void main(String[] args) throws Exception{
Process pro;
Boolean buff=false; //判断命令是否执行失败
String command=null; //dos命令
List <String> packageNames=new ArrayList<String>();
List<String>classNames=new ArrayList<String>();
String methodName=null;
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=dbf.newDocumentBuilder();
Document doc=(Document)builder.parse("文件名") //如果想已参数形式传入则为 args[0]
Element root=doc.getDocumentElement();
NodeList pm=root.getElementsByTagName("packageName");
NodeList cm=null;
NodeList mm=null;
NodeList tc=null;
for(int i=0;i<pm.getLength();i++){
cm=((Element)pm.item(i)).getElementsByTagName("className");
packageNames.add(((Element)pm.item(i)).getAttribute("name")); //从xml文件中读取到包名
for(int j=0;j<cm.getLength();j++){
mm=((Element)cm.item(j)).getElementsByTagName("method");
classNames.add(packageNames.get(i)+"."+((Element)cm.item(i)).getAttribute("name")); //从xml文件中读取包名.类名
for(int k=0;k<mm.getLength();k++){
tc=((Element)mm.item(k)).getElementsByTagName("include");
for(int m=0;m<tc.getLength();m++){
methodName=((Element)tc.item(m)).getAttribute("name");
command="adb shell am instrumentation -e "+classnames.get(j)+"#"+methodName+" - w"+packageName.get(i)+"/android.test.InstrumentationTestRunner" ;
proc= Runtime().getRuntime().exec(command); //执行dos命令
BufferedReader br=new BuffererReader(new InputStreamReader(proc.getInputStream())); //读取执行命令的log信息
String line=br.readLine();
while(line !=null){
if(line.maches(".* INSTRUMENTATION_FAILED.*$")){
//log中是否含有这样的错误信息
buff=true;break;
}
line=br.readLine();
}
if(buff){//如果执行失败就重现执行一次。再次失败就失败把!
proc=Runtime.getRuntime().exec(command);
buff=false;
}
}
}
}
}
}
}
方法比较笨,大家如果有好的想法可以讨论下!!