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
         

Attach File to Current Test Run in ALM (QC)

' Input File Path
strFiletoattach = "C:\Automation\file.xls" ' File type can be any.i.e., .xls,.png,.html etc

Set o_CurrentRun=QCUtil.CurrentRun
Set CTA=o_CurrentRun.Attachments
Set att1 = CTA.AddItem(null)
att1.fileName=strFiletoattach
att1.type=1
att1.post
att1.save False

Saturday, July 9, 2016

Attach files to ALM(QC) TestPlan folder

' Input - QC Folder Path and Local Folder Path
strQCFolderPath= “Subject\Automatin\TestCases”
strUpladAttachmentsLocation= “C:\QCFiles”

' QC Connection to TestPlan Folder ------ Refer 'ALM Quality Center Connectivity' post
Set treeMgr = gTDConn.TreeManager
Set tsFolder = treeMgr.NodeByPath(strQCFolderPath)
Set tsList = tsFolder.Attachments

'Read all files    
Set objfso = CreateObject("Scripting.FileSystemObject")
Set objfolder = objfso.GetFolder(strUpladAttachmentsLocation)
Set objfiles = objfolder.Files

'Attach files one by one to QC folder     
For Each afile In objfiles
 UpladFileName = afile.Name                    
 Set otaAttachmentList = tsList.AddItem(Null)         
 otaAttachmentList.FileName = strUpladAttachmentsLocation & "\" & UpladFileName
 otaAttachmentList.Type = 1
 otaAttachmentList.Post
 otaAttachmentList.Save False
 Set otaAttachmentList = Nothing
Next   

Download UFT(QTP) Scripts from ALM(QC) TestPlan Folder

' Input - QC Folder Path and Local Folder Path
strQCAttachmentsPath = “Subject\Automatin\TestCases”
strDownloadLocation = “C:\QCFiles”

' QC Connection to TestPlan Folder ------ Refer 'ALM Quality Center Connectivity' post
Set treeMgr = gTDConn.TreeManager
Set SubjRoot = treeMgr.NodeByPath(strQCFolderPath)

Set tstFactory = SubjRoot .TestFactory
Set tsetList = tstFactory.NewList("")

'Download all scripts to local folder      
For k=1 to tsetList.Count
 Set TestStorage = tsetList(k).ExtendedStorage
 DwnladFileName = Trim(tsetList(k).Name)
 TestStorage.ClientPath = strDownloadLocation & "\" &  DwnladFileName
 TestDownLoadPath =TestStorage.LoadEx(""TrueNothingFalse)
Next
    ocation & “\” & DwnladFileName

Next

Friday, July 8, 2016

Download Attachments from ALM(QC) TestPlan Folder


' Input - QC Folder Path and Local Folder Path
strQCAttachmentsPath = “Subject\Automatin\TestCases”
strDownloadLocation = “C:\QCFiles”

' QC Connection to TestPlan Folder ------ Refer 'ALM Quality Center Connectivity' post
Set treeMgr = gTDConn.TreeManager
Set SubjRoot = treeMgr.NodeByPath(strQCAttachmentsPath)
Set otaAttachmentFactory = subjRoot.Attachments

Set fso = CreateObject("Scripting.FileSystemObject")

'Download Attachments and Copy to local folder
For i = 1 to otaAttachmentList.Count   

 Set otaAttachmentFilter = otaAttachmentFactory.Filter
 otaAttachmentFilter.Filter("CR_REFERENCE") = "'ALL_LISTS_" & SubjRoot.NodeID & "_*'"
 Set otaAttachmentList = otaAttachmentFilter.NewList

 Set otaAttachment = otaAttachmentList.Item(i)   
 otaAttachment.Load True""
 QCFileName = split(strFile,"ALL_LISTS_" & subjRoot.NodeID & "_")       
 DwnladFileName = QCFileName(1)   

'Copy downloaded file to local folder
 fso.CopyFile otaAttachment.FileName, strDownloadLocation & “\” & DwnladFileName

Next

ALM Quality Center Connectivity

' ALM QC URL
qcServer = "http://" & qcHostName& "/qcbin"

' Create QC Object
 Set gTDConn = CreateObject("tdapiole80.tdconnection")

' Verify for QC object connection status
 If (gTDConn Is NothingThen
  MsgBox "gTDConn object is empty"
 End If

 gTDConn.InitConnectionEx qcServer  ' Connect to QC server
 gTDConn.Login qcUser, qcPassword   ' QC Username, Password
 gTDConn.Connect qcDomain, qcProject ' Project, Domain