Automate the custom file download in Selenium — Chrome

HARSHVARDHAN SINGH CHAUHAN
2 min readDec 31, 2021

When it comes to custom download the file in some location other than the regular downloads folder, it can be done easily via chrome settings. But at times, we need to automate the whole scenario. Selenium can help us to download the file via the WebDriver to the location specified as the browser capability.

All we need to do here is create a HashMap and add the preference as key value pair. This HashMap is used to put the custom path where the file is required to be downloaded. This needs to be set to ChromeOption using setExperimentalOption.

Post download, the verification of the file can be done using Input-Output operations.

Here we need to navigate to the directory. Once we are into the destination directory, we need to iterate through all the files in the directory and match the file name with the target file name. Once, the match is successful, Boolean flag is returned which can be then asserted.

This utility along with the capability can be utilized in order to download a custom file and assert if it is downloaded successfully. Please refer the below sample code.

NOTE: The download is done using Selenium but assertion part is something which is outside the purview of selenium.

When we know the user of any particular machine, then this can be done but what if any remote machine is accessed using build executor. Please let me know your thoughts on how to custom download in such cases. Happy reading!!

--

--

HARSHVARDHAN SINGH CHAUHAN

An avid reader, spoken word artist, dog lover, explorer, full stack qa engineer...