0

I was using my own code launch the driver hence used below code to set default download directory

DesiredCapabilities caps = DesiredCapabilities.chrome();
ChromeOptions co = new ChromeOptions();
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("download.default_directory", System.getProperty("user.dir")+"\\downloads\\); -------------Dynamic Path
co.setExperimentalOption("prefs", chromePrefs);
capabilities.merge(co);

WebDriver driver = new ChromeDriver(caps);

Now our organization has provided an internal framework that uses their inbuilt driver. The only way to set capabilities is through the JSON profile.

{
    "capabilities": {
        "browserName": "chrome",
        "browserVersion": "81",
        "chromeOptions": {
            "args": [
                "--headless"
                "--start-maximized"
            ]
        }
        "prefs": {
            "profile.default_content_settings.popups": 0,
            "download.default_directory": "C:\Users\workspace\project\downloads\" -------------Path is Static
        },
        
    }
}

Hardcoded the download path and changing manually in every machine I run. Is there any way to make this path Dynamic same as above?

Swaroop Humane
  • 1,770
  • 1
  • 7
  • 17
Lakshmipathi G
  • 149
  • 3
  • 13
  • Where are you exactly stuck?What happens when you run the code on different machines? What exactly you are trying to do? – undetected Selenium Aug 26 '20 at 12:50
  • 2
    I think you should contact the support of that internal framework since nobody can know its specifics. – Alexey R. Aug 26 '20 at 13:18
  • @DebanjanB Project Directory may differ in each machine. SO, I need to make that path as dynamic. i.e., need to set it as Project directory instead of passing static path. – Lakshmipathi G Aug 28 '20 at 07:32

1 Answers1

0

I had a similar problem trying to pass a relative path to my firefox.opions. Since it only seems to accept absolute paths i chose a workaround:

I converted my relative path to an absolute one and then passed it with Firefox Options to the browser preferences:

/* downloadPath=./src/test/resources/downloads -->from properties */    
File directory = new File(downloadPath); 
String absoluteDwnldPath = directory.getAbsolutePath();

later:

options.addPreference("browser.download.dir", absoluteDwnldPath);
options.addPreference("browser.download.useDownloadDir", true);

Now for your problem: Make the path in the json-file relative, e.g. "./downloads" and then write the code to get the absolute path internally like above and pass the option afterwards to your chrome preferences.

If you deliver your testing software it seems there is always a downloads-directory inside your project, so it should always be found.

slowlert
  • 21
  • 3