Wednesday 24 December 2014

How to generate HTML results using QTP

'To practice, copy below code in qtp and run to generate HTML results.

'You can customize this code as per your need and implement same in your automation frameworks.

start=Timer
'Variables
Dim fso, ts
dim intCnt
Const ForWriting = 2
dim intPass,intFail
dim StartTime
dim stTime
dim enTime
dim objIE
dim strFileURL
'Implementation
OpenFile "H:\Test.html"
AddNewCase 1,"X","X","X","Pass"
AddNewCase 2,"X","X","X","Fail"
AddNewCase 3,"X","X","X","Pass"
CloseFile
' Function To Open the HTML file
Function OpenFile(strFileName)
StartTime = Timer
stTime = Time
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(strFileName, ForWriting, True)
strFileURL = strFileName
CreateHeader
End Function
' Function To Create Header
Function CreateHeader()
ts.writeline("<html>")
ts.writeline("<title>Test Results</title>")
ts.WriteLine("<head></head>")
ts.WriteLine("<body>")
ts.WriteLine("<font face='Tahoma'size='2'>")
ts.WriteLine("<h1>Test Results</h1>")
ts.writeline("<table border='0' width='100%' height='47'>")
ts.writeline("<tr>")
ts.writeline("<td width='13%' bgcolor='#CCCCFF' align='center'><b><font color='#000000' face='Tahoma' size='2'>TestCaseID</font></b></td>")
ts.writeline("<td width='24%' bgcolor='#CCCCFF'><b><font color='#000000' face='Tahoma' size='2'>Objective</font></b></td>")
ts.writeline("<td width='23%' bgcolor='#CCCCFF'><b><font color='#000000' face='Tahoma' size='2'>Expected Result</font></b></td>")
ts.writeline("<td width='22%' bgcolor='#CCCCFF'><b><font color='#000000' face='Tahoma' size='2'>Actual Result</font></b></td>")
ts.writeline("<td width='18%' bgcolor='#CCCCFF' align='center'><b><font color='#000000' face='Tahoma' size='2'>Pass/Fail</font></b></td>")
ts.writeline("</tr>")
End Function
' Function To Add New Case
Function AddNewCase(strTCID,strObjective,strExpectedResult,strActualResult,strPassFail)
ts.writeline("<tr>")
ts.writeline("<td width='13%' bgcolor='#FFFFDC' valign='middle' align='center'>" & strTCID & "</td>")
ts.writeline("<td width='24%' bgcolor='#FFFFDC' valign='top' align='justify'>" & strObjective & "</td>")
ts.writeline("<td width='23%' bgcolor='#FFFFDC' valign='top' align='justify'>" & strExpectedResult & "</td>")
ts.writeline("<td width='22%' bgcolor='#FFFFDC' valign='top' align='justify'>" & strActualResult & "</td>")
if strPassFail = "Pass" then
ts.writeline("<td width='18%' bgcolor='#FFFFDC' valign='middle' align='center'><b><font color='Green' face='Tahoma' size='2'>" & strPassFail & "</font></b></td>")
intPass = intPass + 1
else
ts.writeline("<td width='18%' bgcolor='#FFFFDC' valign='middle' align='center'><b><font color='yellow 'face='Tahoma' 'size='2'  >" & strPassFail & "</font></b></td>")
intFail = intFail + 1
end if
ts.writeline("</tr>")
End Function
' Function To Footer Info
Function Footer()
Dim EndTime
EndTime = Timer
enTime = Time
ts.writeline("</table>")
ts.writeline("<hr>")
ts.writeline("<table border='0' width='50%'>")
ts.writeline("<tr><td width='100%' colspan='2' bgcolor='#000000'><b><font face='Tahoma' size='2' color='#FFFFFF'>Summary</font></b></td></tr>")
ts.writeline("<tr><td width='45%' bgcolor='#E8FFE8'><b><font face='Tahoma' size='2'>Total Tests Passed</font></b></td><td width='55%' bgcolor='#E8FFE8'>" & intPass & "</td></tr>")
ts.writeline("<tr><td width='45%' bgcolor='#FFE6FF'><b><font face='Tahoma' size='2'>Total Tests Failed</font></b></td><td width='55%' bgcolor='#FFE6FF'>" & intFail & "</td></tr>")
ts.writeline("<tr><td width='45%' bgcolor='#FFFFDC'><b><font face='Tahoma' size='2'>Executed On</font></b></td><td width='55%' bgcolor= '#FFFFDC'>" & Date & "</td></tr>")
ts.writeline("<tr><td width='45%' bgcolor='#FFFFDC'><b><font face='Tahoma' size='2'>Start Time</font></b></td><td width='55%' bgcolor= '#FFFFDC'>" & stTime & "</td></tr>")
ts.writeline("<tr><td width='45%' bgcolor='#FFFFDC'><b><font face='Tahoma' size='2'>End Time</font></b></td><td width='55%' bgcolor= '#FFFFDC'>" & enTime & "</td></tr>")
ts.writeline("<tr><td width='45%' bgcolor='#FFFFDC'><b><font face='Tahoma' size='2'>Execution Time</font></b></td><td width='55%' bgcolor= '#FFFFDC'>" & CDbl(EndTime - StartTime) & "</td></tr>")
ts.writeline("</table>")
ts.writeline("</font>")
ts.WriteLine("</body>")
ts.WriteLine("</html>")
End Function
' Function To Close File
Function CloseFile()
Footer
ts.close()
Set objIE = CreateObject("InternetExplorer.Application")
objIE.visible = True
objIE.Navigate strFileURL
End Function

No comments:

Post a Comment