How to validate if file is offered for download

I run a script where a download button is pressed.
Is it possible to test if the file is downloaded (verify the existence of the file)?

Thanks in advance

I see there appears to be no response to your question since September 2017. Were you ever able to figure this out because I have the same question.

Perhaps would help you somehow

Is exists a forums better than this about Katalon? Here rarely someone something answers

Because I’ve seen Supoj answer the question properly, so therefore I won’t give you any more comments related to that. Hope for your understanding :slight_smile:

What do you mean Alex?

I’ve tried the same exact download examples from Katalon and DZone and both seem to no longer work. The Save browser dialog does not go away and the file never get’s saved locally. Am using FF 52 version and Katalon 5.6. Any ideas?

https://www.katalon.com/resources-center/tutorials/handle-file-uploads/

https://dzone.com/articles/how-to-handle-file-uploads-with-katalon-studio

In case if someone is still looking for answers
1. You have to set your browser so that it gets set to accept a file download (make sure that you use this browser for your file download test cases). I am using Chrome.

‘Set Chrome broswer properties for File download’

System.setProperty(“webdriver.chrome.driver”, DriverFactory.getChromeDriverPath());

    String downloadFilepath = "C:/Downloads";

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

    chromePrefs.put("profile.default\_content\_settings.popups", 0);

    chromePrefs.put("download.default_directory", downloadFilepath);

    chromePrefs.put("safebrowsing.enabled", "true"); 

    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);

‘Change driver to use the above driver’

DriverFactory.changeWebDriver(driver)

‘Maximize Browser Window’

WebUI.maximizeWindow()

‘Navigate to Method URL’

WebUI.navigateToUrl(‘YourURL’)

2.Set your downloadFilepath and your expected fileName that will get downloaded in this test case

downloadFilepath = ‘C:/Downloads’

fileName = ‘Installer.exe’

3.You could use an Assertion statement to verify if the file was downloaded and deleted.
//Method that is expected to return True if the fileName is equal to the one in the downloadFilepath
Assert.assertTrue(isFileDownloaded(downloadFilepath, fileName))

4. Create the below Method in your test case and call this Method.
//a. This Method will create directory if it does not exists. In this case it will create ‘C:/Downloads’ //defined above in step 2.
//b. If the file name is equal this will delete the file from within the directory
‘isFileDownloaded Method’

Boolean isFileDownloaded(String downloadFilepath, String fileName) {

File dir = new File(downloadFilepath)



if (!(dir.exists())) {

    dir.mkdirs()

}

File\[\] dirContents = dir.listFiles()



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

    if (dirContents\[i\].getName().equals(fileName)) {

        dirContents\[i\].delete()



        return true

    }

}

return false

}

Hi Everybody. How can I handle if the file name is not hardcoded? most of the name is generated usind date and time when I download the file.

Regards

hi,
get the latest filename from the download folder

//get latest downloaded fileName
def f = getLastDownloadedFile(downloadPath)
println "latest downloaded file name is: "+f.getName()

public File getLastDownloadedFile(String downloadPath) {
	File latest = null;
	try {
		File fl = new File(downloadPath);
		File[] files = fl.listFiles(new FileFilter() {
			public boolean accept(File file) {
				return file.isFile();
			}
		});
		//Sleep to find latest
		Thread.sleep(10000);
		long lastMod = Long.MIN_VALUE;

		for (File file : files) {
			if (file.lastModified() > lastMod) {
				latest = file;
				lastMod = file.lastModified();
			}
		}
	} catch (Exception e) {
		System.out.println("Exception while getting the last download file :"+ e.getMessage());
	}
	println("The last downloaded file is " + latest.getPath());
	return latest;
}