Unable to download file to specific folder

Hello,

I am trying to verify the downloaded file. But, I am unable to download the file to a specific folder. It is going into default downloads. Please, someone, help me.

@Keyword

public void example_VerifyDownloadWithFileName() {

String downloadFilepath = “E:\\seleniumdownloads”;

HashMap<String, Object> chromePrefs = new HashMap<String, Object>();

chromePrefs.put(“profile.default_content_settings.popups”, 0);

chromePrefs.put(“download.default_directory”, downloadFilepath);

ChromeOptions options = new ChromeOptions();

options.setExperimentalOption(“prefs”, chromePrefs);

DesiredCapabilities cap = DesiredCapabilities.chrome();

cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

cap.setCapability(ChromeOptions.CAPABILITY, options);

driver.findElement(By.linkText(“mailmerge.xls”)).click();

Assert.assertTrue(isFileDownloaded(downloadFilepath, “mailmerge.xls”), “Failed to download Expected document”);

}

public boolean isFileDownloaded(String downloadPath, String fileName) {

boolean flag = false;

File dir = new File(downloadPath);

File dir_contents = dir.listFiles();

for (int i = 0; i < dir_contents.length; i++) {

if (dir_contents[i].getName().equals(fileName))

return flag=true;

}

return flag;

}

TC:

WebUI.openBrowser(‘Excel Templates [Free Download]’)

WebUI.maximizeWindow()

//mailMerge = //h1[text()=‘Mail Merge Demo’]/following-sibling::ul//b[text()=‘Download:’]/following-sibling::a

WebUI.click(findTestObject(‘mailMerge’))

CustomKeywords.‘com.prime.FileDownloadVerify.example_VerifyDownloadWithFileName’()

Have a look at https://docs.katalon.com/katalon-studio/tutorials/using_selenium_webdriver_katalon_studio.html

I don’t think it is possible to use custom DesiredCapabilities on an already launched webdriver. I think you should create a new webdriver to use it, using “BeforeTestCase” on Test Listeners (available on latest release). Somethink like :

	@BeforeTestCase
	def sampleBeforeTestCase(TestCaseContext testCaseContext) {

	System.setProperty("webdriver.chrome.driver", DriverFactory.getChromeDriverPath())
	String downloadFilepath = "C:\\Users\\Administrateur\\Documents\\Test"
	
	HashMap<String, Object> chromePrefs = new HashMap<String, Object>()		
	chromePrefs.put("profile.default_content_settings.popups", 0)
	chromePrefs.put("download.default_directory", downloadFilepath)
	
	ChromeOptions options = new ChromeOptions()
	options.setExperimentalOption("prefs", chromePrefs)
	
	DesiredCapabilities cap = DesiredCapabilities.chrome()	
	cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true)	
	cap.setCapability(ChromeOptions.CAPABILITY, options)
					
	WebDriver driver = new ChromeDriver(cap)		
	DriverFactory.changeWebDriver(driver)
	}

WebUI.navigateToUrl(‘https://www.putty.org/’)
WebUI.click(findTestObject(‘Object Repository/Page_Download PuTTY - a free SSH an/a_here’))
WebUI.click(findTestObject(‘Object Repository/Page_Download PuTTY latest release/code_putty-0.70.tar.gz’))
WebUI.delay(2)
WebUI.closeBrowser()

Working for me. You should add a loop to wait until your file is download (to avoid the WebUI.delay).

Also, please use navigateToUrl and not openBrowser (or it will fail).

Have also a look at this solution, which might be better and easier, as it uses native driver :

Try the following steps to control the folder in which Chrome is downloading the files through test case:

1.) Open Project > Settings > Desired Capabilities > Web UI > Chrome:

2.) Click the ‘Add’ button, and set the name of the new property to ‘prefs’, the type to ‘Dictionary’:

3.) In the value column, click on the ellipses, and set name = ‘download.default_directory’, type = String, value = ‘my/downlaod/directory’:

  1. Add following keyword in any of the utilities and call it before opening the browser:

@Keyword

def setDownloadPath() {

HashMap<Object, String> chromePrefs = new HashMap<Object, String>();

chromePrefs.put(“download.default_directory”, RunConfiguration. getProjectDir () + “/Reports/”)

RunConfiguration. setWebDriverPreferencesProperty (“prefs”, chromePrefs)

}

In this keyword, directory has been set as ${Project Directory}/Reports. You can modify this as well by adding specific folder in second argument of chromPrefs.put().

Thanks for your reply. Will implement and let you know if there is any issue

Hello,

will this config work in gitlab?

Thanks

Hi @trojanhrse_pr, the path had been set successfully. After executing, it shows download failed.
image