About Me

Gaurav Seth is a a Senior Automation Test Analyst with experience in QTP, Selenium and Ranorex tools and currently working on Automating in C# for a FX trading Client

PDF Manipulation with QTP

We can Use LearnQuickTestPDF to Manipulate PDF Using QTP

Download http://www.mediafire.com/?y1ytzwmerly
and run exe to extract file to hard drive, extract to “C:\LearnQTP”.
Open directory LearnQuickTestPDF and find the Install.vbs, this will make the API ready to use. But before this, read the terms and conditions first and then execute the install.vbs file by double clicking it. Accept terms and condition by clicking on ‘Yes’ button and proceed. That’s it you are now ready to use this into QTP.

Use this in the same way you do with other COM APIs.

Once we get the object, we can now proceed with using different methods to manipulate the documents

Set oPDF=createobject("LearnQuickTest.ManipulatePDF")

1. ' Get the Text of PDF . The Second Parameter is the Start Page and Third End Page

Print oPDF.GetPdfText ("C:\LearnQTP\LearnQuickTestPDF\fw4.pdf", 1,2)

2. ' Get the No of Pages of the PDF
Print oPDF.GetNumberOfPages ("C:\LearnQTP\LearnQuickTestPDF\fw4.pdf")

Windows Registry with QTP

The following snippet of code will create an entry in the Registry and give the Value to it.

Sub SetRegistryValue(strRegPath, strRegValue)
Set shObj = CreateObject("WScript.Shell")
shObj.RegWrite strRegPath, strRegValue
End Sub

Function GetRegistryValue(strRegPath)
Set shObj = CreateObject("WScript.Shell")
GetRegistryValue = shObj.RegRead(strRegPath)
End Function


SetRegistryValue "HKEY_CLASSES_ROOT\.ANUNAY\KEY2", "http://hpqtpautomation.blogspot.com/"
Msgbox GetRegistryValue("HKEY_CLASSES_ROOT\.ANUNAY\KEY2")






'You can use the Following Code to delete the Registry Entry
Set shObj = CreateObject("WScript.Shell")
shObj.RegDelete "HKEY_CLASSES_ROOT\.ANUNAY\KEY"


XML With QTP

Following Snippet of code will Print the contents of the XML File

Set xmlObj = XMLUtil.CreateXML()
xmlObj.LoadFile("C:\artifacts.xml")
Print xmlObj.ToString()
'Using a XML file stored on the hard drive




' This will Load any Web Page into an XML File and Print to see the ContentsSet xmlObj = XMLUtil.CreateXML()
xmlObj.LoadFile("http://www.w3schools.com/xml/cd_catalog.xml")
Print xmlObj.ToString()






'The Following Snippet of Code will create an XML File and add Chidren (Nodes) including values
Set doc = XMLUtil.CreateXML()

doc.CreateDocument "Papa"

Set root = doc.GetRootElement()
root.AddChildElementByName "Bambino","Valuable"

doc.SaveFile "C:\one..xml"


' The Following snippet of code will Load an XML File and Save it to another place giving it a different name
Set doc = XMLUtil.CreateXML()

doc.LoadFile "C:\artifacts.xml"

doc.SaveFile "C:\artifacts1.xml"

' Following snippet of code will display the name of the particular child and display the occurences of this
Set doc = XMLUtil.CreateXML()

doc.LoadFile "C:\artifacts.xml"

Set root = doc.GetRootElement()

Set children = root.ChildElements()

if children.Count() <> 0 then

Set firstChild = children.Item(2)

firstChildName = firstChild.ElementName()

Set sameNameChildren = children.AllItemsByName(firstChildName)

msg = "My first child name is " + firstChildName + " and I have " + FormatNumber(sameNameChildren.Count(),0) + " of them."

End If

msgbox msg


' Using the below snippet of code in the Above Mentioned XML will tell you how many children (nodes) does XML Have.
msgbox (children.count)


Parsing the XML

Once we have the XML data loaded with the XML object pointing to it, our job is very simple. We need to navigate through the structure looking for the data we want. In this example we wish to retrieve all the data related a particular CD in the CD Catalog. So what we do is that we utilize the ChildElementsByPath(Path) method do retrieve the data from each element of the XML. Here, "Path" is something common to the kind of path we specify in our PC for locating a particular file. Here, each XML tag can be considered as a folder with the XML Element value as a file. So the topmost folder becomes CATALOG, then comes CD, then (TITLE, ARTIST, COUNTRY, COMPANY, PRICE,YEAR) become the individual folders with CD. So in case you wish to find the Title of a particular CD, you use ChildElementsByPath("/CATALOG/CD/TITLE"). This will actually return a collection of titles of all the Cds and you can iterate through each to get your desired CD title. A similar approach may be employed for getting the other data like, artist, company, price and year. This is just a simple example, there are a number of other XMl methods in QTP that can be used as well. Have a look at the code below -


Set xmlObj = XMLUtil.CreateXML()
xmlObj.LoadFile("http://www.w3schools.com/xml/cd_catalog.xml")
Print xmlObj.ToString()
Print " ========================================================================= "


Set myCDTitle= xmlObj.ChildElementsByPath("/CATALOG/CD/TITLE")
'Get the titles
Set myCDArtist = xmlObj.ChildElementsByPath("/CATALOG/CD/ARTIST")
'Get the Artists
Set myCDCountry = xmlObj.ChildElementsByPath("/CATALOG/CD/COUNTRY")
'Get the Country
Set myCDPrice = xmlObj.ChildElementsByPath("/CATALOG/CD/PRICE")
'Get the Prices
Set myCDYear = xmlObj.ChildElementsByPath("/CATALOG/CD/YEAR")
'Get the Year

'Now iterate through the collection to get the values
For i = 1 to myCDTitle.Count
Print myCDTitle.Item(i).Value()
Print myCDArtist.Item(i).Value()
Print myCDCountry.Item(i).Value()
Print myCDPrice.Item(i).Value()
Print myCDYear.Item(i).Value()
Print " "
Next