How to override the default file download directory in chrome at runtime using RunConfiguration.setWebDriverPreferencesProperty?

Hi,

I am trying to run some tests where I need to download a file to a specific directory and check the file.
Before downloading the file, I tried to override the desired capabilities using “RunConfiguration.setWebDriverPreferencesProperty” but i seem to be passing the wrong arguments. Please help me with the right args for this function call.

Also let me know if “RunConfiguration.setWebDriverPreferencesProperty()” needs to be called before “WebUI.openBrowser()” statement or it could be a written in a function that is called as a utility whenever needed.

At present, I have used it in the listener as per the below code-

==============================================================================
import com.kms.katalon.core.configuration.RunConfiguration

class LoginIn
{
@BeforeTestCase
def sampleBeforeTestCase(TestCaseContext testCaseContext)
{

	String ProjectDirectory=RunConfiguration.getProjectDir()
	String DownloadFolderPath=ProjectDirectory+"/ExcelExports"
	
	**RunConfiguration.setWebDriverPreferencesProperty("download.default_directory", DownloadFolderPath)**
	
            WebUI.delay(2)
	WebUI.openBrowser(URL)
	WebUI.maximizeWindow(FailureHandling.STOP_ON_FAILURE)
}	
}

==============================================================================

hello,

here are some options listed

public WebDriver setChromeOptions(File folder){
	
	ChromeOptions options = new ChromeOptions();
	String downloadPath = folder.getAbsolutePath()
	//String downloadsPath = System.getProperty("user.home") + "/Downloads";
	println ("downloadpath "+downloadPath)
	
	Map<String, Object> chromePrefs = new HashMap<String, Object>()
	chromePrefs.put("profile.default_content_settings.popups", 0);
	chromePrefs.put("download.default_directory", downloadPath)
	chromePrefs.put("download.prompt_for_download", false)
	chromePrefs.put("plugins.plugins_disabled", "Chrome PDF Viewer");
	options.addArguments("--headless")
	options.addArguments("--window-size=1920,1080")
	options.addArguments("--test-type")
	options.addArguments("--disable-gpu")
	options.addArguments("--no-sandbox")
	options.addArguments("--disable-dev-shm-usage")
	options.addArguments("--disable-software-rasterizer")
	options.addArguments("--disable-popup-blocking")
	options.addArguments("--disable-extensions")
	options.setExperimentalOption("prefs", chromePrefs)
	DesiredCapabilities cap = DesiredCapabilities.chrome()
	cap.setCapability(ChromeOptions.CAPABILITY, options)
	cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
	
	System.setProperty("webdriver.chrome.driver", DriverFactory.getChromeDriverPath())
	WebDriver driver = new ChromeDriver(cap);
	return driver
}
1 Like

Thank you very much !!

The above suggested code worked. Although I encountered some error.
#1 After setting the above Browser capabilities, I used ‘WebUI.openBrowser()’ to launch the browser instance which removed all the capabilities.
#2 My download path contained ‘/’ instead of ‘\’ because of which is saw ‘Failed - Download Error’ after clicking the file download button.

Here is the altered code that worked for me-

           //Set Browser Capabilities.
	String ProjectDirectory=RunConfiguration.getProjectDir()
	String DownloadFolderPath1=ProjectDirectory+"/Downloads"
	String DownloadFolderPath=DownloadFolderPath1.replace('/', '\\')
	String AppURL=GlobalVariable.MyAppURL
				
	Map<String, Object> chromePrefs = new HashMap<String, Object>()
	chromePrefs.put("profile.default_content_settings.popups", 0);
	chromePrefs.put("download.default_directory", DownloadFolderPath)
	chromePrefs.put("download.prompt_for_download", false)
	chromePrefs.put("plugins.plugins_disabled", "Chrome PDF Viewer");
	ChromeOptions options=new ChromeOptions();

	//options.addArguments("--headless")
	//options.addArguments("--window-size=1920,1080")
	options.addArguments("--test-type")
	//options.addArguments("--disable-gpu")
	options.addArguments("--no-sandbox")
	//options.addArguments("--disable-dev-shm-usage")
	options.addArguments("--disable-software-rasterizer")
	options.addArguments("--disable-popup-blocking")
	options.addArguments("--disable-extensions")
	options.setExperimentalOption("prefs", chromePrefs)

	DesiredCapabilities cap = DesiredCapabilities.chrome()
	cap.setCapability(ChromeOptions.CAPABILITY, options)
	cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true)
	System.setProperty("webdriver.chrome.driver", DriverFactory.getChromeDriverPath())
	WebDriver driver = new ChromeDriver(cap);		
	driver.get(AppURL)
	driver.manage().window().maximize()
	DriverFactory.changeWebDriver(driver)

            //After this perform the actions using functions available in "WebUI." 

Thank you again for this help, much appreciated !!

Hi,
#1 : Use

WebDriver driver = new ChromeDriver(cap)
DriverFactory.changeWebDriver(driver)
WebUI.navigateToUrl('your/url')

#2 : See Forward slashes in RunConfiguration.getProjectDir cause issues

Thank you Helene, I had already made the necessary changes (They are present in the code I posted in the reply to Timo_Kuisma2)
I was just highlighting the mistakes I did.

Thanks for the help… :grinning:

Hi Experts,
I fixed my code for customization in default download path and it worked fine so far, now i have bit different scenario where my file getting downloaded at default path for this case only. please help -
below is image , first i click on Tools and then go for Export, while click on export for fraction of sec new tab gets open in same browser which and forms url with few additional details , like file name and sessions along with mail url and then it download the file.


code for setting download path-
WebUI.openBrowser(‘’)

	driver = DriverFactory.getWebDriver()
	Capabilities cap = ((SmartWaitWebDriver) driver).getCapabilities()
	String browserName =  cap.getBrowserName()
	println("Browser Name is "+ browserName)

	//WebUI.closeBrowser()
	println("Driver new is "+ driver)
	long startTime = System.currentTimeMillis()

	File downloadPathFile = new File(RunConfiguration.getProjectDir()+ '/' +'download_Upload/')
	if (!downloadPathFile.exists()) {
		downloadPathFile.mkdir();
	}

	String downloadPath = downloadPathFile.getAbsolutePath()
	println("The file Download path is : " +downloadPath)
	Map<String, Object> browserPrefs = new HashMap<String, Object>()

	if(String.valueOf(browserName).contains('chrome')) {
		println("Chrome found")
		System.setProperty('webdriver.chrome.driver', DriverFactory.getChromeDriverPath())
		println("declaring ChromeOptions")
		ChromeOptions options = new ChromeOptions()
		browserPrefs.put('download.default_directory', downloadPath)
		browserPrefs.put('download.directory_upgrade', true)
		browserPrefs.put('download.prompt_for_download', false)
		options.setExperimentalOption('prefs', browserPrefs)
		println("calling change web driver")
		driver=new ChromeDriver(options)

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

Hi dear…

I am also using katalon i want to download the PDF. that pdf


I have to click on that highlighted arrow can the above code w ill work using desired capabilities ?.

I really need this urgent i am badly stuck.

Getting the below error

2020-07-14 13:32:01.503 ERROR k.k.c.m.CustomKeywordDelegatingMetaClass - :x: No signature of method: com.pdf.reader.ReadPdfFromBrowser.setChromeOptions() is applicable for argument types: (java.lang.String) values:
Possible solutions: setChromeOptions(java.io.File)
2020-07-14 13:32:01.511 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/Creation of Quote Fnctionality/Create Domestic Quote - with PDF FAILED.
Reason:
org.codehaus.groovy.runtime.metaclass.MissingMethodExceptionNoStack: No signature of method: com.pdf.reader.ReadPdfFromBrowser.setChromeOptions() is applicable for argument types: (java.lang.String) values:
Possible solutions: setChromeOptions(java.io.File)

I have put my code on custom keyword @Keyword
public WebDriver setChromeOptions(File folder){

	ChromeOptions options = new ChromeOptions();
	options.addArguments("window-size=1680x1050");
	Map<String, Object> prefs = new HashMap<String, Object>();
	String downloadFilepath = System.getProperty("user.dir") + "\\pdf";
	prefs.put("profile.default_content_settings.popups", 0);
	prefs.put("download.default_directory", downloadFilepath);
	prefs.put("credentials_enable_service", false);
	prefs.put("profile.password_manager_enabled", false);
	options.setExperimentalOption("prefs", prefs);
	DesiredCapabilities cap = DesiredCapabilities.chrome();
	cap.setCapability(ChromeOptions.CAPABILITY, options);
	WebDriver driver = new ChromeDriver(cap);
	
}

}

Then pass the keyword in the test case as
CustomKeywords.‘com.pdf.reader.ReadPdfFromBrowser.setChromeOptions’( )

Please suggest me