Sunday, January 11, 2009

QTP Script 21 - How to Export QuickTest Professional (QTP) test results to HTML file?

The results of each QuickTest run session are saved in a single .xml file (called results.xml). As an example C:\Program Files\HP\QuickTest Professional\Tests\Test7\Res1\Report. Here under Report you will see a Results.xml file. This below code will convert this xml file into html file.

Source: http://www.codeproject.com/KB/vbscript/xml2csv.aspx

Dim xmlSource

Dim xmlXForm

Dim strErr

Dim strResult


Dim fso, file

Dim strPath

Const ForWriting = 2


[Source:http://www.ida.liu.se/~TDDB65/laboration/resultat.html


A better approach to retrieving information from XML files is to use an XML parser. An XML parser is, quite simply, software that reads an XML file and makes available the data in it.


We will use a parser that supports the XML Document Object Model (DOM). The DOM defines a standard set of commands that parsers should expose so you can access HTML and XML document content from your programs. An XML parser that supports the DOM will take the data in an XML document and expose it via a set of objects that you can program against.


A DOM for XML is an object model that exposes the contents of an XML document.


There are many ways to use the XML DOM. We will access the DOM by setting a reference to the MSXML type library, provided in Msxml.dll


For example Create an instance of the Parser object like this below


Dim xDoc As MSXML.DOMDocument Set xDoc = New MSXML.DOMDocument


Once you obtain a valid reference, open a file using the Load method.]


Set xmlSource = CreateObject("MSXML.DOMDocument")

Set xmlXForm = CreateObject("MSXML.DOMDocument")


[validateOnParse: This property contains a boolean value indicating whether this document should be validated by the parser. The default is true. If false, only well-formed XML will be parsed.

The async property specifies whether downloading of an XML file should be handled asynchronously or not.

True means that the load() method returns the control to the caller before the download is complete.

False means that the download must be completed before the caller gets the control back.]


xmlSource.validateOnParse = True

xmlXForm.validateOnParse = True

xmlSource.async = False

xmlXForm.async = False


Type the path of your own Results.xml file

xmlSource.Load "C:\Program Files\HP\QuickTest Professional\Tests\sachin-tr\Res2\Report\Results.xml"


If Err.Number <> 0 Then

strErr = Err.Description & vbCrLf

strErr = strErr & xmlSource.parseError.reason & " line: " & _

xmlSource.parseError.Line & " col: " & _

xmlSource.parseError.linepos & _

" text: " & xmlSource.parseError.srcText

MsgBox strErr, vbCritical, "Error loading the XML"

End If

Type the path of your own PDetails.Xsl file

xmlXForm.Load "C:\Program Files\HP\QuickTest Professional\dat\PDetails.Xsl"


If Err.Number <> 0 Then

strErr = Err.Description & vbCrLf

strErr = strErr & xmlSource.parseError.reason & " line: " & _

xmlSource.parseError.Line & " col: " & _

xmlSource.parseError.linepos & _

" text: " & xmlSource.parseError.srcText

MsgBox strErr, vbCritical, "Error loading the Transform"

End If


transformNode() method is used to apply the XSL style sheet to the xml document


strResult = xmlSource.transformNode(xmlXForm)

If Err.Number <> 0 Then


strErr = Err.Description & vbCrLf


strErr = strErr & xmlSource.parseError.reason & _

" line: " & xmlSource.parseError.Line & _

" col: " & xmlSource.parseError.linepos & _

" text: " & xmlSource.parseError.srcText

MsgBox strErr, vbCritical, "Error executing the Transform"


End If


Set fso = CreateObject("Scripting.FileSystemObject")

This will be the resultant html file

strPath = "c:\TestResult.html"

open the file

Set file = fso.opentextfile(strPath, ForWriting, True)

write the info to the file

file.write strResult

close and clean up

file.Close