Is it possible to add relative path in Desired Capabilities?
Hi @discover.selenium.
No, it’s not possible to put there a relative path. I noticed this shortcoming when I was trying the same for my use case: A custom download path.
A workaround I found is using Test Listeners somewhere on this community is to set desired capabilities during run-time just before the browser is actually opened.
An example of my code that you should be able to adjust to your needs below (I trimmed it a bit down as it’s contains more, but isn’t valuable for your specific question). Nutshell: I have a global variable (profile) value called “downloadPath” which leads to a folder within my project repo. So it will run locally and in any build pipeline. These settings below are for chrome.
Hope this helps!
Regards,
Joost
import com.kms.katalon.core.annotation.BeforeTestCase
import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.util.KeywordUtil
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.Files
import java.io.File
import internal.GlobalVariable
class BeforeTc3SetSpecificDownloadDirectoryIfNeeded {
/**
* Executes before every test case starts.
*/
@BeforeTestCase
def setSpecificDownloadDirectoryIfNeeded() {
Map desiredCapabilities = RunConfiguration.getDriverPreferencesProperties("WebUI")
String projectDir = RunConfiguration.getProjectDir()
Path projectPath = Paths.get(projectDir)
Path downloadPath = projectPath.resolve(GlobalVariable.downloadPath)
// you do not need / and \ here, which works on OS Windows & Linux!
def customizedDownloadDirectory = downloadPath.toString()
//CHROME: http://forum.katalon.com/t/is-this-possible-to-use-katalon-to-download-a-file/46318/4
Map prefs = desiredCapabilities.get("prefs")
if (prefs == null) {
prefs = [:]
}
KeywordUtil.logInfo("BeforeTc3SetSpecificDownloadDirectoryIfNeeded: We detected test name contains \"download\" so we get current desiredCapabilities prefs =" + prefs.toString())
KeywordUtil.logInfo("BeforeTc3SetSpecificDownloadDirectoryIfNeeded: ...and set desiredCapabilities to work with a customized download directory = RunConfiguration.getProjectDir() + / + GlobalVariable.downloadPath = : $customizedDownloadDirectory")
prefs.put("download.default_directory", customizedDownloadDirectory)
prefs.put("download_dir", customizedDownloadDirectory)
prefs.put("download.directory_upgrade", true)
prefs.put("download.prompt_for_download", false)
prefs.put("plugins.always_open_pdf_externally", true)
prefs.put("profile.default_content_settings.popups", "0")
prefs.put("profile.content_settings.exceptions.automatic_downloads.*.setting", "1")
RunConfiguration.setWebDriverPreferencesProperty("prefs", prefs)
RunConfiguration.setWebDriverPreferencesProperty("args", ["--no-sandbox", "--disable-dev-shm-usage"])
RunConfiguration.setWebDriverPreferencesProperty("useAutomationExtension", "false")
Thank you @joost.degeyndt
Hi Joost,
Just wondering, this may be related to set it on WebUI, how about on remote? Can I use this to set it up to remote as we are running the TestOps Katalon - TestSuite Scheduler to apply this desired download path…
Hi there,
Thank you very much for your topic! It may take a little while before Katalon team member or others forum members respond to you.
In the meantime, you can double-check your post to see if you can add any extra information i.e. error logs, HTML codes, screenshots, etc. Check out this posting guide to help us help you better!
Thanks!
Katalon Community team
Hi @bryan.pagtananan1 ,
Wow, 3 year old post
I am not entirely sure with what you mean with “remote”, but the piece of code above is meant to set the download folder relative to the Katalon project folder. So that folder should get’s pushed to the repo and if your test case does an action to trigger a download it should end up in that folder, regardless whether you run it locally on your machine or whether it’s trigger via TestOps & rans on any agent on a different machine…
FYI, I do notice I expanded my script with some fallback, just in case the directory is not there (anymore).
//Let's make sure the directory is available!
//It can happen that GIT removed the folder because it's empty I've noticed. So locally the folder is there and tests run fine, but once on the CI linux build agent it's not, causing havoc! Build runs for hours and nothing happens!
if (Files.notExists(downloadPath)) {
KeywordUtil.markWarning("The directory downloadPath $downloadPath does NOT exist! We do a create by executing command newDirectory.mkdir() on File newDirectory from string value customizedDownloadDirectory = $customizedDownloadDirectory! ")
File newDirectory = new File(customizedDownloadDirectory)
newDirectory.mkdir()
if (!newDirectory.exists()) {
KeywordUtil.markFailedAndStop("Oh-oh... Directory still doesn't exist! No need to go further as our download will never work! newDirectory.isDirectory() returns: " + newDirectory.isDirectory())
}
}
Actually was curious on this article Set desired capabilities for TestCloud environment | Katalon Docs it says “For execution triggered by TestOps scheduler” and I was assuming 1.b is applicable for Windows - Chrome environment which our project is currently using. To make story short, I was able to apply this on my Local Katalon Studio Enterprise Version 9.6.0 and when I run on TestOps, the data seems not recognizeable as my keyword that prints the content of the downloaded csv is not updating the data file…But on local it is accurately working, so I suspecting that the setting of download path does not working on TestOps scheduler…I have a TestListener to set the download path before Test Suite since our structure has 2 Streams (Test Suites) that runs in parallel mode…Below is the sample that I am using which atm its on WebUI
class TestListener {
/**
* Executes before every test suite starts.
* @param testSuiteContext: related information of the executed test suite.
*/
@BeforeTestSuite
def SetDownloadPath (TestSuiteContext testSuiteContext) {
if((testSuiteContext.getTestSuiteId()) == GlobalVariable.Suite_ID) {
Map desiredCapabilities = RunConfiguration.getDriverPreferencesProperties("WebUI")
String projectDir = RunConfiguration.getProjectDir()
Path projectPath = Paths.get(projectDir)
Path downloadPath = projectPath.resolve(GlobalVariable.downloadPath)
// you do not need / and \ here, which works on OS Windows & Linux!
def customizedDownloadDirectory = downloadPath.toString()
//CHROME: http://forum.katalon.com/t/is-this-possible-to-use-katalon-to-download-a-file/46318/4
Map prefs = desiredCapabilities.get("prefs")
if (prefs == null) {
prefs = [:]
}
KeywordUtil.logInfo("SetDownloadPath: We detected test name contains \"download\" so we get current desiredCapabilities prefs =" + prefs.toString())
KeywordUtil.logInfo("SetDownloadPath: ...and set desiredCapabilities to work with a customized download directory = RunConfiguration.getProjectDir() + / + GlobalVariable.downloadPath = : $customizedDownloadDirectory")
prefs.put("download.default_directory", customizedDownloadDirectory)
prefs.put("download_dir", customizedDownloadDirectory)
prefs.put("download.directory_upgrade", true)
prefs.put("download.prompt_for_download", false)
prefs.put("plugins.always_open_pdf_externally", true)
prefs.put("profile.default_content_settings.popups", "0")
prefs.put("profile.content_settings.exceptions.automatic_downloads.*.setting", "1")
RunConfiguration.setWebDriverPreferencesProperty("prefs", prefs)
RunConfiguration.setWebDriverPreferencesProperty("args", [
"--no-sandbox",
"--disable-dev-shm-usage"
])
RunConfiguration.setWebDriverPreferencesProperty("useAutomationExtension", "false")
} else {
Map desiredCapabilities2 = RunConfiguration.getDriverPreferencesProperties("WebUI")
String projectDir2 = RunConfiguration.getProjectDir()
Path projectPath2 = Paths.get(projectDir2)
Path downloadPath2 = projectPath2.resolve(GlobalVariable.downloadPath_2)
// you do not need / and \ here, which works on OS Windows & Linux!
def customizedDownloadDirectory2 = downloadPath2.toString()
//CHROME: http://forum.katalon.com/t/is-this-possible-to-use-katalon-to-download-a-file/46318/4
Map prefs2 = desiredCapabilities2.get("prefs")
if (prefs2 == null) {
prefs2 = [:]
}
KeywordUtil.logInfo("SetDownloadPath2: We detected test name contains \"download\" so we get current desiredCapabilities prefs =" + prefs2.toString())
KeywordUtil.logInfo("SetDownloadPath2: ...and set desiredCapabilities to work with a customized download directory = RunConfiguration.getProjectDir() + / + GlobalVariable.downloadPath_2 = : $customizedDownloadDirectory2")
prefs2.put("download.default_directory", customizedDownloadDirectory2)
prefs2.put("download_dir", customizedDownloadDirectory2)
prefs2.put("download.directory_upgrade", true)
prefs2.put("download.prompt_for_download", false)
prefs2.put("plugins.always_open_pdf_externally", true)
prefs2.put("profile.default_content_settings.popups", "0")
prefs2.put("profile.content_settings.exceptions.automatic_downloads.*.setting", "1")
RunConfiguration.setWebDriverPreferencesProperty("prefs", prefs2)
RunConfiguration.setWebDriverPreferencesProperty("args", [
"--no-sandbox",
"--disable-dev-shm-usage"
])
RunConfiguration.setWebDriverPreferencesProperty("useAutomationExtension", "false")