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;
}
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.
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"}
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.
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 !
This worked for me!
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?