-1

When I click on the "Download PDF" link, the file is downloaded in Chrome. I need to open and verify the transaction data is correct in the file.

I use Selenium and Java.

3 Answers 3

0

Selenium is a web browser manipulation library. Dealing with the file system is out of its scope.

If you need to manipulate the PDF file, you can use a library like Apache PDFBox (Maven).

But if you open the file using some program installed on the hosting machine, you can use Java's built-in Process object:

Process process = Runtime.getRuntime().exec(command_to_open_a_PDF_file);
0

You can use the Java library PDFBox to read and verify the contents of a PDF file. Here is an example code snippet to download a PDF file in Chrome and verify its contents:

import java.io.File;
import java.io.IOException;
import java.net.URL;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class VerifyPDFContent {

  public static void main(String[] args) throws IOException {
    // Set the ChromeOptions to download the PDF file
    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("prefs", 
        "{'plugins.always_open_pdf_externally': true, 'download.default_directory': '<download_path>'}");
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    WebDriver driver = new ChromeDriver(capabilities);
    
    // Navigate to the page containing the link to download the PDF
    driver.get("https://www.example.com");
    
    // Click on the link to download the PDF
    driver.findElement(By.linkText("Download PDF")).click();
    
    // Wait for the file to download
    // (use a different method if you want to wait for a specific download completion event)
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    
    // Open the downloaded PDF file and verify its contents
    String filePath = "/home/username/file_name.pdf";
    URL url = new URL("file:///" + filePath);
    PDDocument document = PDDocument.load(url);
    PDFTextStripper pdfStripper = new PDFTextStripper();
    String text = pdfStripper.getText(document);
    System.out.println(text);
    // Verify the transaction data in the text string using regular expressions or other techniques
    
    document.close();
    driver.quit();
  }
}
0

PDF documents are small-sized, highly secure files. Almost all businesses use PDFs for processing their files. The reason being a distinguishing feature of maintaining format regardless of the tool used to access PDF files.

PDF file format is used standardly for generating official letters, documents, contracts, and other important files. Primarily because PDF cannot be edited while a Word format can be. Hence storing confidential information in PDF format is considered a good security practice.

To handle a PDF document in Selenium test automation, we can use a java library called PDFBox. Apache PDFBox is an open-source library that exclusively helps in handling the PDF documents. We can use it to verify the text present in the document, extract a specific section of text or image in the documents, and so on.

To use this in Selenium testing PDF files, we need to either add the maven dependency in the pom.xml file or add it as an external jar.

To check if a specific text piece is present in a PDF document we use PDFTextStripper which can be imported from org.apache.pdfbox.util.PDFTextStripper.

public class PdfHandling {

WebDriver driver = null;

@BeforeTest
public void setUp() {
    //specify the location of the driver
    System.setProperty("webdriver.chrome.driver", "C:\\Users\\vipsingh\\Downloads\\Driver\\chromedriver.exe");
    
    driver = new ChromeDriver();
}

@Test
public void verifyContentInPDf() {
    //specify the url of the pdf file
    String url ="http://www.pdf995.com/samples/pdf.pdf";
    driver.get(url);
    try {
        String pdfContent = readPdfContent(url);
        Assert.assertTrue(pdfContent.contains("The Pdf995 Suite offers the following features"));
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@AfterTest
public void tearDown() {
    driver.quit();
}


public static  String readPdfContent(String url) throws IOException {
    
    URL pdfUrl = new URL(url);
    InputStream in = pdfUrl.openStream();
    BufferedInputStream bf = new BufferedInputStream(in);
    PDDocument doc = PDDocument.load(bf);
    int numberOfPages = getPageCount(doc);
    System.out.println("The total number of pages "+numberOfPages);
    String content = new PDFTextStripper().getText(doc);
    doc.close();

    return content;}

public static int getPageCount(PDDocument doc) {
    //get the total number of pages in the pdf document
    int pageCount = doc.getNumberOfPages();
    return pageCount;   
}}

Most outsourced software testing companies are using Selenium LambdaTest Grid for PDF Testing. According to outsourced software testing companies, LambdaTest grid provides a great option to automate the tests in the cloud. We can carry out tests in multiple environments or browsers which helps us to determine the behavior of the web pages.

Using LambdaTest, we can perform Selenium testing PDF files on 3000+ browsers, devices, and operating systems.

LambdaTest also provides various filters to identify the tests run. The tests could be filtered based on the day of execution, build a name, and also the status of the build.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.