android UiAutomator如何把运行结果写到html文档里

来源:互联网 发布:linux下c语言创建线程 编辑:程序博客网 时间:2024/06/15 15:33

昨天研究了一下如何生成html文件的测试报告,但没有发出来写html文件的代码,经过整理之后,觉得差不多了。发出来供大家参考。

public static void createWebReport(List<String[]> runresult) throws IOException {//这个是页面前面固定的信息String starttext = "<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>"+"</head><body><h1 style='text-align:center'>android UiAutomator测试报告</h1><p style='text-align:center'>"+getNow()+"测试报告"+"</p>"+"<table  border='1' style='table-layout:fixed;font-size:14px;'>"+"<thead>"+"<tr><td width='30px'>编号</td>"+"<td width='120px'>用例名</td>"+"<td width='70px'>运行状态</td>"+"<td width='250px'>错误信息</td>"+"<td width='160px'>错误行Library</td>"+"<td width='160px'>错误行Special</td>"+"<td width='160px'>错误行Case</td><td width='100px'>开始时间</td>"+"<td width='100px'>结束时间</td><td width='100px'>备用列</td></tr>"+"</thead><tbody>";//这里是页面后面固定的信息String endtext = "</tbody></table></body></html>";File file = new File("C:\\Users\\fankaiqiang\\Desktop\\888\\"+getNow()+".html");//新建一个html文档if (!file.exists()) {//判断是否存在,不存在先创建file.createNewFile();}//将运行信息输出到html文档中boolean isClose = false;//写入时用到BufferedWriter bf;FileOutputStream outputStream = new FileOutputStream(file, true);OutputStreamWriter outWriter = new OutputStreamWriter(outputStream);String sheet = "";for (int i = 0; i < runresult.size(); i++) {sheet = sheet + "<tr>";//添加行String[] result = runresult.get(i);//获取用例运行信息for (int j = 0; j < result.length; j++) {if (result[2] == "运行成功") {//运行成功,就用绿色String addtext = "<td style='word-wrap:break-word;word-break:break-all;background-color:paleturquoise'>" + result[j]+ "</td>";sheet = sheet + addtext;} else if (result[2] == "断言错误") {//断言失败,就用黄色String addtext = "<td style='word-wrap:break-word;word-break:break-all;background-color:yellow;'>" + result[j]+ "</td>";sheet = sheet + addtext;} else if (result[2] == "运行错误") {//运行失败就用红色String addtext = "<td style='word-wrap:break-word;word-break:break-all;background-color:red;'>" + result[j]+ "</td>";sheet = sheet + addtext;} else {output("运行信息错误!");}}sheet = sheet + "</tr>";//结束行}bf = new BufferedWriter(outWriter);//写入bf.append(starttext);//写入页面前面的信息bf.append(sheet);//写入报告信息bf.append(endtext);//写入页面后面信息bf.flush();if (isClose) {bf.close();}}

里面用到了一些自己自定义的方法,大家看方法名就知道做什么用了,可以忽略。

原创粉丝点击