下面贴出测试报告调用的类ReportClass代码:
class ReportClass
# Initialize the report class
def initialize()
@overallResult = 'Passed'
@reportContent1 = ''
@reportContent2 = ''
end
# Create a report
def createReport(reportName)
# Get current time
t = Time.now
# Format the day
if(t.day.to_s.length == 1)
strDay = '0' + t.day.to_s
else
strDay = t.day.to_s
end
# Format the month
if(t.month.to_s.length == 1)
strMonth = '0' + t.month.to_s
else
strMonth = t.month.to_s
end
# Format the year
strYear = t.year.to_s
# Format the hour
if(t.hour.to_s.length == 1)
strHour = '0' + t.hour.to_s
else
strHour = t.hour.to_s
end
# Format the minutes
if(t.min.to_s.length == 1)
strMinutes = '0' + t.min.to_s
else
strMinutes = t.min.to_s
end
# Format the seconds
if(t.sec.to_s.length == 1)
strSeconds = '0' + t.sec.to_s
elsif (t.sec.to_s.length == 0)
strSeconds = '00'
else
strSeconds = t.sec.to_s
end
# Create the report name
strTime = '_' + strYear + strMonth + strDay + '_' + strHour + strMinutes + strSeconds + '.html'
strNiceTime = strYear + '-' + strMonth + '-' + strDay + ' @ ' + strHour + ':' + strMinutes + ':' + strSeconds
strTotalReport = reportName + "@TestReport" + strTime
# Create the HTML report
strFile = File.open(strTotalReport, 'w')
# Format the header of the HTML report
@reportContent1 = '<html>
<head>
<meta content=text/html; charset=GBK http-equiv=content-type>
<title>Test Report-测试报告</title>
<style type=text/css>
.title { font-family: verdana; font-size: 30px; font-weight: bold; align: left; color: #045AFD;}
.bold_text { font-family: verdana; font-size: 12px; font-weight: bold;}
.normal_text { font-family: verdana; font-size: 12px; font-weight: normal;}
.small_text { font-family: verdana; font-size: 10px; font-weight: normal; }
.border { border: 1px solid #045AFD;}
.border_left { border-top: 1px solid #045AFD; border-left: 1px solid #045AFD; border-right: 1px solid #045AFD;}
.border_right { border-top: 1px solid #045AFD; border-right: 1px solid #045AFD;}
.result_ok { font-family: verdana; font-size: 12px; font-weight: bold; text-align: center; color: green;}
.result_nok { font-family: verdana; font-size: 12px; font-weight: bold; text-align: center; color: red;}
.overall_ok { font-family: verdana; font-size: 12px; font-weight: bold; text-align: left; color: green;}
.overall_nok { font-family: verdana; font-size: 12px; font-weight: bold; text-align: left; color: red;}
.bborder_left { border-top: 1px solid #045AFD; border-left: 1px solid #045AFD; border-bottom: 1px solid #045AFD; background-color:#045AFD;font-family: verdana; font-size: 12px; font-weight: bold; text-align: center; color: white;}
.bborder_right { border-right: 1px solid #045AFD; background-color:#045AFD;font-family: verdana; font-size: 12px; font-weight: bold; text-align: center; color: white;}
</style>
</head>
<body>
<br>
<center>
<table width=800 border=0 cellpadding=2 cellspacing=2>
<tbody>
<tr>
<td>
<table width=100% border=0 cellpadding=2 cellspacing=2>
<tbody>
<tr>
<td style=width: 150px;> </td>
<td align=right><p class=title>Test Report</p></td>
</tr>
</tbody>
</table>
<br>
<hr width=100% class=border size=1px>
<br>
<br>
<center>
<table border=0 width=95% cellpadding=2 cellspacing=2>
<tbody>
<tr>
<td width=10%><p class=bold_text>报告名称:</p></td>
<td width=90%><p class=normal_text>' + strTotalReport + '</p></td>
</tr>
<tr>
<td width=10%><p class=bold_text>测试时间:</p></td>
<td width=90%><p class=normal_text>' + strNiceTime + '</p></td>
</tr>
<tr>
<td width=10%><p class=bold_text>用例编号:</p></td>
<td width=90%><p class=normal_text>'+ reportName +'</p></td>
</tr>
<tr>
<td width=10%><p class=bold_text>统计结果:</p></td>
'
@reportContent2 = '</tr>
</tbody>
</table>
</center>
<br><br>
<center>
<table width=95% cellpadding=2 cellspacing=0>
<tbody>
<tr>
<td class=bborder_left width=10%><p>编号</p></td>
<td class=bborder_left width=10%><p>结果</p></td>
<td class=bborder_right width=80%><p>描述</p></td>
</tr>'
# Close the report
strFile.close
return strTotalReport
end
def addtoReport(reportName, step, result, description)
@reportContent2 = @reportContent2 + '<tr><td class=border_left width=10%><p class=normal_text>' + step + '</p></td>'
# Format the body of the HTML report
if (result == 'Passed')
@reportContent2 = @reportContent2 + '<td class=border_right width=10%><p class=result_ok>' + result + '</p></td>'
else
@overallResult = 'Failed'
@reportContent2 = @reportContent2 + '<td class=border_right width=10%><p class=result_nok>' + result + '</p></td>'
end
@reportContent2 = @reportContent2 + '<td class=border_right width=80%><p class=normal_text>' + description + '</p></td></tr>'
end
def finishReport(reportName)
# Open the HTML report
strFile = File.open(reportName, 'a')
# Format the footer of the HTML report
@reportContent2 = @reportContent2 + '<tr>
<td class=bborder_left width=10%><p> </p></td>
<td class=bborder_left width=10%><p> </p></td>
<td class=bborder_right width=80%><p> </p></td>
</tr>
</table>
<br><br>
<hr width=100% class=border size=1px>
<br>
<center><p class=small_text>©2010</p></center>
<br>'
strFile.puts(@reportContent1)
if (@overallResult == 'Passed')
strFile.puts('<td width=90%><p class=overall_ok>' + @overallResult + '</p></td>')
else
strFile.puts('<td width=90%><p class=overall_nok>' + @overallResult + '</p></td>')
end
strFile.puts(@reportContent2)
# Close the report
strFile.close
end
end