Tuesday, September 13, 2016

Create Customised UFT/QTP HTML Report Results

Dim iSNO, iTestCaseNumber, strTestNameWithTimeStamp,strNow,strResultFilePath
Dim micPass : micPass = "micPass"
Dim micFail : micFail = "micFail"
Dim micDone : micDone = "micDone"
Dim micWarning : micWarning = "micWarning"

    Public Function ReportEvent(ByVal Status, ByVal strStepName, ByVal strStepDetails, ByVal strScreenshotRequired)
        Dim objFso,objFolder,objFile,link,iCurrentTime,strResultFolderPath,strFileContent
        Dim strStepResult,strScreenshotPath,strBase64Content,strScreenshotInfo
        Set objFso = CreateObject("Scripting.FileSystemObject")
        strResultFolderPath = Environment("ResultDir")
        If Len(strTestNameWithTimeStamp) = 0 Then
            strTestNameWithTimeStamp = fnGenerateFolderName
        End If
        If not objFso.FolderExists(strResultFolderPath) Then
            Set objFolder=objFso.CreateFolder(strResultFolderPath)
        End If
        iCurrentTime = Now
        strResultFilePath = strResultFolderPath &"\"& strTestNameWithTimeStamp &".html"
        Print strResultFilePath
        If NOT(objFso.FileExists(strResultFilePath)) Then
            iSNO = 1
            Set  objFile = objFso.CreateTextFile(strResultFilePath,truefalse)
            objFile.writeline "<html>" & VBNewLine
            objFile.writeline "<head>" & VBNewLine
            objFile.writeline "<style type=""text/css"">.passed{display: table-row; background-color: #E1E1E1; border: 1px solid #4D7C7B; color: #000000; font-size: 0.75em;" & VBNewLine
            objFile.writeline "td,th { padding: 5px; border: 1px solid #4D7C7B; text-align: inherit /; } "  & VBNewLine
            objFile.writeline "th.Logos { padding: 5px; border: 0px solid #4D7C7B; text-align: inherit /;}</style>"  & VBNewLine
            objFile.writeline "<style type=""text/css"">.failed{display: table-row;background-color: #FFFFFF; color: #000000; "  & VBNewLine
            objFile.writeline "font-size: 0.75em; display: table-row;} </style>"VBNewLine
            objFile.writeline "<style type=""text/css"">.notvisible{display: None; </style><meta charset='UTF-8'> <title>Detailed Results Report</title>"VBNewLine
            objFile.writeline "<style type='text/css'>body { background-color: #FFFFFF; font-family: Verdana, Geneva, sans-serif; text-align: center; }"VBNewLine
            objFile.writeline "small { font-size: 0.75em; } table { box-shadow: 9px 9px 10px 4px #BDBDBD;border: 0px solid #4D7C7B; border-collapse: "VBNewLine
            objFile.writeline "collapse; border-spacing: 0px; width: 1000px; margin-left: auto; margin-right: auto; } tr.heading { background-"VBNewLine
            objFile.writeline "color: #041944;color: #FFFFFF; font-size: 0.75em; font-weight: bold; background:-o-linear-gradient"VBNewLine
            objFile.writeline "(bottom, #999999 5%, #000000 100%);background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #999999)"VBNewLine
            objFile.writeline ", color-stop(1, #000000));background:-moz-linear-gradient( center top, #999999 5%, #000000 100%);"VBNewLine
            objFile.writeline "filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#999999, endColorstr=#000000);"VBNewLine
            objFile.writeline "background: -o-linear-gradient(top,#999999,000000);} tr.subheading { background-color: #FFFFFF; color: #000000;"VBNewLine
            objFile.writeline "font-weight: bold; font-size: 0.75em; text-align: justify; } tr.section { background-color: #A4A4A4; color: #333300; "VBNewLine
            objFile.writeline "cursor: pointer; font-weight: bold; font-size: 0.75em; text-align: justify; background:-o-linear-gradient"VBNewLine
            objFile.writeline "(bottom, #56aaff 5%, #e5e5e5 100%); background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #56aaff),"VBNewLine
            objFile.writeline "color-stop(1, #e5e5e5));background:-moz-linear-gradient( center top, #56aaff 5%, #e5e5e5 100%);filter:progid:"VBNewLine
            objFile.writeline "DXImageTransform.Microsoft.gradient(startColorstr=#56aaff, endColorstr=#e5e5e5); background: -o-linear-gradient(top,#56aaff,e5e5e5);}"VBNewLine
            objFile.writeline "tr.subsection { cursor: pointer; } th { padding: 5px; border: 1px solid #4D7C7B; } "VBNewLine
            objFile.writeline "th.Logos { padding: 5px; border: 0px solid #4D7C7B; text-align: inherit /;}"VBNewLine
            objFile.writeline "td { padding: 5px; border: 1px solid #4D7C7B;align:center;}td.pass {font-weight: bold; color: green; text-align:center;}"VBNewLine
            objFile.writeline "td.fail {font-weight: bold; color: red; text-align:center;}</style></head>"VBNewLine
            objFile.writeline "<body></br><table id='Logos'> <colgroup><col style='width: 25%' /><col style='width: 25%' /><col style='width: 25%' /><col style='width: 25%' />"VBNewLine
            objFile.writeline "</colgroup></table><table id='header'> <colgroup> <col style='width: 25%' /> <col style='width: 25%' /> <col style='width: 25%' /> <col style='width: 25%' /> "VBNewLine
            objFile.writeline "</colgroup><thead><tr class='heading'> <th colspan='4' style='font-family:Copperplate Gothic Bold; font-size:1.4em;'> ** "&Environment("TestName") &" **</th>"VBNewLine
            objFile.writeline "</tr> <tr class='subheading'><th>&nbsp;Date&nbsp;&&nbsp;Time&nbsp;:&nbsp;</th>"
            objFile.writeline "<th>"&DatePart("d", iCurrentTime) & "-" & MonthName(Month(iCurrentTime), True) & "-" & DatePart("yyyy", iCurrentTime) & Space(1) & Hour(iCurrentTime) & ":" & Minute(iCurrentTime) & "</th>" & VBNewLine
            objFile.writeline "<th>&nbsp;Operating&nbsp;System&nbsp;:&nbsp;</th>"VBNewLine
            objFile.writeline "<th> " &Environment("OS") & "</th> </tr> <tr class='subheading'><th>&nbsp;Executed&nbsp;By&nbsp;:&nbsp;</th><th> " & Environment("UserName") & "</th> <th>&nbsp;Executed&nbsp;on&nbsp;:&nbsp;</th>"VBNewLine
            objFile.writeline "<th>" & Environment("LocalHostName") & "</th> </tr> </thead></table><table id='main' cellpadding=""0"" cellspacing=""0""> <Head>" & VBNewLine
            objFile.writeline  "<Body>" & VBNewLine
            objFile.writeline  "<colgroup>" & VBNewLine
            objFile.writeline  "<col style='width: 5%' /> <col style='width: 26%' /> <col style='width: 51%' /> " & VBNewLine
            objFile.writeline  "<col style='width: 8%' /> <col style='width: 10%' />" & VBNewLine
            objFile.writeline  "</colgroup>"
            objFile.writeline  "<thead>"
            objFile.writeline  "<tr class='heading'>"
            objFile.writeline  "<th>S.No</th> "
            objFile.writeline  "<th>Step"
            objFile.writeline  "</th> "
            objFile.writeline  "<th>Details"
            objFile.writeline  "</th> "
            objFile.writeline  "<th> Status"          
            objFile.writeline  "</th>"
            objFile.writeline  "<th>Time</th>"
            objFile.writeline  "</tr> "
            objFile.WriteBlankLines(5)
            objFile.writeline  "<script type=""text/javascript"">" & VBNewLine
            objFile.writeline  "function filter()" & VBNewLine
            objFile.writeline  "{" & VBNewLine
            objFile.writeline  "if(document.getElementById(""filter"").value==""passed"")" & VBNewLine
            objFile.writeline  "{" & VBNewLine
            objFile.writeline  "document.getElementsByTagName(""style"")[0].textContent = "".passed{display: table-row;background-color: #E1E1E1; border: 1px solid #4D7C7B; color: #000000; font-size: 0.75em;}"";" & VBNewLine
            objFile.writeline  "document.getElementsByTagName(""style"")[1].textContent = "".failed{display: none;}"";" & VBNewLine
            objFile.writeline  "}" & VBNewLine
            objFile.writeline  "else if (document.getElementById(""filter"").value==""failed"")" & VBNewLine
            objFile.writeline  "{" & VBNewLine
            objFile.writeline  "document.getElementsByTagName(""style"")[1].textContent = "".failed{display: table-row;background-color: #FFFFFF;color: #000000; font-size: 0.75em; display: table-row;}"";" & VBNewLine
            objFile.writeline  "document.getElementsByTagName(""style"")[0].textContent = "".passed{display: none;}"";" & VBNewLine
            objFile.writeline  "}" & VBNewLine
            objFile.writeline  "else" & VBNewLine
            objFile.writeline  "{" & VBNewLine
            objFile.writeline  "document.getElementsByTagName(""style"")[0].textContent = "".passed{display: table-row;background-color: #E1E1E1; border: 1px solid #4D7C7B; color: #000000; font-size: 0.75em;}"";" & VBNewLine
            objFile.writeline  "document.getElementsByTagName(""style"")[1].textContent = "".failed{display: table-row;background-color: #FFFFFF;color: #000000; font-size: 0.75em; display: table-row;}"";" & VBNewLine
            objFile.writeline  "}" & VBNewLine
            objFile.writeline  "}" & VBNewLine
            objFile.writeline  "</script>" & VBNewLine
            objFile.writeline  "<script type=""text/javascript"">"
            objFile.writeline  "function filterStatus()"
            objFile.writeline  "{"
            objFile.writeline  "searchtext = (document.getElementById(""txtStepValue"").value).toLowerCase();"
            objFile.writeline  "if(searchtext!="""")"
            objFile.writeline  "{"
            objFile.writeline  "var rowIndex = 0; // rowindex, in this case the first row of your table"
            objFile.writeline  "var table = document.getElementById('main'); // table to perform search on"
            objFile.writeline  "var row = table.getElementsByTagName(""tr"");"
            objFile.writeline  "irowcount = row.length"
            objFile.writeline  "for (i = 1; i < row.length; i++) {"
            objFile.writeline  "status = (row[i].getElementsByTagName(""td"")[1].textContent).toLowerCase();"
            objFile.writeline  "if (status.indexOf(searchtext) == -1) "
            objFile.writeline  "{"
            objFile.writeline  "row[i].className = 'content notvisible'"
            objFile.writeline  "}}}"
            objFile.writeline  "else {"
            objFile.writeline  "window.location.reload()"
            objFile.writeline  "}}"
            objFile.writeline  "</script>"
            objFile.writeline  "<script type=""text/javascript"">"
            objFile.writeline  "function filterDetails()"
            objFile.writeline  "{"
            objFile.writeline  "searchtext = (document.getElementById(""txtDetailsValue"").value).toLowerCase();"
            objFile.writeline  "if(searchtext!="""")"
            objFile.writeline  "{"
            objFile.writeline  "var rowIndex = 0; // rowindex, in this case the first row of your table"
            objFile.writeline  "var table = document.getElementById('main'); // table to perform search on"
            objFile.writeline  "var row = table.getElementsByTagName(""tr"");"
            objFile.writeline  "for (i = 1; i < row.length; i++) {"
            objFile.writeline  "Details = (row[i].getElementsByTagName(""td"")[2].textContent).toLowerCase();;"
            objFile.writeline  "if (Details.indexOf(searchtext) == -1) "
            objFile.writeline  "{"
            objFile.writeline  "row[i].className = 'content notvisible'"
            objFile.writeline  "}}}"
            objFile.writeline  "else {"
            objFile.writeline  "window.location.reload()"
            objFile.writeline  "}}"
            objFile.writeline  "</script>"
            objFile.writeline "<script type=""text/javascript"">"
            objFile.writeline "function BlankStatus()"
            objFile.writeline "{"
            objFile.writeline "document.getElementById(""txtStepValue"").value = """";"
            objFile.writeline "}"
            objFile.writeline "</script>"
            objFile.writeline "<script type=""text/javascript"">"
            objFile.writeline "function BlankDetails()"
            objFile.writeline "{"
            objFile.writeline "document.getElementById(""txtDetailsValue"").value = """";"
            objFile.writeline "}"
            objFile.writeline "</script>"
        Else
            Set objFile=objFso.OpenTextFile(strResultFilePath, 1,TRUE)
            strFileContent = Split(objFile.ReadAll,"<!--Screenshots-->")
            Set objFile = Nothing
            Set objFile=objFso.OpenTextFile(strResultFilePath, 8,TRUE)
                      
        End If
      ''on Error Resume Next
        Err.Clear
        Status = Trim(Status)
        Select Case Ucase(Status)
            Case "MICFAIL","FAIL"
                Reporter.ReportEvent micFail , strStepName , strStepDetails
                iCurrentTime = Now
                Set objFile = Nothing
                Set objFile=objFso.OpenTextFile(strResultFilePath, 1,TRUE)
                strFileContent = Split(objFile.ReadAll,"<!--Screenshots-->")

                    strScreenshotPath = fnCaptureImage(strResultFolderPath)
                    Wait 1
                    strBase64Content = fnGetBase64Image(strScreenshotPath)
                    If Ubound(strFileContent) = 0 Then
                        strScreenshotInfo = ""
                    Else
                        strScreenshotInfo = strFileContent(1)  
                    End If
                    strStepResult = "<tr class='content failed' ><td><a id=R"& iSNO &" href =#"&iSNO&">"&iSNO&"</a></td><td class='justified'>" & strStepName &"</td><td class='justified'>" & strStepDetails & "</td>" & _
                                    "<td class='fail'>FAIL</td><td><small>" & DatePart("d", iCurrentTime) & "-" & MonthName(Month(iCurrentTime), True) & "-" & DatePart("yyyy", iCurrentTime) & Space(1) & Hour(iCurrentTime) & _
                                    ":" & Minute(iCurrentTime) & ":" & Second(iCurrentTime)& "</small></td> </tr>"VBNewLine & "<!--Screenshots--></table>"&strScreenshotInfo&"<p><br><center><img id="&iSNO&" width=80%  src=""data:image/png;base64," & _
                                    strBase64Content & """" & " </center><br><a href=#R"&iSNO&">Go Back</a><p><br><center><img id="&iSNO&" width=80%  "
                If iSNO = 1 Then
                    Set objFile = Nothing
                    Set objFile=objFso.OpenTextFile(strResultFilePath, 8,TRUE)
                    objFile.Writeline strStepResult
                Else
                    strStepResult = strFileContent(0) & strStepResult
                    Set objFile = Nothing
                    Set objFile=objFso.OpenTextFile(strResultFilePath, 2,TRUE)
                    objFile.Write strStepResult
                End If          
            Case "MICPASS","PASS"
                Reporter.ReportEvent micPass , strStepName , strStepDetails
                iCurrentTime = Now
                Set objFile = Nothing
                Set objFile=objFso.OpenTextFile(strResultFilePath, 1,TRUE)
                strFileContent = Split(objFile.ReadAll,"<!--Screenshots-->")
                If Trim(Ucase(strScreenshotRequired)) = "YES"  Then
                    strScreenshotPath = fnCaptureImage(strResultFolderPath)
                    Wait 1
                    strBase64Content = fnGetBase64Image(strScreenshotPath)
                    If Ubound(strFileContent) = 0 Then
                        strScreenshotInfo = ""
                    Else
                        strScreenshotInfo = strFileContent(1)  
                    End If
                    strStepResult = "<tr class='content passed' ><td><a id=R"& iSNO &" href =#"&iSNO&">"&iSNO&"</a></td><td class='justified'>" & strStepName &"</td><td class='justified'>" & strStepDetails & "</td>" & _
                                    "<td class='pass'>PASS</td><td><small>" & DatePart("d", iCurrentTime) & "-" & MonthName(Month(iCurrentTime), True) & "-" & DatePart("yyyy", iCurrentTime) & Space(1) & Hour(iCurrentTime) & _
                                    ":" & Minute(iCurrentTime) & ":" & Second(iCurrentTime)& "</small></td> </tr>"VBNewLine & "<!--Screenshots--></table>"&strScreenshotInfo&"<p><br><center><img id="&iSNO&" width=80%  src=""data:image/png;base64," & _
                                    strBase64Content & """" & " </center><br><a href=#R"&iSNO&">Go Back</a><p><br><center><img id="&iSNO&" width=80%  "
                Else
                    strStepResult = "<tr class='content passed' ><td>" & iSNO & "</td> <td class='justified'>" & strStepName &"</td><td class='justified'>" & strStepDetails & "</td>" & _
                "<td class='pass'>PASS</td><td><small>" & DatePart("d", iCurrentTime) & "-" & MonthName(Month(iCurrentTime), True) & "-" & DatePart("yyyy", iCurrentTime) & Space(1) & Hour(iCurrentTime) & ":" & Minute(iCurrentTime) & ":" & Second(iCurrentTime)& "</small></td> </tr>"VBNewLine & "<!--Screenshots--></table>"
                End If
                If iSNO = 1 Then
                    Set objFile = Nothing
                    Set objFile=objFso.OpenTextFile(strResultFilePath, 8,TRUE)
                    objFile.Writeline strStepResult
                Else
                    strStepResult = strFileContent(0) & strStepResult
                    Set objFile = Nothing
                    Set objFile=objFso.OpenTextFile(strResultFilePath, 2,TRUE)
                    objFile.Write strStepResult
                End If  
          
            Case "MICDONE","DONE"
                Reporter.ReportEvent micDone , strStepName , strStepDetails
            Case "MICWARNING","WARNING"
                Reporter.ReportEvent micWarning , strStepName , strStepDetails
        End Select
        iSNO = iSNO+1
    End Function

Function fnGetBase64Image(strFileName)
   Dim objStream, objXML, objDocElem
    Set objStream = CreateObject("ADODB.Stream")
    objStream.Type = 1
    objStream.Open
    objStream.LoadFromFile (strFileName)
    Set objXML = CreateObject("MSXml2.DOMDocument")
    Set objDocElem = objXML.createElement("Base64Data")
    objDocElem.DataType = "bin.base64"
    objDocElem.nodeTypedValue = objStream.Read()
  
    fnGetBase64Image = objDocElem.Text
End Function


Public function fnCaptureImage(ByVal strPath)

        Dim sResultPath, strTime, sImagePath

        sResultPath = Environment.Value("ResultDir") & "\"

       'Get Current Date and Time
        strTime = "Screenshot_"Year(Now()) & Month(now) & Day(Now()) & "_" & Hour(Now) & Minute(Now()) & Second(Now())
        sImagePath = strPath & "\" & strTime & ".png"
        Print sImagePath
        Reporter.Filter = rfDisableAll
        Desktop.CaptureBitmap sImagePath,True
        Reporter.Filter = rfEnableAll
  
        fnCaptureImage = sImagePath

End Function


Public Function fnGenerateFolderName
    Dim strNow,strTestNameWithTimeStamp
    strNow = Now
    fnGenerateFolderName = Environment("TestName")&"_"&Hour(strNow)&"."&Minute(strNow)&"."&Second(strNow)&","&Day(strNow)&"-"&Month(strNow)&"-"&Year(strNow)
End Function


Public Function AttachFileToCurrentTestSetTest(strFiletoattach) 
        Dim objFSO,blnStatus,o_CurrentRun,CTA,att1
        Set objFSO  = CreateObject("Scripting.FileSystemObject")
        blnStatus=objFSO.FileExists(strFiletoattach)
        If blnStatus=False Then
                        AttachFileToCurrentTestSetTest ="Failed to attach file because file does not exist"
                        Set objFSO = Nothing
                        Exit Function
        End If
        Set o_CurrentRun=QCUtil.CurrentRun
        ' Check that we are running this test from QC, otherwise we can exit
        If (o_CurrentRun Is NothingThen
                        Exit Function
        End If
        Set CTA=o_CurrentRun.Attachments
        Set att1 = CTA.AddItem(null)
        att1.fileName=strFiletoattach
        att1.type=1
        att1.post
        att1.save False
        strResultFilePath = ""
        strTestNameWithTimeStamp = ""
 End Function

No comments:

Post a Comment