How to verify downloaded file?

I run a script where a download button is pressed.
Is it possible to test if the file is downloaded in machine?

I use this method:

Boolean isFileDownloaded(String downloadPath, String fileName) {

File dir = new File(downloadPath);

File[] dirContents = dir.listFiles();

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

return true;

}

}

return false;

}

2 Likes

can you send with an example so it is easy for me

Just call this method.
isFileDownloaded(“C:\\Your\\Download\\Path”, “yourfilename.pdf”)

I see a example, it could help you.

1 Like

isFileDownloaded is showing no signature. What do I need to import to get this working? Thanks.

Anyone?

Edita Markauskaitė said:

I use this method:

Boolean isFileDownloaded(String downloadPath, String fileName) {

File dir = new File(downloadPath);

File dirContents = dir.listFiles();

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

return true;

}

}

return false;

}

@Edita Markauskaitė I have this in my script, but it is not run. I have

//Define Custom Path where file needs to be downloaded
String DownloadPath = 'C:\\ChromeDownloads\'

Before it, but the script stops there. Never runs the boolean at all?

I took would like an answer to this, it’s not working for me either. I have also tried to create custom keyword but keeps telling me length is not defined…

What about this?

File downloadFolder = new File("C:\\test\\")List namesOfFiles = Arrays.asList(downloadFolder.list())if(namesOfFiles.contains("test.txt")) {	println "Success"}else {	println "Failure"}

1 Like

Still no good, however I did make progress. I created a keyword which is working I also setup the download to be the default user’s download. the problem I’m having now is it’s passing even though it’s not… I have it looking for the file but if it can’t find it, it still says it passed:

Keyword:

package customKeywords

  

import com.kms.katalon.core.annotation.Keyword

  

@Keyword

public boolean isFileDownloaded(String downloadPath, String fileName) {

  

File dir = new File(downloadPath);

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

if (dirContents.length > 0) {  

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

return true;

}

}

return false;

}

return false;

}   

set up in testcase:


'path for pdf download verification'

String home = System.getProperty("user.home");

String userDownloads = new File(home+"/Downloads/")  

CustomKeywords.'customKeywords.verifyFileDownloaded.isFileDownloaded'(userDownloads, 'testfile.pdf')  

this is saying pass when it’s not… it’s not deleting it, I debugged and walked through but still no good

I figured it out… so here is what I am doing and how it works:

I am creating the following keyword to verify and delete the downloaded file. IF this fails it will continue, it will also tell me if the file was found or not and name in the log viewer: (FYI ignore ### not part of code but I don’t know how to block the code here)

#######################################################

package customKeywords

import com.kms.katalon.core.annotation.Keyword

import com.kms.katalon.core.util.KeywordUtil

import org.testng.Assert

@Keyword

public boolean isFileDownloaded(String downloadPath, String fileName) {

File dir = new File(downloadPath);

File[] dirContents = dir.listFiles();

String lastAttempt = ‘’;

if (dirContents.length > 0) {

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

return true;

}

lastAttempt = dirContents[i].getName().equals(fileName);

}

if (lastAttempt != fileName) {

KeywordUtil.markFailed(fileName + ’ does not exist in ’ + downloadPath)

return false;

}

}

return false;

}

#######################################################

Then I call that keyword in my test case like this:
#######################################################

‘Click on the Generate button for Packet’

WebUI.click(generateButton)

WebUI.delay(5)

CustomKeywords.‘customKeywords.verifyFileDownloaded.isFileDownloaded’(userDownloads, ‘Packet.pdf’)

WebUI.delay(1)

#######################################################

If you are like me and want this to run on ANY machine and use the user you are signed in with. put this before you call the keyword:
#######################################################

‘path for pdf download verification’

String home = System.getProperty(‘user.home’)

String userDownloads = new File(home + ‘/Downloads/’)

#######################################################

What this does is set the userDownloads path to equal the user you are signed in with. I hope you all find this useful. I’ll be referencing again if ever the need arises.

10 Likes

Hi

I tried the same method but showing an error “Cannot get property ‘length’ on null object”

Can you please temme where exactly its going wrong?

I’d have to see how you wrote up the keyword and the error message from Katalon. However if it’s telling you null object how are you calling the keyword? Are you passing in the two values

public boolean isFileDownloaded(String downloadPath, String fileName) 

This may vary based upon where you have it stored in your keywords but it looks like this in a test case:

this at the top:

String userDownloads = new File(home + '/Downloads/')

This where you need the validation

CustomKeywords.'customKeywords.verifyFileDownloaded.isFileDownloaded'(userDownloads, 'filename' + '.pdf')

this would look for “filename.pdf” in your downloads folder for example.

Hi

I was not set the chrome setup in settings modal and i was getting that error. It is working fine now.

Thank you:)

Nice , the keyword is useful for me. Thanks !

1 Like

This worked for me! :slight_smile:

didn’t work for me

can u please provide any other way or give me with example

what does your code look like so far? What error message are you getting?