Update Chrome Capabilities at runtime

Hi,

My goal is to update the Download path of my Chrome so that I know where to get the download file. This will be run on a VM so the path cannot be hardcoded

Desired download path = ProjectDir + ‘Download’ folder

Also needed to also have safebrowsing.enabled as true to keep the popup “This type of file can harm your computer” away

I tried to manually set everything at Project > Settings > Desired Caps > WebUI > Chrome:

And you can also check it in the com.kms.katalon.core.webui.chrome.properties
{"CHROME_DRIVER":{"prefs":{"download.default_directory":"C:\Users\btp\WorkSpace\btp-mlab\Downloads","safebrowsing.enabled":true}}}

So if I just run my test case it will run smoothly without issue.

HOWEVER, since I cannot hardcode the path since my test cases will be run in a VM I need to update the com.kms.katalon.core.webui.chrome.properties at runtime.

So I have this code:
@Keyword
def updateDownloadPath() {
String strDownloadsPath = RunConfiguration.getProjectDir() + ‘/Download’

		strDownloadsPath = strDownloadsPath.replace('/', '\\')
			
		String data = '{"CHROME_DRIVER":{"prefs":{"download.default_directory":"' + strDownloadsPath + '","safebrowsing.enabled":true}}}'
			
		FileUtils.writeStringToFile(file, data, Charset.defaultCharset(), false)
	}

This will overwrite everything in com.kms.katalon.core.webui.chrome.properties

IF I call this method @BeforeTestCase it will just be ignored the hardcoded data above will still be what Katalon will use. Also I noticed after the runtime the Capabilities disappear

IF I call this method before I click the download button still it is ignored.

Did I miss an important thing for this to work? Will really appreciate if you can help me. Thank you!

You did not show us how the file variable is defined.

What is com.kms.katalon.core.webui.chrome.properties?
Is it a file on disk?
Is it a Groovy class name?
Or something else?

You update a file
<projectdir>/settings/internal/com.kms.katalon.core.webui.chrome.properties
in the hope that the updated file will be used by Katalon Studio to open a Chrome browser.

I do not think this approach would work. Forget that file.

You should learn how to instantiate ChromeDriver with Desired Capabilities you want. See Guru99; Chrome Options & Desired Capabilities in Selenium: Headless, AdBlocker, Incognito
and other articles that tell you how to start Chrome browser yourself.

Once you have got a Chrome browser in action, you can let Katalon Studio to use that ChromeDriver instance with WebUI.* keyword. Just do

ChromeDriver chrome = ....
DriverFactory.changeWebDriver(chrome)

See https://docs.katalon.com/javadoc/com/kms/katalon/core/webui/driver/DriverFactory.html#changeWebDriver(org.openqa.selenium.WebDriver)

Oh I see! Will definitely try this out. Thanks for the helpful input as always @kazurayam

I just did this combined with your article here:

Steps:

  1. Removed the Capabilities I setup above that failed
  2. Added your CustomWebDriverFactory
  3. Added the capabilities I needed:

HashMap<String, Object> chromePrefs = new HashMap<String, Object>()
String strDownloadPath = RunConfiguration.getProjectDir() + GlobalVariable.Execution_DownloadPath
strDownloadPath = strDownloadPath.replace(’/’, ‘\’)
chromePrefs.put(“download.default_directory”, strDownloadPath)
chromePrefs.put(“safebrowsing.enabled”, true)
ChromeOptions options = new ChromeOptions()
options.setExperimentalOption(“prefs”, chromePrefs)

  1. Updated my @BeforeTestCase with this:
WebDriver driver = CustomKeywords.'ck.CustomWebDriverFactory.createWebDriver'()
DriverFactory.changeWebDriver(driver)
//WebUI.openBrowser('')
WebUI.maximizeWindow()

Its much easier now. Thank you so much @kazurayam glad to have you in this community. Cheers!