I attempt to change the WebDriver’s download.default_directory
, but for some odd reason, when we click a link to download a PDF, for example, it goes to the user’s Downloads folder instead of that which we specify.
I am using Katalon Studio v8.6.8 on a Windows 11 machine.
Like this:
WebDriverUtils.groovy
package me.mikewarren.myCaseScraper.utils
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
import com.kms.katalon.core.webui.driver.DriverFactory
public final class WebDriverUtils {
public static void SetUpDriver() {
// create if not exists the download directory
File downloadDir = new File(this.GetDownloadDirectory());
if (!downloadDir.exists()) {
downloadDir.mkdirs();
}
System.setProperty('webdriver.chrome.driver', DriverFactory.getChromeDriverPath())
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption('prefs', [
'download.prompt_for_download' : 'false',
'download.default_directory' : downloadDir.getPath(),
]);
WebDriver driver = new ChromeDriver(options);
driver.manage().window().maximize();
DriverFactory.changeWebDriver(driver);
}
public static String GetDownloadDirectory() {
return "./downloads";
}
public static void CloseDriver() {
DriverFactory.closeWebDriver();
}
}
NewTestListener.groovy
import com.kms.katalon.core.annotation.AfterTestCase
import com.kms.katalon.core.annotation.BeforeTestCase
import com.kms.katalon.core.context.TestCaseContext
import me.mikewarren.myCaseScraper.utils.WebDriverUtils
class NewTestListener {
/**
* Executes before every test case starts.
* @param testCaseContext related information of the executed test case.
*/
@BeforeTestCase
def sampleBeforeTestCase(TestCaseContext testCaseContext) {
WebDriverUtils.SetUpDriver();
}
/**
* Executes after every test case ends.
* @param testCaseContext related information of the executed test case.
*/
@AfterTestCase
def sampleAfterTestCase(TestCaseContext testCaseContext) {
// WebDriverUtils.CloseDriver();
}
}
After much head-banging-on-keyboard, I found that the solution was to spell out the absolute pathname of the desired download directory:
public static String GetDownloadDirectory() {
return "${RunConfiguration.getProjectDir()}/downloads";
}