Setting a variable value in Desired Capabilities

Hi,
I want to add this string in the Desired Capabilites
String downloadPath = System.getProperty(“user.dir”) + File.separator + “Downloads”
Can someone help?
I want to set this value for “download.default_directory”.

See if this helps:

1 Like

Thank you @Russ_Thomas for sharing tips.

Potential workaround in this thread:

This is great information, but it does not solve the issue the OP and myself are having. I need all files downloaded from Chrome to go into this folder. The full path is C:\Katalon\KatalonAutomation\LocalDataRepo\downloads.
When running within Katalon and adding in the default.download_directory all is good. The issue is we need to pass the folder to download either using a variable that we have created and use in our tests of:
GlobalVariable.localDataRepo + ‘\downloads\’ or be able to use .\LocalDataRepo\downloads for the string value. Neither of these work and result in Chrome downloading all files into its default download folder in the \users folder structure.

image
Works fine…

image
c.k.k.c.w.util.WebDriverPropertyUtil - User set preference: [‘prefs’, ‘{download.default_directory=.\LocalDataRepo\downloads}’]
Does not and file ends up in default download folder.

image
Does not work either. I have tried many variations and cannot get this working.
The overall reason is we run our tests in ADO and it always has a random project folder. So we cannot use static folder.

I got this working now. I use a keyword that I call at the beginning of test cases that will download files using Chrome. I do not use a test listener as a lot ouf our test cases just run on their own and not in a suite so it makes it easier to work with. You could easily put this code into the @BeforeTestCase in the listener tho.

I have a global.chromePrefs keyword.

package global

import internal.GlobalVariable
import com.kms.katalon.core.configuration.RunConfiguration
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.Files
import java.io.File


class chromePrefs {
	/**
	 * Set Chrome default download directory to the \LocalDataRepo\downloads folder within the project folder.
	 */
	@Keyword
	def downloadDirectory() {

		Map desiredCapabilities = RunConfiguration.getDriverPreferencesProperties("WebUI")
		String projectDir = RunConfiguration.getProjectDir()
		println('projectDir: ' + projectDir)
		Path projectPath = Paths.get(projectDir)
		Path localDataRepoDownloads = projectPath.resolve(GlobalVariable.localDataRepoDownloads)
		// you do not need / and \ here, which works on OS Windows & Linux!
		def downloadDirectory = localDataRepoDownloads.toString()
		println(downloadDirectory)

		Map prefs = desiredCapabilities.get("prefs")
		if (prefs == null) {
			prefs = [:]
		}
		prefs.put("download.default_directory", downloadDirectory)
		RunConfiguration.setWebDriverPreferencesProperty("prefs", prefs)
	}

}

My localDataRepo variable is in the default profile and is ‘LocalDataRepo\downloads’.
Then in my test cases that download I added this:
//Set \LocalDataRepo\downloads as Chrome download folder
CustomKeywords.‘global.chromePrefs.downloadDirectory’()

I tried this running Katalon locally and running ADO pipelines.