Sunday, 28 June 2015

Pulling Defects From QC ALM Using OTA API

        Hi everyone! In my last post I have shown you to Connect to QC Using OTA API. Today we use the TDConnection Object to connect to QC and do something more. I was confused thinking about which of the topics to cover after we have established the connection to QC.  I wanted to portray how simply and easily we can use OTA without getting into a lot of coding jargon, yet giving a good glimpse of how OTA API worked. So finally I decided to show how to pull defects from QC.
        Now, before I get into the code, you need to know that everything(Test Plan,Test Set,Bugs) is handled in QC as a Factory, which is but a collection of Objects. For example, we have the TestSetFactory, TestFactory, BugFactory, etc about which I'll talk about in later posts.
        Each item in the factory can be accessed directly via Item Property of the Factory Object. For example - FactoryObject.Item([SomePrimaryKeyID]). However for easy manipulation, what we usually do is get a List of Objects from the Factory Object. There is also an option of using a Filter for populating the list. For a Filter Object, we set the filter on a field name and provide the desired value. However if we do not want to filter out our results, we can easily use a null string when we populate the list. Needless to say if no filter is required, there's absolutely no need to create a Filter Object.

        Here is the code:

Sub GetOpenDefects()

    Dim msgStr as String
    Dim TDConn as TDAPIOLELib.TDConnection
    
    Set TDConn = CreateObject("TDApiOle80.TDConnection")
    
    TDConn.InitConnectionEx [QCbinpath]
    
    TDConn.Login [UserName], [Password]
    
    If TDConn.LoggedIn Then
    
        TDConn.Connect [Domain], [Project]
    
        If TDConn.Connected
            Dim BugFact as TDAPIOLELib.BugFactory
            Dim BugFilter as TDAPIOLELib.Filter
            Dim BugList as TDAPIOLELib.List
            
            'Get the BugFactory 
            Set BugFact = TDConn.BugFactory
            'Get the Filter for the connection
            Set BugFilter = TDConn.Filter

            'Set the filter to the field
            BugFilter.Filter("BG_STATUS") = "Open"

            'Get/Populate a list of all the defects with status Open
            Set BugList = BugFact.NewList(BugFilter.Text) 

            'To Get a list of all the defects we can use null string
            'Set BugList = BugFact.NewList("") 

            'Iterate through all the defects.
            For Each aBug In BugList
            'Get a specified set of fields.
            msgStr = " Defect ID : " & aBug.Field("BG_BUG_ID") & vbNewLine &
                 "Summary : " & aBug.Field("BG_SUMMARY") & vbNewLine & 
                 "Detected By" & aBug.Field("BG_DETECTED_BY") & vbNewLine &
                 "Detected on Date" & aBug.Field("BG_DETECTION_DATE") & vbNewLine &
                 "Status" & aBug.Field("BG_STATUS") & vbNewLine &
                 "Subject" & aBug.Field("BG_SUBJECT") & vbNewLine &
                 "Severity" & aBug.Field("BG_SEVERITY") & vbNewLine &
                 "Priority" & aBug.Priority & vbNewLine &
                 "Assigned To" & aBug.Field("BG_RESPONSIBLE")
            MsgBox(msgStr)
            'Send a Mail if you would like
            TDConn.SendMail [Mail To], [Mail From], [Mail Subject], msgStr, NULL , "HTML"
            Next
        Else: MsgBox("Failed to Connect to Project!")
        End If
    Else : MsgBox("Login Failed!")
    End If
    TDConn.Logout
    TDConn.ReleaseConnection
    TDConn = Nothing
End Sub

        So here we have the code to get defects from QC. However I have not added ant error handler this time. Instead I have used if-else to handle the errors. That's not a good way I know, but it works! One thing to be noted is that, the items in the Lists are also Objects and each Object has certain fields. Here I have accessed most of the fields of a Defect by the field-names in the QC Database. However, almost all of them can be accessed as reference to the Bug Object. For example:

  1. aBug.Field("BG_BUG_ID")  is same as  aBug.ID,
  2. aBug.Field("BG_SUMMARY") is same as aBug.Summary,
  3. aBug.Field("BG_DECTECTED_BY") is same as aBug.DetectedBy, etc.
        However, not all fields you can see in the UI can be accessed similarly. Once a HP QC ALM is licensed and installed, the licensee has the right to add Custom Fields to the existing databases in ALM. These custom new fields will be referenced as BG_USER_XX for the Bug Object, where XX stands for numbers like BG_USER_01. In such situations, you will need to use the Field property of the Bug Object in order to retrieve the value of the field.
        So you have a brand new code to play with. Go ahead and explore new possibilities and when you do, keep me posted!
Thanks for visiting! For queries/bugs/thoughts on the matter, please leave a comment below.


No comments :

Post a Comment