博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
watir测试报告(二)
阅读量:4052 次
发布时间:2019-05-25

本文共 4331 字,大约阅读时间需要 14 分钟。

#本文主要是生成测试报告的格式, 可以根据各自的需要进行修改。

class HTMLReport

 
# Initialize the report class
  def
initialize()
    @overallResult = 'PASS'
    @reportContent1 = ''
    @reportContent2 = ''
    @start_time = Time.now
    @passed = 0
    @failed = 0
  end
 
  def
createReport(reportName, header, browser_type)
#此处包含生成测试报告的名字、title、浏览器的类型。   
    @reportName = reportName
   
    def
get_date
#报告产生的日期
      Time.now.strftime("%m.%d.%y")
    end
    def
get_time
      Time.now.strftime("%I.%M.%S.%p")
    end
   
   
# Create the report name
    d = self.get_date
    t = self.get_time
    strTime = "#{d}-#{t}.html"

    strNiceTime = "#{d} @ #{t}"

#此处规定了测试报告生成的名字, 以及路径, 其实路径最好不好写, 以免影响其独立性。

    strTotalReport =
"results\\" + reportName + '_' + browser_type + '-' + 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=ISO-8859-1 http-equiv=content-type>
      <title>Test Report: ' + header + '</title>
      <link rel="stylesheet" type="text/css" href="../classes/css/bluegray.css">
      </head>
      <body>
      <br />
      <center>
      <table width=800 border=1 cellpadding=1 cellspacing=1>
      <tbody>
      <tr>
      <td>
      <table width=100% cellpadding=2 cellspacing=2 bgcolor="#E0FFFF">
      <tbody>
      <tr>
      <th class="header" align=center>Test Report: ' + header + '</th>
      </tr>
      </tbody>
      </table>
      <br />
      <center>
      <table border=1 width=95% cellpadding=1 cellspacing=1>
      <tbody>
      <tr>
      <th width=15% bgcolor="#E0FFFF">File Name:</th>
      <td width=85% colspan=5 align="center">' + reportName + '-' + strTime + '</td>
      </tr>
      <tr>
      <th class="nobg" width=15% bgcolor="#E0FFFF">Test Date:</th>
      <td width=30% align="center">' + strNiceTime + '</td>
      <th class="nobg" width=15% bgcolor="#E0FFFF">Test Result:</th>'
   
    @
reportContent2 = '
      </center>
      <br>
    <table width=95% cellpadding=2 cellspacing=1 border=1>
      <tr><td>case title</td></tr>
      <tr><td>test</td></tr>
      </table>
      <br>
      <center>
      <table width=95% cellpadding=2 cellspacing=1 border=1>
      <tbody>
      <tr bgcolor="#E0FFFF">
      <th width=45%>Test Step</th>
      <th width=10%>Result</th>
      <th width=45%>Description</th>
      </tr>'
     
   
# Close the report
    strFile.close
       
    return strTotalReport
  end
 
  def
newTestName(name)
    @reportContent2 = @reportContent2 + '<tr><td class ="alt" colspan="3" align="center" bgcolor="#B0C4DE">' + name + '</td></tr>'
  end
 
  def
addtoReport(step, result, description)
    @reportContent2 = @reportContent2 + '<tr><td class="step">' + step + '</td>'
    # Format the body of the HTML report
    if result == 'PASS'
      @reportContent2 = @reportContent2 + '<td class="result_pass" bgcolor="green" align="center">' + result + '</td>'
      @passed += 1
    else
      @overallResult = 'FAIL'
      @reportContent2 = @reportContent2 + '<td class="result_fail" bgcolor="red" align="center">' + result + '</td>'
      @failed += 1
    end
 
    @reportContent2 = @reportContent2 + ' <td class="result_text">' + description + '</td></tr>'
  end
 
  def
add_to_report(result, test, pass_text, fail_text)
    result == true ? self.addtoReport(test, 'PASS', pass_text) : self.addtoReport(test, 'FAIL', fail_text)
  end
 
  # formats seconds to minutes, seconds
  def
format_test_time(seconds)
    if seconds < 60
      "0 min, #{"%.02f" % seconds} sec"
    else
      minutes = (seconds/60).to_i
      seconds = seconds - (minutes*60)
      "#{minutes} min, #{"%.02f" % seconds} sec"
    end
  end
 
  def
finishReport(reportName, browser, env)
    # Open the HTML report
    strFile = File.open(reportName, 'a')
    # Format the footer of the HTML report
    @reportContent2 = @reportContent2 + '</table>
      <br><br>
      <hr width=100% size=1px>
      <br />
      <center><h5>&copy;CompanyName 2009</h5></center>
      <br>'
   
    strFile.puts(@reportContent1)
    total = @passed + @failed
    percent_pass = ((@passed.to_f/total * 100)).to_s
    percent_fail = ((@failed.to_f/total * 100)).to_s
    strFile.puts('<td align="center" colspan=3>' + '<b><font color="green">' + "%.02f" % percent_pass + '% Passed, ' + '<font color="red">' + "%.02f" % percent_fail + '% Failed' + '</b></td></tr>')
    # get test time
    seconds = (Time.now - @start_time)
    test_time = self.format_test_time(seconds)
    strFile.puts('</tr>
      <th width=15% bgcolor="#E0FFFF">Run time:</th>
      <td width=20% align="center">' + test_time  + '</td>
      <th width=15% bgcolor="#E0FFFF">Browser:</th>
      <td align="center" width=20%><img src="../Images/' + browser.downcase + '.gif" width=30%/></td>
      <th width=10% bgcolor="#E0FFFF">Env:</th>
      <td align="center">' + env + '</td>
      </tr>
      </tbody></table>')
    strFile.puts(@reportContent2)
 
    # Close the report
    strFile.close
  end
end

转载地址:http://cwjci.baihongyu.com/

你可能感兴趣的文章
关于对象赋值及返回临时对象过程中的构造与析构
查看>>
VS 2005 CRT函数的安全性增强版本
查看>>
Visual Studio 2010:C++0x新特性
查看>>
drwtsn32.exe和adplus.vbs进行dump文件抓取
查看>>
cppcheck c++静态代码检查
查看>>
在C++中使用Lua
查看>>
一些socket的编程经验
查看>>
socket编程中select的使用
查看>>
可以在线C++编译的工具站点
查看>>
关于无人驾驶的过去、现在以及未来,看这篇文章就够了!
查看>>
所谓的进步和提升,就是完成认知升级
查看>>
为什么读了很多书,却学不到什么东西?
查看>>
长文干货:如何轻松应对工作中最棘手的13种场景?
查看>>
如何用好碎片化时间,让思维更有效率?
查看>>
No.174 - LeetCode1305 - 合并两个搜索树
查看>>
No.175 - LeetCode1306
查看>>
No.176 - LeetCode1309
查看>>
No.182 - LeetCode1325 - C指针的魅力
查看>>
mysql:sql alter database修改数据库字符集
查看>>
mysql:sql truncate (清除表数据)
查看>>