Skip to main content

PDF Automation in QTP

                                                                            The most challenging issue with PDFs is that it could be of any kind, not just a tabular data; it could have plain text, images or even forms to fill up. So this makes a tester’s life a bit difficult, never mind, we will definitely find an easy of do it…

Although there are already some better approaches we have to deal with PDF documents but I found many of us are facing so many difficulties using this.

There are lots of queries coming at QTP forums asking for an easy way of doing it with PDFs. keeping those in my mind I started creating this API mainly for comparing two PDF documents, and added few more features in it. We will see all of them later in this article.

LearnQuickTestPDF API works with iTextSharp. Sometime back when I was involved in a PDF project I found this really useful library which does a great deal to ease the burden of manipulating PDF documents.It provides all of the primitive functions necessary to create a PDF document. However,since all of the methods are based on primitive operations, it is easy to confuse the look and feel of a document without enforcing certain standards. Visit iText Home to learn more about iTextSharp.

Let us now see how we can use LearnQuickTestPDF

Download 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.
Set oPDF=createobject("LearnQuickTest.ManipulatePDF")
Once we get the object, we can now proceed with using different methods to manipulate the documents.

Let’s see one by one,
1. Comparing two pdf documents: Use ‘ComparePdfFiles’ method to compare two pdf documents. It returns true if your there is no difference in the two documents otherwise false.
PDFFile1 – Pdf file path to compare with
PDFFile2 – Pdf file path to compare to
ResultFile – text output file to store the log/difference if any
FromPageNum – [Optional Parameter] Page number to start from
ToPageNum -[Optional Parameter] Page number to end to.

Example –
If oPDF.ComparePdfFiles ("C:\Actual.pdf","C:\Expected.pdf","C:\ResultCompare.txt") =True then
reporter.ReportEvent micPass ,"PDF Compare", "No Difference found"
reporter.ReportEvent micFail ,"PDF Compare", "Files are different"
End IF

2. Retrieve text from pdf document: Use ‘GetPDFText’ to retrieve the content from the pdf file.
Parameters –
PDFFile – PDF file path
FromPageNum – [Optional Parameter] Page number to start from
ToPageNum – [Optional Parameter] Page number to end to.

print oPDF.GetPdfText ("C:\fw4.pdf")

3. Find number of pages in the pdf document: Use GetNumberOfPages(“<>”) to find out the number of pages in the document.

msgbox oPDF.GetNumberOfPages("C:\ Expected.pdf")

4.Retrieve field names from the PDF form: Method – GetFieldNames
Use Parameters
PDFFile – document path which contains form
resultFile – text output file to store the Field names

example –
oPDF.GetFieldNames "C:\fw4.pdf","C:\ fw4Fields.txt"

5.Fill the PDF form: Use method – FillPDFForm
Parameters –
sourcePDF – Form PDF path to fill
outputPDF – output pdf path for completed document
FiledNames – Name of the fields to fill in each separated with ‘,’
FieldValues – Values for the corresponding fields separated with ‘,’
LogFile – Path to store log for the action

Fields = "f1_01(0),f1_02(0),f1_03(0),f1_04(0),f1_05(0)
Values = "1,1,1,8,0,1,16,28,Saket K, Test"
oPDF.FillPDFForm "C:\fw4.pdf","C:\fw4Filled.pdf",Fields, Values,"C:\\fillFormLog.txt"

Apart from this we have some more methods to work with
6. GetFieldValue – to retrieve the value of a particular field in the acro form
7. Set SingleValue – to set the a single field value if required.
8. DownloadPDF – to get the document downloaded to your hard drive from a web location.

[This utility has been successfully tested to work with QTP 10.0, Adobe 9.0 and IE 8.0. Please report bugs and issues here.]

Post a Comment

Popular posts from this blog

Convert JSON to XML using QTP/UFT/VBScript

Sample Code : Dim strPage,strJSON,objIE strPage = "C:\Jay\JLoader.html" Set objIE = CreateObject("InternetExplorer.Application") objIE.Visible = True objIE.Navigate2 strPage While objIE.Busy : Wend strJSON = "{""FirstName"":""Jay"", ""LastName"":""Krishna""}" Set objWin = objIE.document.parentWindow objWin.execScript "var jsonStr2XML = function(strJSON) { return json2xml(JSON.parse(strJSON));};" Msgbox  oWin.jsonStr2XML(strJSON) objIE.Quit

In Detail:
Converting The most popular data interchange format JSON(JavaScript Object Notation) to XML using QTP/UFT.
Parsing JSON in UFT could be a challenge so we will use JavaScript in UFT to make it perfect.
SO We need :              Java Script API  - To Convert JSON to XML                         JavaScript Files :
             JSON parser …

Compare Two Excel sheets cell by cell Using Vb Script

'This code will open two excel sheet and compare each sheet cell by cell, if any changes there in cells , it will highlight the cells in red color  in the first sheet.
Set objExcel = CreateObject(“Excel.Application”)
objExcel.Visible = True
Set objWorkbook1= objExcel.Workbooks.Open(“C:\Jaykrishna\Docs1.xls”)
Set objWorkbook2= objExcel.Workbooks.Open(“C:\Jaykrishna\Docs2.xls”)
Set objWorksheet1= objWorkbook1.Worksheets(1)
Set objWorksheet2= objWorkbook2.Worksheets(1)
   For Each cell In objWorksheet1.UsedRange
       If cell.Value <> objWorksheet2.Range(cell.Address).Value Then
           cell.Interior.ColorIndex = 3′Highlights in red color if any changes in cells
           cell.Interior.ColorIndex = 0
       End If
set objExcel=nothing

Customised HTML Result Report for UFT/QTP with ScreenShots in HP ALM TestSet attached.

'Module Name            :    GenerateCustomReport.qfl
' Author                    :    Jay
'Date Created            :     13/11/2014
'Description                :     The library contains various functions that are used to generate a Custom HTML report
'Project/Test Specific Variables
Public blnGlobalHTMLReport : blnGlobalHTMLReport =True' is a global variable. If this is set to true then Custom HTML Reporting will be enabled for the Test
Public blnGlobalQTPReport : blnGlobalQTPReport =True' is a global variable. If this is set to FALSE then the Reporter events will NOT be logged by this function only. Reporter events called outside this function will be logged

How to Read or Select Context Menu or Right Click Menu using QTP.

Select The Item in Right Click Menu or Context Menu: Window("sampleWindow").WinMenu("MenuObjType:=1).Select"File;New"

Here MenuObjtype can be 1 r 2 r 3 .......n

Check wether the Item is Exist or Not:
If Window("sampleWindow").WinMenu("MenuObjType:=1).GetItemProperty("1","Exist") Then
 Msgbox"Does Not Exist"
End If


If Window("sampleWindow").WinMenu("MenuObjType:=1).GetItemProperty("File","Exist") Then
 Msgbox"Does Not Exist"
End If

Get the Items in Context Menu:
For i = 1 to 10
Print  Window("sampleWindow").WinMenu("MenuObjType:=" & i).GetItemProperty("1","Label") Then

How to run Excel Macro in QTP

Function RunMacro(sPath, sMacroName, sArg1, sArg2)
   Set xlApp = CreateObject("Excel.Application")
   Set xlWbk = xlApp.Workbooks.Open(sPath)
   RunMacro = xlApp.Run(sMacroName, sArg1, sArg2)
End Function

***your macro in VBA should be public