Change Default Download Path Of Chrome Browser Using Selenium
Hello,
Welcome to the first Blog by me on one of the very simple yet important aspects of creating a Test Automation Framework. Changing/Setting up the Default path to Download files while executing a script.
Downloading files in the correct folder is one of the crucial steps when doing Test Automation of a Web Application. By default, files are downloaded in the Download folder under User Directory typically the path address is C:\Users\<User_Name>\Downloads. But if the user wants to reuse the downloaded file or verify the content of the file, then it may become hard to find the right file among all the other already downloaded files/folders.
So we can always change the path of Download directory and save files at our desired location which is always the right choice to be done inside a framework. This path should remain inside our Project Directory Workspace folder so as it is easy to find when used by a new person.
Doing it inside our project structure would guarantee a consistent flow for all users of the project and will also make it look consistent. Also, we can delete all files before the start of the actual execution of our Test Scripts.
So let's start to understand, how to do it.
So we can always change the path of Download directory and save files at our desired location which is always the right choice to be done inside a framework. This path should remain inside our Project Directory Workspace folder so as it is easy to find when used by a new person.
Doing it inside our project structure would guarantee a consistent flow for all users of the project and will also make it look consistent. Also, we can delete all files before the start of the actual execution of our Test Scripts.
So let's start to understand, how to do it.
We need to make use of the ChromeOptions class.
We would create a folder named <Download_Path> in our Workspace Project Folder and write the below code to execute it.
In line 2, we have defined the new path for the file to be downloaded in a String var downloadFilepath.
The same is passed as a key-value pair to a HashMap chromePrefs. The key name is download.default_directory.
In line 7, the created chrome preferences are passed as a parameter to the method setExperimentalOption using the object of ChromeOptions class.
In line 7, the created chrome preferences are passed as a parameter to the method setExperimentalOption using the object of ChromeOptions class.
In line 9, the driver is launched.
VOILA !! It is done if you now download any file from the web in your automated script, so it will go and reside inside this new folder.
Comments
Post a Comment