Customize Download location using script other than UI settings

Hi Team

Can you tell me how I can set the value to a variable, I need to set this using script instead of WebUI configuration. As my script should run on vm and should pick the customized path that i set in script and download file in that folder.

Hi

Can some one give exact script for the customize download location?

import org.openqa.selenium.chrome.ChromeDriver as ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions as ChromeOptions
import com.kms.katalon.core.webui.driver.DriverFactory as DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

String downloadFilepath = 'C:\\Users\\XXX\\Path\\'

System.setProperty('webdriver.chrome.driver', DriverFactory.chromeDriverPath)

ChromeOptions options = new ChromeOptions();

Map<String, Object> prefs = new HashMap<String, Object>();

prefs.put("download.default_directory", downloadFilepath);

options.setExperimentalOption("prefs", prefs);

WebDriver driver = new ChromeDriver(options)

DriverFactory.changeWebDriver(driver)

WebUI.navigateToUrl('https://testfiledownload.com/test-file-generator/')

WebUI.maximizeWindow()

NOTE :

  • Chrome disallows using certain directories for download. In particular, you cannot use the desktop folder as the download directory. On Linux, you also cannot use the home directory for download. Since the exact list of forbidden directories is subject to change, it is recommended that you use a directory that has no special meaning to the system.
  • ChromeDriver does not automatically wait for download to complete. If you call driver.quit() too soon, Chrome might terminate before the download has finished.
  • Relative paths do not always work. For best result, use full path instead.
  • On Windows, Use “\” as path separators. Using “/” is not reliable on Windows.

Hi Team

With the above code file is not downloading in customized directory. It’s by default download under ‘Download’ folder.

Script:

WebDriver driver = DriverFactory.getWebDriver()

	ChromeOptions options = new ChromeOptions();

	Map<String, Object> prefs = new HashMap<String, Object>();

	prefs.put("download.default_directory", "/Users/i339884/Documents/sreekumar/product/automation");
	
	options.setExperimentalOption("prefs", prefs);

	WebUI.click(findTestObject('Object Repository/Page_Ariba Spend Management/a_View Details'))

Console:
022-11-01 05:18:14.499 DEBUG testcase.PublishCW - 12: driver = getWebDriver()
2022-11-01 05:18:14.500 DEBUG testcase.PublishCW - 13: println(driver)
com.kms.katalon.core.webui.driver.SmartWaitWebDriver@4b790d86
2022-11-01 05:18:14.513 DEBUG testcase.PublishCW - 14: options = new org.openqa.selenium.chrome.ChromeOptions()
2022-11-01 05:18:14.545 DEBUG testcase.PublishCW - 15: println(options)
Capabilities {browserName: chrome}
2022-11-01 05:18:14.566 DEBUG testcase.PublishCW - 16: prefs = new java.util.HashMap()
2022-11-01 05:18:14.567 DEBUG testcase.PublishCW - 17: println(prefs)
[:]
2022-11-01 05:18:14.568 DEBUG testcase.PublishCW - 18: prefs.put(“download.default_directory”, “/Users/i339884/Desktop/”)
2022-11-01 05:18:14.569 DEBUG testcase.PublishCW - 19: options.setExperimentalOption(“prefs”, prefs)
2022-11-01 05:18:14.573 DEBUG testcase.PublishCW - 20: println(prefs)
[download.default_directory:/Users/i339884/Desktop/]
2022-11-01 05:18:14.575 DEBUG testcase.PublishCW - 21: click(findTestObject(“Object Repository/Page_Ariba Spend Management/a_View Details”))

@sreekumar.narayana

Your code is doing something completely different from what @discover.selenium told us. I am not surprised to hear that your code does not work.

@discover.selenium does this:

Do you see what he does here?

@sreekumar.narayana I edited my post to avoid confusion. Please try my solution. The code seems to work perfectly at my end.

@kazurayam

yes it’s working. Thank you