Skip to main content

PDF Validation with UI

Overview

The Verifying PDF Document guide explains how to validate the contents of a PDF file as part of a test case in TestGrid. The validation process involves:

  • Navigating your application to the point where the PDF is generated or available.
  • Extracting text from the PDF using a script within the test case.
  • Comparing extracted PDF data with values shown in the UI to ensure correctness.
  • Running the test and verifying that logs correctly reflect the values from the PDF.

This method is useful when your application produces PDF outputs (reports, receipts, etc.) and you want to assert that PDF content matches expected UI or data values.

Prerequisite Steps

Before you implement PDF verification in TestGrid, here are the preparation steps you need to perform:

1. Create a Test Case

  • Add a new test case in TestGrid where you will include steps to navigate to the screen or operation that generates the PDF you want to verify.

2. Define Variables

  • Declare variables in the test case to store values you will extract from the PDF (e.g., title, author, page count).

3. Add Custom Script for PDF Extraction

  • Insert a custom script step that opens the PDF (via URL or local path) and extracts text content from it.
  • Use a PDF parsing approach (e.g., PDFBox or similar library) to split the text into lines and assign values to your variables.

4. Extract UI Values

  • Add steps to capture values from your application UI that should match the PDF content.
  • Store those UI values in variables declared earlier.

5. Compare Values

  • Add assertion steps in the test case to compare the extracted PDF values with the corresponding UI values to validate consistency.

6. Test Execution

  • Save your test case.
  • Run it with the appropriate URL and browser/device configuration. Logs will show extracted values and test results.

Step 1: Click on Add Test Case and create a new test case.

Image

 

Step 2: Add the steps to the test case to navigate to the screen where elements are visible, which we need to validate in the PDF document.

Image

Step 3: Declare the empty variables in the test case writer to save values that are extracted from the PDF using a custom script.

Image

Step 4: Add a custom script to access the data from the PDF document.

Image

  • In the custom script you can access the data from the PDF, which is further assigned to the variables declared by the user in the previous step.
  • The following is an example script to draw out value from the PDF.
try {
            URL url = new URL("https://devtestos.testgrid.io/assets/pdf/ToolsQA.pdf");
            System.out.println("PDFURL2 == :-");
            InputStream is = url.openStream();
            BufferedInputStream fileToParse = new BufferedInputStream(is);
            PDDocument document = null;
            String output = null;
            try {
                document = PDDocument.load(fileToParse);
                output = new org.apache.pdfbox.text.PDFTextStripper().getText(document);

                String lines[] = output.split("\\r?\\n");
         for (int i = 0; i < lines.length; i++) {

                    var_ISBNFromPDF =  lines[15];
                    var_TitleFromPDF =  lines[16];
                    var_SubTitleFromPDF =  lines[17];
                    var_AuthorFromPDF =  lines[18];
                    var_TotalPagesFromPDF =  lines[20];

                             System.out.println("line ====== :"+i +"Value of lines" + lines[i]);
                    
                }

           } finally {
                if (document != null) {
                    document.close();
                }
                fileToParse.close();
                is.close();
            }
        }catch (Exception e){

            e.printStackTrace();
        }
  • Once the custom script is executed, the next step is to take the UI element values and save them in the variables declared by them.
Step 5: Declare the variables in the test case writer in which values are retrieved from the UI elements.

Image

Step 6 : Add steps to check if the values retrieved from the UI are the same as the values obtained from the PDF.

Image

Step 7: Click on ‘Save’ to save the testcase.
Step 8: Once the testcase is ready, select the testcase and click on ‘Run’.

Image

Step 9: Enter the URL of the website you want to test and tap ‘Next.’

Image

Step 10: Select the browser and tap ‘Run Test’.

Image

  • As the test case starts to run, you can see the logs of the test case execution.

Image

  • You can also see the values printed in logs, which are taken from a PDF using a custom script.

Image

  • If the test run is successfully executed, the user can see the messages “Completed” and “Test success” in the detailed log.

Image

Table of Contents