0

I use WebDriverManager.chromedriver().setup(); for getting chrome property in my selenium tests. It works fine. I am trying to download a file by changing the default download location of chrome browser because I want to download the file to my java project classpath, rather than to my local machine, but I am unsure if WebDriverManager has such an implementation. Currently, I am trying something like this:

    WebDriverManager.chromedriver().setup();
    String downloadDir = System.getProperty("user.dir");
    HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
    chromePrefs.put("download.default_directory", downloadDir);
    ChromeOptions options = new ChromeOptions();
    options.setExperimentalOption("prefs", chromePrefs);
    DesiredCapabilities cap = DesiredCapabilities.chrome();
    cap.setCapability(ChromeOptions.CAPABILITY, options);
    driver = new ChromeDriver(cap);

The code block works and downloads the file to the project classpath, as expected, but I think there would be a cleaner and shorter way to do it. I have done some research on the Bonigarcia WebDriverManagerdependency and some of its implementations but can't find anything helpful. Is there a better way to achieve the above?

The man
  • 129
  • 16

1 Answers1

0

Use the method targetPath() to change the default location of the driver downloaded by WebDriverManager:

WebDriverManager.chromedriver().targetPath("/my/custom/path").setup();
Boni García
  • 4,618
  • 5
  • 28
  • 44
  • I tried your suggesstion this way but the file still got dowmloaded in my local machine:`WebDriverManager.chromedriver().targetPath(System.getProperty("user.dir")).setup()`. Is there something else I need to do? – The man Nov 14 '19 at 10:42