Download file in Selenium is a need sometimes in test automation projects. Also, there are test cases where you need to check if the download links are working fine or not. It’s not easy to automate them as mostly download feature will work but content type might change. So why not automate that test cases?

Tool and Technology Stack for download file in Selenium

Step 1: Create WebDriver and Add Options

Chrome has many options that you can manipulate. First of all, you need to set the auto download directory in ChromeOptions, then create the driver with that option. For this purpose, you need to set “download.default_directory” to a path that you desire. 

Step 2: Download the file

This is custom part as you all have different scenarios. I assume that you all implement this part within your application. But just in case I add mine.

Step 3: Check The File in Folder

We create a Folder Object with the download path. Then get the file list on that folder and do a REGEX operation to check if the file is downloaded or not. Our regex is very hardcoded, you might need to do some complex regex operation according to your needs.

Now, you are able to download a file within a Selenium project and make the validation.

Full Source code can be found in swtestacademy GitHub Page:

Happy Testing!
Canberk Akduygu