{{announcement.body}}
{{announcement.title}}

Scala: Change Chrome's Default Download Path in Selenium

DZone 's Guide to

Scala: Change Chrome's Default Download Path in Selenium

In this article, see how to change Chrome's default download path in Selenium.

· Java Zone ·
Free Resource

Many of us have faced a situation where we need to automate a download scenario in our tests. To automate this, the first challenge is to override the default download path of the browser. Let's see how to achieve this in Scala.

ChromeOptions

Selenium itself comes with a simple solution to overcome this by having a library called ChromeOptions. Since Scala runs on JVM, Scala and Java can be mixed for a seamless experience, hence we can also use the same Java library in Scala.

Though it looks simple, the actual challenge comes when we set the preference and pass the arguments.

Preference — download.default_directory

The chrome preference that we need to set is download.default_directory. This should be a Map or HashMap. We are not using the Scala mutable library - Map. Instead, we have used the Java library directly. This is one advantage of Scala. Scala and Java are easily mixed. 

Scala
 







The next step is to pass this preference to chrome options. setExperimentalOptions method sets this as an option to chrome driver. 

Below is the Scala snippet to change the path using ChromeOptions.

Scala
 







With ChromeOptions and Desired Capabilities

Many of us would be using DesiredCapabilites instead of ChromeOptions. If you are using Desired Capabilities, then ChromeOption should be passed as one of the capabilities to chromedriver. 

Let's say I am using DesiredCapabilities to ignore SSL certificate errors. To combine both, the code looks like below: 

Scala
 







That's it! We are set to download files in our new path. 

To Delete a Directory

It is always best practice to leave our test and test code in a state as before the execution. So it is always a best practice to delete the files that we create as a part of our tests.

The below code deletes the entire folder.

Scala
 







Thanks for reading!

Topics:
chrome ,chromeoptions ,java ,scala ,selenium ,tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}