Saturday, 23 May 2015

How to test PDF documents in Selenium, QTP and other tools

Many testers find it very tough to verify the contents of the PDF documents using any testing tools like Selenium, QTP, TestComplete or Tosca.

But let me tell you that it is much easier than what you might be thinking.
Below example will show how to do it.Please note that you will have to add the reference to the iTextSharp dll files or you can even install them from the Nuget Package manager in Visual Studio.

using System;
using System.Collections.Generic;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;

namespace pdfdoc
{
    class Program
    {
        static void Main(string[] args)
        {

            PdfReader reader
                 = new PdfReader("f:\\travel\\letter.pdf");
            // total number of pages
            int n = reader.NumberOfPages;
            Console.WriteLine("Total Number of pages in PDF " + n);
           // Get text from the first page
string txt = PdfTextExtractor.GetTextFromPage(reader,1, new LocationTextExtractionStrategy()); Console.WriteLine("Contents of First Page" + txt); if (txt.Contains("Brisbane")) { Console.WriteLine("Brisbane Exists"); } } } }

Now you may counter question me saying that it is pretty easy doing it from the C#.Net. But we want to do it in QTP and other tools where C# is not used.

Well - Answer to above question is simple. You can create a workable program using C#.Net with command lines where you will pass the pdf file path and contents to be checked as an arguments. Then you can easily convert this program into exe file which can be launched by QTP using shell commands.

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

No comments:

Post a Comment

Buy Best Selenium Books