- Read pdf to verify a report in test complete.
- You have to import pdfbox.dll
- Read report pdf data and validate any string to verify a test functionality.
function verifyPDFTextValue(){
var status = verifyPDFText("50 plus_ element");
return status;
}
function readPDFData(strReportFilePath)
{
var filePath = callPDF();
var doc = dotNET.org_apache_pdfbox_pdmodel.PDDocument.load(filePath);
var pdfStripper = dotNET.org_apache_pdfbox_util.PDFTextStripper.zctor_2();
var str = pdfStripper.getText_2(doc);
Log.Message("See Additional Info", str);
doc["close"]();
return str;
}
function verifyPDFText( strTextToVerify ){
var status ;
var pdfFileFullPath = callPDF();
var pdfText = readPDFData(pdfFileFullPath);
if(pdfText.Contains(strTextToVerify)){
Log["Message"]("Verification successful. Text in PDF file is equal!");
status = "Success";
}
else {
Log["Error"]("Text in PDF file is different from the parameter passed!");
status = "Failure";
}
return status;
}
// We should call last modified file in place of below method in a dynamic environment.
function callPDF(){
var sPath = "../IrisProject/TestData/TestDataPDF/TestPDF.pdf"
var sExpPath = aqFileSystem["ExpandFileName"](sPath);
var fileName = aqFileSystem["GetFileNameWithoutExtension"](sExpPath);
// Posts the file name and extension to the test log
Log["Message"]("The file name is " + fileName);
Files["TestPDF"]["Check"](fileName);
Log.Message(sExpPath);
return sExpPath;
}
- For any means the report or pdf file path/name is dynamic then below method will help to get the latest file in a folder. pass that path to readPDFData function insted of calling function callPDF().
function getLastModifiedFileName(){
var status ;
Delay(5000);
var reportsPath = CreateReportFolder();
Delay(5000);
var fileToSearch = FindLastModifiedFileInFolder(reportsPath, ".*");
Log["Message"](fileToSearch);
if(fileToSearch != null)
{
Log["Message"]("File found " + fileToSearch);
status = "Success";
}
else
{
Log["Error"]("File not found!");
status = "Failure";
}
return status;
}
//This method Creates report folder under project root folder, if report folder does not exists , ignores otherwise.
function CreateReportFolder()
{
var reportsFolderRelativePath = "../IrisProject/PTPReports";
var reportsFolderFullPath = aqFileSystem["ExpandFileName"](reportsFolderRelativePath);
if(!aqFileSystem["Exists"](reportsFolderFullPath))
{
reportsFolderFullPath = aqFileSystem["CreateFolder"](reportsFolderFullPath);
}
return reportsFolderFullPath;
}
function FindLastModifiedFileInFolder(FolderPath,FileNameContains)
{
var FolderObject = aqFileSystem.GetFolderInfo(FolderPath); //The folder to look in
var FileItems = FolderObject.Files //Collection of all the files in the folder
var FileDateModified = new Array(); //Array of all the relevant date modified info for the files
var FileNumber = new Array(); //The absolute index number for the files sorted
var LatestFileNumber; //The index to the latest file modified
var NewestTime; //Placeholder for the Newest time when searching
//Builds up the arrays with the filnames containing FileNameContains reg exp
for (var i=0; i < FileItems.Count; i++)
{
if(FileItems.Item(i).Name.search(FileNameContains) > -1)
{
FileNumber.push(i);
FileDateModified.push(FileItems.Item(i).DateLastModified);
}
}
//Finds the most resent modified file
NewestTime = FileDateModified[0];
Log["Message"](NewestTime);
LatestFileNumber = FileNumber[0];
Log["Message"](FileDateModified);
for (var i=0; i < FileNumber.length; i++)
{
if(aqDateTime["Compare"](NewestTime, FileDateModified[i]) < 0)
{
NewestTime = FileDateModified;
LatestFileNumber = FileNumber;
}
}
return FileItems.Item(LatestFileNumber).Path;
}
- Find the screenshot it will help in getting a clear idea.
***** For any further queries...............reply back:)