先看下效果图
格式排版不是很好看,但是目前基本满足要求,具体做法如下:
因为要提取不同 table 的内容,原来的 xsl 模板又没有唯一标识符, 需要修改 xsl 模板, 增加了 classname, 便于过滤。
<tr class="case_details" valign="top">
<xsl:attribute name="id">
<xsl:choose>
<xsl:when test="$failureCount > 0">Failure</xsl:when>
</xsl:choose>
</xsl:attribute>
<td>
<xsl:if test="$failureCount > 0">
<a><xsl:attribute name="href">#<xsl:value-of select="$label" /></xsl:attribute>
<xsl:value-of select="$label" />
</a>
</xsl:if>
<xsl:if test="0 >= $failureCount">
<xsl:value-of select="$label" />
</xsl:if>
</td>
<td align="center">
<xsl:value-of select="$count" />
</td>
<td align="center">
<xsl:value-of select="$failureCount" />
</td>
<td align="center">
<xsl:call-template name="display-percent">
<xsl:with-param name="value" select="$successPercent" />
</xsl:call-template>
</td>
<td align="center">
<xsl:call-template name="display-time">
<xsl:with-param name="value" select="$averageTime" />
</xsl:call-template>
</td>
<td align="center">
<a href="">
<xsl:attribute name="href"><xsl:text/>javascript:change('page_details_<xsl:value-of select="position()" />')</xsl:attribute>
<img src="expand.png" alt="expand/collapse"><xsl:attribute name="id"><xsl:text/>page_details_<xsl:value-of select="position()" />_image</xsl:attribute></img>
</a>
</td>
</tr>
public static void getHTMLData(String path){
File input = new File(path);
try{
org.jsoup.nodes.Document doc = Jsoup.parse(input, "UTF-8");
// get summary of report
detail = doc.select("table.details").first();
// get case_details table
casetrs = doc.getElementsByClass("case_details");
// setfailTable format to combine failed case together
failTable = "<table class=\"details\" width=\"95%\" cellspacing=\"2\" cellpadding=\"5\" border=\"0\" align=\"center\">";
failTable += "<tbody>";
failTable += "<tr valign=\"top\">";
failTable += "<th>URL</th><th># Samples</th><th>Failures</th><th>Success Rate</th><th>Average Time</th><th></th>";
failTable += "</tr>";
// get fail td element
for(int i = 1; i<casetrs.size();i++){
failuretd = casetrs.get(i).select("td").get(2);
//System.out.println(failuretd);
if (failuretd.text().contentEquals("0") == false){
failTable += casetrs.get(i);
}
}
failTable += "</tbody>";
failTable += "</table>";
}catch(IOException e){
e.printStackTrace();
}
}
目前还没有跟 Jenkins 结合,所以用了个傻办法:每次登录网站,根据响应内容获取版本信息, 作为邮件主题一部分进行发送。
<target name="run">
<antcall target="clean" />
<antcall target="runAPI" />
<antcall target="generateReport" />
<antcall target="sendEmail" />
</target>
<target name="clean">
<echo message="Precondition: clean log and report files"/>
<!-- Modified by Michelle 20170719 to create output folder based on time. This is to keep older result for a while-->
<mkdir dir="${lastreport}"/>
<copy todir="${lastreport}">
<fileset dir="${jmeter.result.jtl.dir}" >
<include name = "TestReport.html" />
<include name = "TestReport.jtl" />
<include name = "jmeter.log" />
</fileset>
</copy>
<delete file="${jmeter.home}/bin/jmeter.log" />
<delete file="${jmeter.result.jtl.dir}/jmeter.log" />
<delete file="${jmeter.result.jtl.dir}/TestReport.html"/>
<delete file="${jmeter.result.jtl.dir}/TestReport.jtl"/>
</target>
<target name="runAPI">
<taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" />
<jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlName}">
<testplans dir="C:\jmeter" includes="restapi.jmx" />
<property name="jmeter.save.saveservice.output_format" value="xml" />
</jmeter>
</target>
<target name="sendEmail">
<taskdef name="jmeter" classname="org.programmerplanet.ant.taskdefs.jmeter.JMeterTask" />
<jmeter jmeterhome="${jmeter.home}" resultlog="${jmeter.result.jtlName}">
<testplans dir="C:\jmeter" includes="sendReport.jmx" />
</jmeter>
</target>
<target name="generateReport">
<xslt in="${jmeter.result.jtlName}" out="${jmeter.result.htmlName}" style="${jmeter.home}/extras/jmeter-results-detail-report_21-Michelle.xsl" />
<copy todir="${jmeter.result.html.dir}">
<fileset dir="${jmeter.home}/extras">
<include name="collapse.png" />
<include name="expand.png" />
</fileset>
<fileset dir="${jmeter.home}/bin">
<include name="jmeter.log" />
</fileset>
</copy>
</target>
后期还有结合 CI, 优化报告。