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.
parameters:
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"
Else
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.


Example
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.

Example
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


Example
Fields = "f1_01(0),f1_02(0),f1_03(0),f1_04(0),f1_05(0)
,f1_06(0),f1_07(0),f1_08(0),f1_09(0),f1_10(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.]

 Source: http://www.learnqtp.com

Comments

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 :                         http://goessner.net/download/prj/jsonxml/j

Read Outlook mail attachment and Body using Vb Script or QTP

Set olApp = CreateObject("Outlook.Application") Set olns = olApp.GetNameSpace("MAPI") Set ObjFolder = olns.GetDefaultFolder(6) j = 0 For each item1 in ObjFolder.Items        iattachCnt = item1.Attachments.Count     Print "Attachments Count: " & iattachCnt     For i = 1 to iattachCnt         Print "FileName :    " & item1.Attachments(i).FileName         Print "Display Name:   " & item1.Attachments(i).DisplayName         Print "Size: " & item1.Attachments(i).Size     Next     Print " Body : " & item1.body     Print "--------------------------------------Mail Num - " & j & " -----------------------------------------------"     j = j+1    Next

Excel Sorting By Rows and Columns

Excel Sorting By Row: Const xlAscending = 1 Const xlNo = 2 Const xlSortRows = 2 Set objExcel = CreateObject(“Excel.Application”) objExcel.Visible = True Set objWorkbook = objExcel.Workbooks.Open(“C:\Jay\Docs1.xls”) Set objWorksheet = objWorkbook.Worksheets(1) objWorksheet.Cells(1,1).activate Set objRange = objExcel.ActiveCell.EntireRow objRange.Sort objRange, xlAscending, , , , , , xlNo, , , xlSortRows set objExcel=nothing Excel Sorting By Column : Const xlAscending = 1′represents the sorting type 1 for Ascending 2 for Desc Const xlYes = 1 Set objExcel = CreateObject(“Excel.Application”)’Create the excel object objExcel.Visible = True’Make excel visible Set objWorkbook = _ objExcel.Workbooks.Open(“C:\Jay\Docs1.xls”)’Open the document Set objWorksheet = objWorkbook.Worksheets(1)’select the sheet based on the index .. 1,2 ,3 … Set objRange = objWorksheet.UsedRange’which select the range of the cells has some data other than blank Set objRange2 = objExcel.Range

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"Exist" Else  Msgbox"Does Not Exist" End If                                         Or If Window("sampleWindow").WinMenu("MenuObjType:=1).GetItemProperty("File","Exist") Then   Msgbox"Exist" Else  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 Next

How to Download a file using VbScript

Following is the code to download a file using Vbscript, without using QTP This code uses the HTMLDom and URLDownloadToFile method from urlmon API. Since VBScript does support calling Native API methods directly, here I am using  Excel macro to declare a function for the urlmon API and running the macro by Excel API from VBscript Step1: Create a new excel and open the visual basic editor, Insert Module and paste the following code the Module, save the excel file Private Declare Function URLDownloadToFile Lib “urlmon” Alias _                                            “URLDownloadToFileA” ( _                                            ByVal pCaller As Long, ByVal szURL As String, _                                            ByVal szFileName As String, _                                            ByVal dwReserved As Long, _                                            ByVal lpfnCB As Long) As Long Sub FileSave(strUrl, Des)     r = URLDownloadToFile(0, strUrl, Des, 0, a)