How to verify downloaded file?

Thank you B_L for the example, it was clear and easy to implement

katalon won’t even open chrome to run the test for me and i copied that code exactly. Any suggestions?

I got it to run, but the test fails and I dont get an error message in the Log Viewer

Never mind, i fixed it, thanks for the example

@Marek_Melocik is it possible to add FailureHandling.STOP_ON_FAILURE to this case, instead of just println, to make step fails while execution?
Thanks in advance.

You can use
KeywordUtil.markFailedAndStop("your message") or KeywordUtil.markErrorAndStop("your message") with the import import com.kms.katalon.core.util.KeywordUtil

1 Like

that works, thank you

I am using same code, and though file is present it always through exception for not founding the file- below is my code:
public boolean isFileDownloaded(String downloadPath, String fileName) {

	File dir = new File(downloadPath);
	File[] dirContents = dir.listFiles();
	String lastAttempt = '';

	if (dirContents.length > 0) {
		println("cheking for total files")
		for (int i = 0; i < dirContents.length; i++) {
			if (dirContents[i].getName().equals(fileName)) {
				// File has been found, it can now be deleted:
				dirContents[i].delete();
				 KeywordUtil.markPassed(fileName + 'exist in' + downloadPath + 'and was deleted')
                println("got the file")
				return true;
			}

			lastAttempt = dirContents[i].getName().equals(fileName);
		}
		if (lastAttempt != fileName) {
			KeywordUtil.markFailed(fileName + ' does not exist in' + downloadPath)
			return false;
		}
	}
	println("got NO file")
	return false;
}

calling method in script-
@And(“Verify CSV downloaded report”)
def CourseParticipantList_verify(){

	def file_name=(findTestData("Data Files/Student_Connect/Downloadable_Reports").getValue('Value',1 ))
	def String  download_Path= RunConfiguration.getProjectDir()+ '/' +'download_Upload/'
	
	def File_download= download_Path +file_name*/
	println File_download
	println download_Path
	println file_name
	
	println("CSV downloaded")
	
	println("check if file exist " +cus.isFileDownloaded(download_Path,file_name))
	
			if (cus.isFileDownloaded(download_Path,file_name)==false)
	
			{
				println ("No such file present")
			}
			
	else{
		//cus.Open_File(File_download)
		//	cus.readDocxFile(File_Upload)

		//delete the downloaded file
		cus.Delete_File(File_download)
	}
}

############
console output----
File_download is C:/Users/shilpi/Desktop/functional-tests/download_Upload/CoursePaticipantsList_report
download_Path is C:/Users/shilpi/Desktop/functional-tests/download_Upload/
file_name is CoursePaticipantsList_report

CoursePaticipantsList_report does not exist inC:/Users/shilpi/Desktop/functional-tests/download_Upload/

@B_L First of all thanks a lot for putting the code for keyword.

Its working for me only if the file is downloaded , Then the script find the file and delete it.

But if for some reason file is not downloaded then instead of message (fileName + 'does not exist in ’ + downloadPath ), I get an error as below :- Please help

uploadFiles.Filedownloaded.isFileDownloaded(userDownloads, “BuyBay PIM” + “.xlsx”)
2020-08-13 18:40:02.772 ERROR k.k.c.m.CustomKeywordDelegatingMetaClass - :x: No such property: i for class: uploadFiles.Filedownloaded
2020-08-13 18:40:02.773 ERROR c.k.katalon.core.main.TestCaseExecutor - :x: Test Cases/Test variable set up/Product Excel FAILED.
Reason:
groovy.lang.MissingPropertyException: No such property: i for class: uploadFiles.Filedownloaded
at uploadFiles.Filedownloaded.isFileDownloaded(Filedownloaded.groovy:56)
at uploadFiles.Filedownloaded.invokeMethod(Filedownloaded.groovy)

@B_L Is there any solution where I can verify the downloaded file whose name gets generated dynamically, eg: Filename_123.csv where 123 is dynamically generated number. I tried replacing equals with contains in your keyword, but it doesn’t work, I get Cannot get property ‘length’ on null object. Although the file has got downloaded.