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

Wednesday, July 13, 2016

Update a Test Plan Field in ALM(QC)

 How to get the QC/ALM Field Name

In order to update a field in ALM using OTA you will need to know the backend name that ALM assigns to the field.
This name is normally different than the label name that you might see in the ALM Test Lab section.
If you don’t know what the actual field names are, you can easily find them by going into QC’s Tools>Customize.

In the Project Customization section and go into the “Project Entities” section.



 Under the Project Entities Tree view click expand Defect and click on your System Folder or User Fields. Clicking on a field will reveal the field name that you will need to use.

For this example I want to find the System Field > Status and get the name value for it (TS_STATUS)


QTP OTA Code to Update a Test Plan Field in an ALM/QC Test Lab

The following example updates the ‘Test Plan’ that has the ‘Test Set ID’ of 6 and changes the ‘Status’ field to Ready.






Set tdc = createobject("TDApiOle80.TDConnection")
tdc.InitConnectionEx "http://yourURL/qcbin"
tdc.Login "yourName","yourPassword"
tdc.Connect "yourDomain","yourProject"

'Update Status Field
Set TestList = tdc.TestFactory
Set TestPlanFilter = TestList.Filter
Set TestPlanList = TestList.NewList("")
For each tpTest in TestPlanList
 Set myTestPlan = TestPlanList.Item(tpTest.ID)
 myTestPlan.Field("TS_STATUS") = "Ready"
 myTestPlan.Post
Next

Set TestPlanFilter = Nothing
Set myTestPlan = Nothing
Set TestList = Nothing
Set TestPlanFilter = Nothing

Release Locked User from ALM(QC)

'Release Locked QTP Tests from QC
Set QCConnection=QCUtil.QCConnection
Set con=QCConnection.command
con.CommandText="DELETE FROM LOCKS WHERE LK_USER = 'USERID' "
Set recset=con.execute
'Mention your QC USERID in above code

Monday, July 11, 2016

Delete Previous Run Results from a Test Set - ALM(QC)

' Input - TestSet Name in QC
sFolder = "Root\Automation"
sTestSetName = "TestSet1"

'Variable declaration
Dim oTestDirObj
Dim sQCServer
sQCServer = "http://xxxxxx/qcbin"
sQcUser = "UserName"
sQcPassword = "Password"
sQcDomain = "Domain"
sQcProject = "ProjectName"

'Define the connection object
Set oTestDirObj = CreateObject("TDApiOle80.TDConnection")
oTestDirObj.InitConnectionEx sQCServer
oTestDirObj.Login sQcUser, sQcPassword
oTestDirObj.Connect sQcDomain, sQcProject

'Navigate to the folder containing the test set.
Set oTstFact = oTestDirObj.TestSetFactory
Set oTstMgr = oTestDirObj.TestSetTreeManager
Set oTestFolder = oTstMgr.NodeByPath(sFolder)

'selecting a particular testset in the folder
set oTstSetFilter = oTestFolder.TestSetFactory.Filter
set oTstList = oTstSetFilter.NewList()
For i = 1 To oTstList.Count
    If Trim(oTstList.Item(i).Name) = Trim(sTestSetName)Then
        Set oListTstSet = oTstList.Item(i)
        Exit For
    Else
'            MsgBox "Testset not found"    
    End If
Next

Set oListTstSetFilter = oListTstSet.TSTestFactory.Filter
Set oTestCase = oListTstSetFilter.NewList()
For testcasenum = 1 To oTestCase.count
    set oTstRunObj = oTestCase(testcasenum).RunFactory
    'Retrieve the run list object
    Set oRunFil = oTstRunObj.Filter
    Set oRunList = oRunFil.NewList()
    If oRunList.Count > 2 Or oRunList.Count = 2 Then
        For runnum = 1 To oRunList.Count-1
            Set oRunObj = oRunList.Item(runnum)
            oTstRunObj.RemoveItem oRunObj
            'Post results
            oRunObj.Post
        Next
    End If
Next

'Closing the TD connection
oTestDirObj.Disconnect
If oTestDirObj.LoggedIn Then
    oTestDirObj.Logout
End If

oTestDirObj.releaseConnection 

Update Test Steps in TestLab ALM (QC)

' Read data from excel add to dictionary
Set objTestRunResult = CreateObject("Scripting.Dictionary")

objTestRunResult.Add "Step 1"Array("Passed""Desc1""Expected1""Actual1")
objTestRunResult.Add "Step 2"Array("Passed""Desc2""Expected2""Actual2")
objTestRunResult.Add "Step 3"Array("Failed""Desc3""Expected3""Actual3")

'Input - TestSet Path
sFolder = "Root\Automation\Test"
sTestSetName = "test"
strTestName = "test"

' ALM Connection
    Dim oTestDirObj
    Dim sQCServer
    sQCServer = "http://XXXXX/qcbin"
    sQcUser = "XXXX"
    sQcPassword = "xxxxx"
    sQcDomain = "xxxx"
    sQcProject = "xxxxx"

'Define the connection object
    Set oTestDirObj = CreateObject("TDApiOle80.TDConnection")
    oTestDirObj.InitConnectionEx sQCServer
    oTestDirObj.Login sQcUser, sQcPassword
    oTestDirObj.Connect sQcDomain, sQcProject

'Navigate to the folder containing the test set.
'    Set oTestDirObj = QCUtil.QCConnection
    Set oTstFact = oTestDirObj.TestSetFactory
    Set oTstMgr = oTestDirObj.TestSetTreeManager
    Set oTestFolder = oTstMgr.NodeByPath(sFolder)

'selecting a particular testset in the folder
    set oTstSetFilter = oTestFolder.TestSetFactory.Filter
    set oTstList = oTstSetFilter.NewList()
    For i = 1 To oTstList.Count
        If Trim(oTstList.Item(i).Name) = Trim(sTestSetName)Then
            Set oListTstSet = oTstList.Item(i)
            Exit For
        Else
'            MsgBox "Testset not found"    
        End If
    Next

    Set oListTstSetFilter = oListTstSet.TSTestFactory.Filter
    Set oTestCase = oListTstSetFilter.NewList()
    For testcasenum = 1 To oTestCase.count
        If oTestCase.Item(testcasenum).Name = "[1]"& strTestName Then ' Test case Name            
            set oTstRunObj = oTestCase(testcasenum).RunFactory         
            strRun_Name = "Run_" & Year(Now()) & "_" & Month(Now())& "_" & Day(Now()) & "-" & Hour(Now()) & "_" &Minute(Now()) & "_" & Second(Now())
            Set objTestRun = oTstRunObj.AddItem(CStr(strRun_Name))
            objTestRun.CopyDesignSteps
            objTestRun.Post
        
            Set objTsTestStepFactory = objTestRun.StepFactory
       
            Dim newStep, iStep
            Dim objSteps
            strfailflg = False
         
            objSteps = objTestRunResult.Items         
            For iStep = 1 To objTestRunResult.Count Step 1             
                Set myStep = objTsTestStepFactory.NewList("")
                myStep.Item(iStep).Field("ST_STATUS") = objSteps(iStep-1)(0' "Passed"
                'myStep.Item(iStep).Field("ST_DESCRIPTION") = objSteps(iStep-1)(1)
               ' myStep.Item(iStep).Field("ST_EXPECTED") = objSteps(iStep-1)(2)
                myStep.Item(iStep).Field("ST_ACTUAL") = objSteps(iStep-1)(3)' "Actual Value"
                myStep.Item(iStep).Post                 
                'Check for Failed
                If objSteps(iStep-1)(0) = "Failed" Then
                    strfailflg = True
                End If
            Next
            ' Update Final Result
            If strfailflg Then
                objTestRun.Status = "Failed" ' Final Result
            Else
                objTestRun.Status = "Passed" ' Final Result
            End If
            objTestRun.Post     
        End If             
    Next