How to Download a File Without a Browser
Intro
In a previous tip we went through how to download files with Selenium by configuring the browser to download them locally and verifying their file size when done.
While the previous method works, it requires a custom configuration that is inconsistent from browser to browser.
Ultimately, we shouldn't care if a file was downloaded or not. Instead, we should care that a file can be downloaded, which we can do by using an HTTP client alongside Selenium in our test.
A Solution
Ultimately we shouldn't care if a file was downloaded or not. Instead, we should care that a file can be downloaded. And we can do that by using an HTTP client alongside the browser in our test.
With an HTTP library we can perform a header (or HEAD) request for the file. Instead of downloading the file we'll
receive header information for the file which contains information like the content type and content length
(amongst other things). With this information we can easily confirm the file is what we expect without onerous
configuration, local disk usage, or lengthy download times (depending on the file size).
Let's continue with an example.
Example
NOTE: We are using a file download example found on the-internet.
It's just a simple matter of visiting the page with download links, grabbing a URL from one of them, and performing a
HEAD request with it. Once we receive the response we check its headers for the content disposition and the
content length to make sure the server sends the file as a download and that it is not empty.
Expected Behavior
- Open the browser
- Load the page
- Grab the URL of the first download link
- Perform a
HEADrequest against it with an HTTP library - Check the response headers to see that the file type is correct
- Check the response headers to see that the file is not empty
- Close the browser
Summary
Compared to the browser specific configuration with Selenium this is hands-down a leaner, faster, and more maintainable approach. But unfortunately it only works with files served up from a flat URL. So if you're trying to test file downloads that are generated in-memory as part of the browser session (a.k.a. not accessible from a URL) then you'll need to reach for the browser specific Selenium configuration.
Happy Testing!
Special Thanks


Authors

