Friday, 8 February 2013

Validate PDF data in selenium using java in firefox browser

There are many web applications which acknowledges some operation by  showing pdf file(like order confirmation receipt in pdf document) . Few days before I stuck when i was automating such scenario which shows pdf file using selenium , so i did some research on this and written following blog .Following code will help you to automate the pdf data validation.


You can download required java library from this link :

http://itextpdf.com/download.php


To automatically download the PDF file .
We need to do following settings in firefox .
1. Go to Tools
2.Go to options.
3. Select Applications.
4.Select Adobe Acrobat Document
5. Select action is Save File.  
Please refer following java code . Please let me know if you need more explanation :

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import com.itextpdf.awt.geom.Rectangle;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.FilteredTextRenderListener;
import com.itextpdf.text.pdf.parser.LocationTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
import com.itextpdf.text.pdf.parser.RegionTextRenderFilter;
import com.itextpdf.text.pdf.parser.RenderFilter;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;




public class PDFReader {

    public static String folderName="C:\\Users\\gurushant\\Downloads\\";
    public static final String pdfName=folderName+ "myPdfFile";
    public static final String outputFileName=folderName + "myFile.doc";
   
   
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       
        String data[]={"DataTobeValidated_1","DataTobeValidated_2","DataTobeValidated_3"};
         PDFReader pdf=new PDFReader();
         pdf.readPDFData();
         pdf.verifyData(data);
    }



void readPDFData()
{
    try {
        PdfReader reader=new PdfReader(pdfName);
        PrintWriter out=new PrintWriter(new FileOutputStream(outputFileName));
        Rectangle rect=new Rectangle(0, 0, 1000, 1000);
        RenderFilter filter=new RegionTextRenderFilter(rect);
        TextExtractionStrategy strategy;
       
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            strategy = new FilteredTextRenderListener(new LocationTextExtractionStrategy(), filter);
            out.println(PdfTextExtractor.getTextFromPage(reader, i, strategy));
        }
        out.flush();
        out.close();
        //GetAllImages.main(null);
         
        }
        catch (Exception e)
        {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

void verifyData(String data[])
{
try
{
   
    BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(outputFileName)));
    StringBuilder stringData=new StringBuilder();
   
    String line=null;
   
    while((line=br.readLine())!=null)
    {
        stringData.append(line);
    }
   
    for(int i=0;i<data.length;i++)
    {
        String txt=data[i];
        if(stringData.indexOf(txt) >0)
        {
            System.out.println( txt + " contains in order confirmation");
        }
        else
        {
            System.out.println( txt + " does not contains in order confirmation");
        }
       
    }
    br.close();
    }
    catch (Exception e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
}}

8 comments:

  1. Good one Gurushant.. It worked for me..
    But here we can give relative path for locating file instead of absolute path.

    anyways but it helped alot..

    ReplyDelete
    Replies
    1. @Punit: Firefox downloads at the default path. SO if you want relative path then you need to set the path for firefox downloading and use relative path in your program.

      Delete
  2. Nice and thanks..looking for this..

    ReplyDelete
  3. Hi Gurushant,
    If i have to click on a save button on the opened pdf file,can i use the pdfreader class?
    In my application ,clicking on a link opens a new pdf form in a new firefox windoe.Can i use the above code to click the save button of the pdf reader

    ReplyDelete
  4. Hi Nandini,
    Sorry for being late.Above code it saves the file automatically to the location which you specified in your browser. Follow following steps to save file automatically instead of opening in new window and verifying.

    We need to do following settings in firefox .
    1. Go to Tools
    2.Go to options.
    3. Select Applications.
    4.Select Adobe Acrobat Document
    5. Select action is Save File.

    And use above code with pdf file path . It works !!!. Let me know if you have any issue.

    ReplyDelete
  5. Hi Gurshant thanks for this post.
    I need your help from above code.
    pdf.readPDFData();
    pdf.verifyData(data);
    readpdfdata and verifydata are showing error it is not resolved rest code is not showing any error can you please help me out in this.
    Thanks

    ReplyDelete
  6. Hi Gurushant,

    I am getting file not found exception even though I have both myPdfFile and myFile in the right location and i have also ensured that the location is rightly given in the program.Please help.

    Thanks,
    Kiran

    ReplyDelete