0

I have a scenario to automate using Selenium WebDriver Java:
A webpage contains some customer data. It has a link Convert to PDF to convert that customer list in a secured pdf format. I have to verify that list of customers listed inside PDF document is according to the list displayed in the webpage.

Selenium WebDriver has capabilities to interact with elements present within Browser DOM. So to interact with PDF document rendered inside Browser window, I need some other Java utility which can retrieve data present within that PDF document. Please help if anyone faced anything similar before. Thanks in advanced.

Thanks,
Sitam

mkl
  • 90,588
  • 15
  • 125
  • 265
Sitam Jana
  • 3,123
  • 2
  • 21
  • 40
  • 1
    Do you mean you have to verify in the server before it serves the pdf to the client Browser? Or on the client while it is rendered? Or does the pdf include a Form which is posted by the client Browser to the server and you have to verify the posted data? – mkl Feb 10 '14 at 05:11
  • I have to verify pdf on the client browser. Basically the scenario is : There is a print link on a browser page. Upon clicking, it renders pdf document in browser window. I have to verify content inside that pdf document. – Sitam Jana Feb 10 '14 at 10:36
  • How do you want to execute a Java program in the client browser? A servlet in a different tab or window? A separate Java program? (Or do you actually mean Java Script in the browser? Or in the PDF?) – mkl Feb 10 '14 at 10:42
  • Sorry! I guess my question was not so clear! Edited :) – Sitam Jana Feb 10 '14 at 11:00
  • Which PDF viewer shall be used? The Adobe Reader plugin? Viewers built into the browser? Something else? – mkl Feb 10 '14 at 11:13
  • Ok, so I added the [selenium] and [adobe-reader] tags to your question. I doubt, though, that the Adobe Reader plugin is remote-controllable enough for such a test. Maybe a better test would be to retrieve the PDF and analyze it directly, not via a displaying PDF viewer. – mkl Feb 10 '14 at 12:46

1 Answers1

1

To read from a pdf file you can use the traditional FileReader API.

import java.io.*;    

public class FileRead {


    public static void main(String[] args) throws IOException {


        File f=new File("C:\\Documents and Settings\\abc\\Desktop\\abc.pdf");

        OutputStream oos = new FileOutputStream("test.pdf");

        byte[] buf = new byte[8192];

        InputStream is = new FileInputStream(f);

        int c = 0;

        while ((c = is.read(buf, 0, buf.length)) > 0) {
            oos.write(buf, 0, c);
            oos.flush();
        }

        oos.close();
        System.out.println("stop");
        is.close();

    }
}

You can pass this stream to the UI or you can directly pass a file path to the UI by using which it can access the file. To write or update a PDF File you can use either itext/pd4ml libraries.Both Work pretty good.

Links:

Itext

PD4ML

Mark Rowlands
  • 5,357
  • 2
  • 29
  • 41
Pankaj
  • 592
  • 7
  • 19