This is an attempt to update the original HTML report class. I have added the following:
- Reformatted the time methods
- Changes to the overall look and format from the original report
- Added a css folder to easily change the color scheme of the report
- Added an images folder to include browser type icons
- Included a test time duration in the report (minutes, seconds)
- Included a field to indicate the environment being tested
- Included percentage of passed/failed in the report
The .zip file contains everything you need to get up and running, and includes a sample script plus two sample HTML reports. Please feel free to modify and enhance to your liking.
Here is the test script that creates, modifies and finishes the HTML report (google_search.rb):
# google_search.rb require 'watir' require 'classes/html.class' site = http://www.google.com/ browser_type = 'IE' #~ browser_type = 'Firefox' test_environment = 'QA' # initialize HTML report @html = HTMLReport.new() # createReport(reportName, header, browser_type) @report = @html.createReport('google_search', 'Google Search', browser_type) # begin begin if browser_type == 'IE' @browser = Watir::Browser.start(site) elsif browser_type == 'Firefox' Watir::Browser.default = 'firefox' @browser = Watir::Browser.start(site) end ###################################### test_case = @html.newTestName('Verify controls') ###################################### test = 'Check search text field' @html.add_to_report(@browser.text_field(:name, 'q').exists?, test, 'PASS: Located search text field', 'FAIL: Unable to locate search field') test = 'Check Google Search button' @html.add_to_report(@browser.button(:name, 'btnG').exists?, test, 'PASS: Located Google Search button', 'FAIL: Unable to locate Google Search button') test = 'Check Feeling Lucky button' @html.add_to_report(@browser.button(:name, 'btnI').exists?, test, 'PASS: Located Feeling Lucky button', 'FAIL: Unable to locate Feeling Lucky button') ###################################### test_case = @html.newTestName('Perfom Watir search') ###################################### @browser.text_field(:name, 'q').set 'Watir' @browser.button(:name, 'btnG').click test = 'Check valid search results (1)' @html.add_to_report(@browser.text.include?('Babysteps in WATIR'), test, 'PASS: Located text "Babysteps in WATIR"', 'FAIL: Unable to locate text "Babysteps in WATIR"') test = 'Check valid search results (2)' @html.add_to_report(@browser.text.include?('Project Info'), test, 'PASS: Located text "Project Info", 'FAIL: Unable to locate text "Project Info"') test = 'Check for invalid search results' @html.add_to_report(@browser.text.include?('Suez Canal'), test, 'PASS: Did not locate text "Suez Canal"', 'FAIL: Found text "Suez Canal"') # teardown @browser.close @html.finishReport(@report, browser_type, test_environment) rescue => e puts $! puts e.backtrace @html.finishReport(@report, browser_type, test_environment) #~ @browser.close end
And here is the modified HTML class (html.class.rb):
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) @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=0 cellpadding=0 cellspacing=0> <tbody> <tr> <td> <table width=100% border=0 cellpadding=2 cellspacing=2> <tbody> <tr> <th class="header" align=right>Test Report: ' + header + '</th> </tr> </tbody> </table> <br /> <center> <table border=0 width=95% cellpadding=0 cellspacing=0> <tbody> <tr> <th width=15%>File Name:</th> <td width=85% colspan=5 align="center">' + reportName + '-' + strTime + '</td> </tr> <tr> <th class="nobg" width=15%>Test Date:</th> <td width=30% align="center">' + strNiceTime + '</td> <th class="nobg" width=15%>Test Result:</th>' @reportContent2 = ' </center> <br><br> <center> <table width=95% cellpadding=2 cellspacing=0> <tbody> <tr> <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">' + 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">' + result + '</td>' @passed += 1 else @overallResult = 'FAIL' @reportContent2 = @reportContent2 + '<td class="result_fail">' + 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>©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%>Run time:</th> <td width=20% align="center">' + test_time + '</td> <th width=15%>Browser:</th> <td align="center" width=20%><img src="../classes/images/' + browser.downcase + '.gif" width=30%/></td> <th width=10%>Env:</th> <td align="center">' + env + '</td> </tr> </tbody></table>') strFile.puts(@reportContent2) # Close the report strFile.close end end