Member-only story
Automated Testing PDF Download in Selenium WebDriver
How to test downloading PDFs in Selenium WebDriver

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
- 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. - Verify the downloaded PDF exists
Once the file is downloaded from the browser, check if it is downloaded successfully on the machine. - 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