Member-only story

Automated Testing PDF Download in Selenium WebDriver

How to test downloading PDFs in Selenium WebDriver

Zhimin Zhan
5 min readMay 3, 2022

A repost of my daughter’s article with permission and slight modification. I added it here for the convenience of my blog subscribers. You can find more Selenium examples like this one in my eBook: Selenium WebDriver Recipes in Ruby.

This is also included in my “How to in Selenium WebDriver” series.

Many websites feature links that download a PDF document. These PDF files might be static (e.g. a restaurant menu) or dynamically generated (e.g. a receipt).

This tutorial will show you how to download a PDF document and verify its contents in a Selenium WebDriver automated test script.

Table of Contents:
· Test Design
Saving the download file to a specific location
PDF verification library
· Open browser with specified download folder
· Download and Verify the downloaded file
· Verify the PDF
Verify PDF page count
Verify PDF contents
· Completed Test Script

Test Design

  1. Navigate to a web page and download the PDF
    For my example, I’m downloading a book sample PDF (static) at http://zhimin.com/books/pwta.
  2. Verify the downloaded PDF exists
    Once the file is downloaded from the browser, check if it is downloaded successfully on the machine.
  3. Read and verify the PDF’s contents
    It’s good practice to verify the PDF’s contents, just to make sure.

Saving the download file to a specific location

By default, the browser will save downloaded files into a special folder, such as /Users/Me/Downloads on macOS. This folder might change on the Windows platform, or even with permission issues when used by test automation. To test safely and avoid conflicts, we should specify the download folder in the automated test scripts.

PDF verification library

I use the ‘pdf-reader’ gem, a PDF parser library, to verify the PDF. Install it from the command line:

gem install pdf-reader

Open browser with specified download folder

--

--

Zhimin Zhan
Zhimin Zhan

Written by Zhimin Zhan

Test automation & CT coach, author, speaker and award-winning software developer. Help teams succeed with Agile/DevOps by implementing real Continuous Testing.

Responses (1)

Write a response