31.5.14

How to work with QC OTA - Example 3 - Function to Run a Query on QC Database

'------------------------------------
' Function Name:         funcRunQueryOnOC
' Description:               This function will run a query on the QC database and then return the recordset containing the results
' Input Parameters:     sQCQuery - The query to be run on the QC database
' Output Parameters:  The QC RecordSet Object
' Author:                      Ashish Jaiswal
'------------------------------------

Function funcRunQueryOnOC(sQCQuery)

    'Getting the QC Connection Object. Refer earlier post about the implementation of this function.
    Set oALMConnObj = funcGetALMConnectionObj (sQCServer, sQCUsername, sQCPassword, sQCDomain, sQCProject)

    'Getting the Command object to run the query
    Set oQCCommand = oALMConnObj.Command

    oQCCommand.CommandText = sQCQuery
    'Sample query: "Select * from Bug where BG_BUG_ID = 9295"

    Set oQCRecordSet = oQCCommand.Execute

    'Now returning the recordset
    Set funcRunQueryOnOC = oQCRecordSet

End Function 
'------------------------------------

How to work with QC OTA - Example 2 - Get a List of All Defects in QC

Code to get a list of all the details for all the defects in QC Defects Module


''Complete path of the file where the info from QC needs to be written
sQCLogFilePath = "C:\Automation\Defects_List.csv"

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.CreateTextFile(sQCLogFilePath)

''Getting the QC Connection Object. Refer earlier post about the implementation of this function
Set oALMConnObj = funcGetALMConnectionObj (sQCServer, sQCUsername, sQCPassword, sQCDomain, sQCProject)

''Getting the Bug Factory object
Set oBugFactory = oALMConnObj.BugFactory

Set oBugList = oBugFactory.NewList("")

oFile.WriteLine("Defect ID" & "," & "Summary")

For Each oBug In oBugList

oFile.WriteLine(oBug.ID & "," & oBug.Summary)
''We can add other fields as: Bug.Status, Bug.Priority, Bug.AssignedTo

Next

Set oFile = Nothing
Set oFSO = Nothing
Set oBugFactory = Nothing
Set oALMConnObj = Nothing

How to work with QC OTA - Example 1 - Get Defect ID and the size of Attachments in each defect

More often than not, we get weird requests, like getting a list of all the defects in QC and the size of attachments for each of them, but thankfully we have QC OTA to help us ease the pain. Use the below code to get a file with Defect ID and the size of all the attachments for them.


''Complete path of the file where the info from QC needs to be written
sQCLogFilePath = "C:\Automation\Attachments_List.csv"

Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.CreateTextFile(sQCLogFilePath)

''Getting the QC Connection Object. Refer earlier post about the implementation of this function
Set oALMConnObj = funcGetALMConnectionObj (sQCServer, sQCUsername, sQCPassword, sQCDomain, sQCProject)

''Getting the Bug Factory object
Set oBugFactory = oALMConnObj.BugFactory

''Defining the filters for the Defects
Set oBugFilter = oBugFactory.Filter
oBugFilter.Filter("BG_STATUS") = "Closed"
oBugFilter.Filter("BG_ATTACHMENT") = "Y"
oBugFilter.Filter("BG_DETECTED_BY") = "ASHISH.JAISWAL"

Set oBugList = oBugFilter.NewList

oFile.WriteLine("Defect ID" & "," & "Total Attachment Size")

For Each oBug In oBugList

        Set oBugAttachments = oBug.Attachments
        Set oBugAttachmentList = oBugAttachments.NewList("")

        iTotalAttachmentSize = 0
        For Each oBugAttachment In oBugAttachmentList
            iTotalAttachmentSize = iTotalAttachmentSize + oBugAttachment.FileSize
        Next

        oFile.WriteLine(oBug.ID & "," & iTotalAttachmentSize)

        Set oBugAttachmentList = Nothing
        Set oBugAttachments = Nothing

Next

Set oFile = Nothing
Set oFSO = Nothing
Set oBugFilter = Nothing
Set oBugFactory = Nothing
Set oALMConnObj = Nothing

19.5.14

Convert QTP Results XML to HTML

QTP generate it's results in an XML format. We can use the inbuilt result convertor to convert these into any format we want, but for that we need to have the RunResultsViewer installed on the system. But we can also convert these results into an HTML file manually. In order to convert this XML to any other format we need to use the XSL style sheet to define the conversion.
XSL is a Stylesheet language which can be used to transform an XML according to the specification. The output could be a HTML file, a text file, a XML file etc...To do this transformation at run-time we need to load the XML, and then the XSL into that, and save the output.

QTP comes with 3 different XSL files which would be located in the following folder - "C:\Program Files (x86)\HP\QuickTest Professional\dat\"

    PDetails.xsl - for getting the detailed results in HTML format
    PShort.xsl - for getting the short results in HTML format
    PSelection.xsl - for getting the selective results in HTML format

The PSelection.xsl file requires input arguments for the XSL and the other 2 can be directly loaded into the XML.

We can either refer these XSL files from the default directory [dat] or copy them to any folder we like, and then reference from there. Also, in order for the results to be displayed correctly the PResults.css [also located in the dat folder] file should be present in the same folder as the output HTML file. Hence, in order to avoid creating all the new result HTML files in the dat folder itself, its better to copy all the required files to a different directory and then work off of that one.

Below is the function that would do this conversion.


'''--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'' Function Name:         funcConvertResultsXMLtoHTML
'' Description:         This function will convert the QTP Results.xml into HTML format, with Detailed or Short versions.
''                         This can be called from within QTP, or an external VbScript
'' Input Parameters:     sInputResultsXML - The path of the QTP Results.xml
''                         sXSLType - The XSL type, based on which the results will be converted to either Detailed or Short version
''                         sOutputHTMLPath - The complete path where the HTML result file needs to be created
'' Output Parameters:     None
'' Author:                 Ashish Jaiswal
'''--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Function funcConvertResultsXMLtoHTML (sInputResultsXML, sXSLType, sOutputHTMLPath)

    ''Creating an instance for the DOMDocument Class of the MS XML Parser [MSXML.dll]
    Set oXMLDoc = CreateObject("MSXML.DOMDocument")
    Set oXSLDoc = CreateObject("MSXML.DOMDocument")

    ''Since a file is loaded asynchronously by default by the parser, we set it to false
    ''By setting the document's Async property to False, the parser will not return control to your code until the document is completely loaded and ready for manipulation. If you leave it set to True, you will need to either examine the ReadyState property before accessing the document or use the DOMDocument's events to have your code notified when the document is ready. Using the default value may cause issues during reading and writing to file, hence, its best to keep things simple, and let it be False.
    oXMLDoc.ASync = False
    oXSLDoc.ASync = False

    ''Opening the document via the Load method, by specifying the path of the input XML and XSL
    ''The XML parser can load XMLs from the local disk, over the network using the UNC path, or via a URL
    oXSLDoc.Load sXSLType
    oXMLDoc.Load sInputResultsXML

    ''Processing the entire XML (it processes all the nodes and the children) using the specified XSL Style Sheet, and then returning the transformation
    sTransformedXML = oXMLDoc.transformNode(oXSLDoc.documentElement)

    ''Creating a new file to hold the transformed XML data - the output HTML file, and enabling overwriting the file with True
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oOutputHTML = oFSO.CreateTextFile(sOutputHTMLPath,True)

    ''Writing the transformed text to the HTML file
    oOutputHTML.Write sTransformedXML

    ''Saving the file finally
    oOutputHTML.Close

    ''Destroying the objects
    Set oOutputHTML = Nothing
    Set oFSO = Nothing
    Set oXMLDoc = Nothing
    Set oXSLDoc = Nothing

End Function
'''--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Now to use the function...

''Path of the Results file which needs to be converted
sResultsXMLPath = "C:\auto\Results.xml"

''Path of the XSL for detailed version
sDetailedXSLPath = "C:\auto\PDetails.xsl"

''Path of the XSL for short version
sShortXSLPath = "C:\auto\PDetails.xsl"

Call funcConvertResultsXMLtoHTML (sResultsXMLPath, sDetailedXSLPath, "C:\auto\Results_Detailed.html")
Call funcConvertResultsXMLtoHTML (sResultsXMLPath, sShortXSLPath, "C:\auto\Results_Short.html")