Member-only story
Case Study: Wait for File Download to Complete Safely in Selenium
How to increase reliability in an automated test for a file download.

A repost of my daughter’s article, I added a few notes. This is also included in my “How to in Selenium WebDriver” series.
This article will show you how to verify a file download completes successfully in Selenium WebDriver.
Table of Contents:
∘ Test Design
∘ Fixed Wait
∘ Check the Downloaded File’s Size
∘ Wait for the Browser Download to Complete
∘ Complete Test Script
∘ Zhimin’s Notes
The test site for this article is http://zhimin.com/books/pwta. There is a sample PDF download that I will use for the tests.

Test Design
The test design is quite straightforward.
- Click the download button
- Wait…
- Verify the file contents
There is an unknown factor with Step 2 — how long should we wait in the test script? And when do we know the file has finished downloading?
There are three approaches:
- Fixed wait
- Check the downloaded file’s size
- Wait for the browser download to complete ✅
Fixed Wait
This approach is very easy to understand. Just hardcode the wait time.
it "Fixed wait time" do
driver.get("http://zhimin.com/books/pwta")
driver.find_element(:link_text, "Download").click
sleep 10 # wait 10 seconds
expect(File.exist?("/Users/me/Downloads/practical-web-test-automation-sample.pdf")).to be true
end
The above file-download script is not safe, we need to
- set the custom file download path (for Chrome)
- delete the file if already exists, before clicking the ‘Download’.
This has been covered in this article, Automated Testing PDF…