Case Study: Wait for File Download to Complete Safely in Selenium

How to increase reliability in an automated test for a file download

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 Download in Selenium WebDriver.
This article just focus on the waiting part.

The temporary file was created by Chrome during the download.
I am using Chrome for this article.
// return list of all downloaded items from the shadow root's downloadsList
return document.querySelector('downloads-manager')
.shadowRoot
.getElementById('downloadsList')
.items;
driver.get("http://zhimin.com/books/pwta")
driver.find_element(:link_text, "Download").click
sleep 0.2 # give time for download to begin

driver.get("chrome://downloads")
# get download list under shadowRoot using JS (see above)
items = driver.execute_script("return document.querySelector('downloads-manager').shadowRoot.getElementById('downloadsList').items;")
expect(items[0]["fileName"]).to eq("practical-web-test-automation-sample.pdf")

Keen readers may notice that I still added a fixed time wait after clicking the Download button. It takes time for the download to begin, Chrome to do a security scan, etc., so I added a small wait.

while items[0]["state"] == "IN_PROGRESS"
sleep 1
items = driver.execute_script("return document.querySelector('downloads-manager').shadowRoot.getElementById('downloadsList').items;")
end
expect(items[0]["state"]).to eq("COMPLETE")
expected_download_file_path = File.expand_path "~/Downloads/practical-web-test-automation-sample.pdf"
expect(File.exist?(expected_download_file_path)).to be true
Test Execution in TestWise
it "Wait Browser Download To Complete" do
expected_download_file_path = File.expand_path "~/Downloads/practical-web-test-automation-sample.pdf"
File.delete(expected_download_file_path) if File.exists?(expected_download_file_path)

# click download Sample button
driver.find_element(:link_text, "Download").click
sleep 0.2 # give time for download to begin

driver.get("chrome://downloads")
items = driver.execute_script("return document.querySelector('downloads-manager').shadowRoot.getElementById('downloadsList').items;")
expect(items[0]["fileName"]).to eq("practical-web-test-automation-sample.pdf")

while items[0]["state"] == "IN_PROGRESS"
sleep 1
items = driver.execute_script("return document.querySelector('downloads-manager').shadowRoot.getElementById('downloadsList').items;")
end
expect(items[0]["state"]).to eq("COMPLETE")
expect(File.exist?(expected_download_file_path)).to be true
end

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store