Changing the download path dynamically during runtime

Hi All,

I just want to ask if it is possible to change the download directory by passing a variable instead of a fixed download path during runtime? (e.g. C:\users\user\Downloads)

I tried passing it through settings but unfortunately there is no variable Type and I am guessing you can only put a fixed path there.

The reason behind is I want to create a temporary folder everytime i run the test is because I have a lot of files to be downloaded and I want to separate them per category and delete them after execution.

So far i tried RunConfiguration.setWebDriverPreferenceproperty like this:

Unfortunately, it’s not working. I am thinking that I should pass “prefs” on the first parameter but not sure what to pass on the second as it should be a dictionary Type.

Thanks!

1 Like

Maybe there is something in this post that might help…

Hi Russ_Thomas,

Thanks for the post.

Though I was finally able to make it work by passing HashMap instead of the contents.

now I was finally able to use this custom method to make the download path “dynamic” by passing a variable instead of setting it up hardcoded under settings.

image|690x293

1 Like

Hi porte.marvin, I have to do same, can you please share how you have done this.

As of now I am hardcoding downloadpath but it will get changed on other machine, eg username.
How can i get tha downloadpath on real time and then perform actions using that path.

Please help

1 Like

for me this works:

import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
import com.kms.katalon.core.webui.driver.DriverFactory

String downloadPath = “C:\customDownloadFolder\”

Map<String, Object> chromePrefs = new HashMap<String, Object>()
chromePrefs.put(“download.default_directory”, downloadPath)
chromePrefs.put(“download.prompt_for_download”, false)

System.setProperty(“webdriver.chrome.driver”, DriverFactory.getChromeDriverPath())
ChromeOptions options = new ChromeOptions()
options.setExperimentalOption(“prefs”, chromePrefs)
WebDriver driver = new ChromeDriver(options)
DriverFactory.changeWebDriver(driver)

WebUI.navigateToUrl(https://www.katalon.com/)
// other Test Stuff as usual

1 Like

Thanks, but I need more validations, it works only for chrome , I have to do for other browsers aswell. So i am trying like below …but not that opens two browsers, one with OpenBrowser and another with ChromeDriver initializing- please help
def pageload(String browser) {

	WebUI.openBrowser('')

	WebDriver driver = DriverFactory.getWebDriver()
	WebUI.closeBrowser()
	println("Driver new is "+ driver)

	long startTime = System.currentTimeMillis()
	println("The Value of brwonser is : "+ browser);
	String downloadPath = RunConfiguration.getProjectDir()+ '/' +'download_Upload/'
	println("The Chrome_Browser Download path is : " +downloadPath)
	Map<String, Object> browserPrefs = new HashMap<String, Object>()
	browserPrefs.put('download.default_directory', downloadPath)
	browserPrefs.put('download.directory_upgrade', true)
	browserPrefs.put('download.prompt_for_download', false)
	if(String.valueOf(driver).contains('chrome')) {
		println("Chrome found")
		System.setProperty('webdriver.chrome.driver', DriverFactory.getChromeDriverPath())
		println("declaring ChromeOptions")
		ChromeOptions options = new ChromeOptions()
		options.setExperimentalOption('prefs', browserPrefs)
		println("calling change web driver")
		driver=new ChromeDriver(options)

		DriverFactory.changeWebDriver(driver)
		println("executed change web driver")
	}
	else {
		DriverFactory.changeWebDriver(driver)
	}


	println("Rest of the program")

	long endTime = System.currentTimeMillis()
	long totalTime = endTime - startTime
	long TimeInSeconds = totalTime / 1000
	def String page_load_time = ("Time to load page "  +TimeInSeconds + " in Seconds")
	println (page_load_time)
	WebUI.navigateToUrl(GlobalVariable.url_app_navigator)
	println("Navigate to site")
	WebUI.maximizeWindow()
	WebUI.delay(10)

can this be achieved with Desired settings??

Hi Marvin, can you please share your working code snippet.?

I have changed the HashMap but it didn’t work, want to know where did I miss by looking at your code.

Hi all,

For those who are asking what I did, There are basically 3 methods i created to simplify things.

#1 createDownloadFolder()
#2 getCurrentBrowser()
#3 changeDownloadPath(string path, string browser)

If you need only the changeDownloadPath, here is my code simple code snippet. We are only using chrome and firefox so my code is only for chrome and firefox. You can just basically add another elseif for other browsers.

@Keyword
def changeDownloadPath(String browser, String path){

	if(browser=="chrome" || browser=="chrome-headless"){
		HashMap<String, Object> chromePrefs = new HashMap<String, Object>()

		chromePrefs.put("download.default_directory", path)
		chromePrefs.put("download.directory_upgrade", true)
		chromePrefs.put("browser.set_download_behavior", 'allow')
		chromePrefs.put("download.prompt_for_download", false)

		RunConfiguration.setWebDriverPreferencesProperty("prefs", chromePrefs)

	} else if(browser=="firefox" || browser=="firefox-headless"){

		HashMap<String, Object> profile = new HashMap<String, Object>()

		profile.put("browser.download.dir", path)
		profile.put("browser.download.folderList", 2)
		profile.put("browser.helperApps.neverAsk.saveToDisk", "application/vnd.openxmlformats-officedocument.wordprocessingml.document,	application/msword, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;application/vnd.ms-excel, application/vnd.ms-excel, application/pdf")
		profile.put("browser.download.manager.showWhenStarting",false);
		profile.put("pdfjs.disabled", true);

		RunConfiguration.setWebDriverPreferencesProperty("firefox_profile", profile)

	} else{
		KeywordUtil.logInfo("-- CHANGE DOWNLOAD PATH FAILED --")
	}
}

Hope this helps. :slight_smile:

3 Likes

Hi Marvin,

Keyword until was passed without any error, Didn’t download corresponding path.
Can you please help on this issue

This setup is working for me .

GlobalVariable.DownloadPath=RunConfiguration.getReportFolder()+"\Download"
println(‘GlobalVariable.DownloadPath:’+GlobalVariable.DownloadPath)
File file = new File(GlobalVariable.DownloadPath)
file.mkdir();
GlobalVariable.DownloadPath =GlobalVariable.DownloadPath+"\"
HashMap<String, Object> chromePrefs = new HashMap<String, Object>()
chromePrefs.put(“download.default_directory”, GlobalVariable.DownloadPath)
chromePrefs.put(“download.directory_upgrade”, true)
chromePrefs.put(“browser.set_download_behavior”, ‘allow’)
chromePrefs.put(“download.prompt_for_download”, false)
RunConfiguration.setWebDriverPreferencesProperty(“prefs”, chromePrefs)

It’s not working for me.
I’m getting below error message.
Reason:
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property;

Anyone please help me for this issue…

Thanks in advance.

Hi Everyone
I have resolved the issue

@porte.marvin. Same here. I can get this code to run without error, but subsequent downloads do not appear in the “path” location.