Watir:生成html格式的测试报告
上一篇 / 下一篇 2009-02-05 16:00:09 / 个人分类:watir
如下代码生成html格式的测试报告
# Load classes
require 'rubygems'
require 'watir'
class CLReport
# 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 = '_' + strDay + strMonth + strYear + '_' + strHour + strMinutes + strSeconds + '.html'
strNiceTime = strDay + '-' + strMonth + '-' + strYear + ' @ ' + strHour + ':' + strMinutes + ':' + strSeconds
strTotalReport = reportName + strTime
# Create the HTML report
strFile = File.open(strTotalReport, 'w')
puts strFile
# Format the header of the HTML report
@reportContent1 = '<html>
<head>
<meta content=text/html; charset=ISO-8859-1 http-equiv=content-type>
<title>QA 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>QA 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=20%><p class=bold_text>Report Name</p></td>
<td width=5%><p class=bold_text>:</p></td>
<td width=75%><p class=normal_text>' + strTotalReport + '</p></td>
</tr>
<tr>
<td width=20%><p class=bold_text>Test Execution</p></td>
<td width=5%><p class=bold_text>:</p></td>
<td width=75%><p class=normal_text>' + strNiceTime + '</p></td>
</tr>
<tr>
<td width=20%><p class=bold_text>Overall Result</p></td>
<td width=5%><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=30%><p>Test Step</p></td>
<td class=bborder_left width=10%><p>Result</p></td>
<td class=bborder_right width=60%><p>Descrīption</p></td>
</tr>'
# Close the report
strFile.close
return strTotalReport
end
def addtoReport(reportName, step, result, descrīption)
@reportContent2 = @reportContent2 + '<tr><td class=border_left width=30%><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=60%><p class=normal_text>' + descrīption + '</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=30%><p> </p></td>
<td class=bborder_left width=10%><p> </p></td>
<td class=bborder_right width=60%><p> </p></td>
</tr>
</table>
<br><br>
<hr width=100% class=border size=1px>
<br>
<center><p class=small_text>©2008</p></center>
<br>'
strFile.puts(@reportContent1)
if (@overallResult == 'PASSED')
strFile.puts('<td width=75%><p class=overall_ok>' + @overallResult + '</p></td>')
else
strFile.puts('<td width=75%><p class=overall_nok>' + @overallResult + '</p></td>')
end
strFile.puts(@reportContent2)
# Close the report
strFile.close
end
end
# The main test
begin
# Create the new report
r = CLReport.new()
testReport = r.createReport('C:\\')
# Start browser
browser = Watir::IE.new
browser.goto('http://www.blabla.com')
# --- START TEST ---
# Do a check and report the result
if (1 > 0)
r.addtoReport(testReport, 'Check if 1 is bigger than 0', 'PASSED', '1 is indeed bigger than 0')
else
r.addtoReport(testReport, 'Check if 1 is bigger than 0', 'FAILED', '1 is NOT bigger than 0!')
end
# Do another check and report the result
if (2 > 1)
r.addtoReport(testReport, 'Check if 2 is bigger than 1', 'PASSED', '2 is indeed bigger than 1')
else
r.addtoReport(testReport, 'Check if 2 is bigger than 1', 'FAILED', '2 is NOT bigger than 1!')
end
# --- END TEST ---
# Close browser
browser.close
# Finish the report
r.finishReport(testReport)
rescue
# Send crash info to report
r.addtoReport(testReport, 'Test crashed with reason '+$!, 'FAILED', 'Test crashed!')
# Finish the report
r.finishReport(testReport)
end
红色部分为测试结果填入.
相关阅读:
- 自动化测试常用的工具 --- 开源WEB篇 (WayneT, 2008-12-03)
- [转载]比较watir 和 selenium --- Bret Pettichord (WayneT, 2008-12-04)
- watir编写阿里助手中“管理供求信息”中的重发 (spring, 2009-1-21)
- Watir对WEB的操作 (spring, 2009-1-22)
- watir键盘指令发送列表 (spring, 2009-1-22)
- Watir学习系列--环境配置 (spring, 2009-1-22)
- watir学习系列--eclipse环境搭建 (spring, 2009-1-22)
- watir学习系列 (spring, 2009-1-22)
- Watir:Excel文件打开数据读取 (spring, 2009-2-04)
TAG: watir