vitlev
February 12, 2019, 10:28am
1
Hi guys!
I have to following test case which I need some help with:
Click on export button
Wait for file is generated and downloaded
Verify if the file is downloaded
The problem that I’m facing is that the file’s name is generated based on the current date/time.
The way I try to verify it is the following:
mydate = new Date
formattedDate = mydate.format('yyyyMMdd')
dateHour = mydate.format('HHmmss')
FileName = "export_'$formattedDate'_'$dateHour'.qif"
FileName = FileName.replaceAll('\'', '')
CustomKeywords.'customkeywords.isFileDownloaded.isFileDownloaded'(userDownloads, FileName)
The Problem is that the downloaded file’s name and the variable’s timestamp last two digits (the seconds)
are sometimes not equal.
Any idea how to handle this situation?
Thanks in advance!
Sounds like a bug somewhere. If you hardwire a name, I take it you have no issues?
Or is it a zero-padding issue?
vitlev
February 12, 2019, 1:48pm
4
The problem is that the generation of the file and the generation of the variable is not in synch.
Like, the file’s name is: export_20190212_144205
and the generated variable is: export_20190212_144200
A possible solution that I am trying, is to cut the last 2 digits of the generated variable like: export_20190212_1442 , and use regex or some kind of pattern to replace it.
But don’t know if it is even possible in katalon? And if it is, then how (what is the correct syntax)?
did i understand correctly that you cannot set name of downloaded file?
if yes, then basically you are guessing name you search?
will be solution for you :
clear download dir before test
download
check existence of file with non 0 size in folder.
?
1 Like
Which is what I first thought but then I noticed you’re producing the filename incorporating the time you produced… which seems to suggest a bug or something very weird going on.
But then you’ll end up with a race. If the seconds are close to “59” you’re going to get the minutes tick over. If the minutes are close to “59” you’ll get the hours tick over.
That’s a race best avoided.
I think I would take the most recent file from the folder and compare the fixed name portion - if it matches DELETE it so it does not conflict with other tests. (But even this is non-satisfactory).
I would really want to establish if you’ve found a bug because the way you have described what you are doing it looks like there’s something strange going on.
1 Like
take look here
http://grails.asia/groovy-file-examples
there is example on how to find file based on wildchars…
1 Like
Try this:
formattedDate = mydate.format('yyyyMMdd')
dateHour = mydate.format('HHmmss')
FileName = "export_" + formattedDate + "_" + dateHour + ".gif"
WebUI.comment("Name is: " + FileName)
String dirName = "Your\folder\name"
List files = new File(dirName).list()
WebUI.comment("Files: " + files.toString())
CustomKeywords.'customkeywords.isFileDownloaded.isFileDownloaded'(userDownloads, FileName)
files = new File(dirName).list()
WebUI.comment("Files: " + files.toString())
Does the file show up in the list?
Does it have the correct name? If so, your bug is somewhere in your custom keyword, I guess.
vitlev
February 12, 2019, 3:44pm
9
So, I executed the following:
WebUI.click(findTestObject('Export/button_QIF'))
mydate = new Date()
formattedDate = mydate.format('yyyyMMdd')
dateHour = mydate.format('HHmmss')
FileName = "transaction_export_" + formattedDate + "_" + dateHour + ".qif"
WebUI.comment("Name is: " + FileName)
WebUI.delay(5)
String dirName = "C:/Users/Downloads"
List files = new File(dirName).list()
WebUI.comment("Files: " + files.toString())
CustomKeywords.'customkeywords.isFileDownloaded.isFileDownloaded'(userDownloads, FileName)
files = new File(dirName).list()
WebUI.comment("Files: " + files.toString())
WebUI.verifyElementVisible(findTestObject('Export/button_CSV'))
WebUI.click(findTestObject(Export/button_CSV'))
formattedDate2 = mydate.format('yyyyMMdd')
dateHour2 = mydate.format('HHmmss')
FileName2 = "transaction_export_" + formattedDate2 + "_" + dateHour2 + ".csv"
WebUI.comment("Name is: " + FileName2)
WebUI.delay(10)
List files2 = new File(dirName).list()
WebUI.comment("Files: " + files2.toString())
CustomKeywords.'customkeywords.isFileDownloaded.isFileDownloaded'(userDownloads, FileName2)
files2 = new File(dirName).list()
WebUI.comment("Files: " + files2.toString())
And I got the following result:
click(findTestObject("Export/button_QIF")
mydate = new java.util.Date()
formattedDate = mydate.format("yyyyMMdd")
dateHour = mydate.format("HHmmss")
FileName = "export_" + formattedDate + "_" + dateHour + ".qif"
comment("Name is: " + FileName)
Name is: export_20190212_162727.qif
delay(5)
dirName = "C:/Users/Downloads"
files = File(dirName).list()
comment("Files: " + files.toString())
Files: [desktop.ini, transaction_export_20190212_162727.qif]
customkeywords.isFileDownloaded.isFileDownloaded(userDownloads, FileName)
export_20190212_162727.qif exist in C:\Users\Downloads and was deleted
files = File(dirName).list()
comment("Files: " + files.toString())
Files: [desktop.ini]
verifyElementVisible(findTestObject("Export/button_CSV"))
click(findTestObject("Export/button_CSV"))
formattedDate2 = mydate.format("yyyyMMdd")
dateHour2 = mydate.format("HHmmss")
FileName2 = "transaction_export_" + formattedDate2 + "_" + dateHour2 + ".csv"
comment("Name is: " + FileName2)
Name is: export_20190212_162727.csv
delay(10)
files2 = File(dirName).list()
comment("Files: " + files2.toString())
Files: [desktop.ini, transaction_export_20190212_162734.csv]
customkeywords.isFileDownloaded.isFileDownloaded(userDownloads, FileName2)
com.kms.katalon.core.exception.StepFailedException: export_20190212_162727.csv does not exist in C:\Users\Downloads
files2 = File(dirName).list()
comment("Files: " + files2.toString())
2Files: [desktop.ini, export_20190212_162734.csv]
So for the first time it was executed successfully but for the second time it failed.
1 Like
vitlev
February 12, 2019, 4:00pm
10
The custom keyword that I use is this:
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;
}
2 Likes
So YOU deleted the file, on disk, manually - right? And, if I’m following this correctly, that was the correct name - right?
But then…
Name is: export_20190212_162727.csv
I don’t think I’m following your method of posting the timeline. Perhaps a dump of your messages might help?
But if the file was deleted then this:
com.kms.katalon.core.exception.StepFailedException: export_20190212_162727.csv does not exist in C:\Users\Downloads
makes sense (I think).
Try this:
Send a copy of your code verbatim .
Follow it with a dump of the log messages (you can delete all the stuff that doesn’t matter, but leave the interesting stuff intact).
lastAttempt is now the result of calling a boolean method.
lastAttempt
and fileName
(types) are now in conflict (I think).
vitlev
February 13, 2019, 10:21am
13
Thanks for the help guys!
I managed to solve the problem ( I messed up the second current time request), now it is working fine.
thanks for posting an answer, this worked for me!
How did you manage to get the 2 times to be consistent / same? I can get close but the seconds can be inconsistent if the execution time’s milliseconds borders on .900 / .001
The 1st time being when the file is generated and the 2nd time being when you format/get the current time. I’m struggling to get them to be the same. Plus, I’m struggling to convert the time stamp to decrease by an hour.
Hi Vitlev, can i now how do you solve it? I still have issue with the last 2 digit (second)
vitlev
April 23, 2020, 6:37am
17
Hi guys!
I made some changesto use regex, maybe this can help to solve your problems too
Code snippet:
mydate = new Date()
formattedDate = mydate.format('yyyyMMdd')
dateHour = mydate.format('HHmm')
fileName = ~/transaction_export_$formattedDate\_$dateHour\d{2}.qif/
WebUI.delay(5)
CustomKeywords.'customkeywords.isFileDownloadedRegex.isFileDownloadedRegex'(fileName)
Custom keyword:
package customkeywords
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.util.KeywordUtil
import org.testng.Assert
@Keyword
public boolean isFileDownloadedRegex(fileName) {
String userHome = System.getenv( 'userprofile' );
String downloadPath = userHome + File.separator + "Downloads"
System.out.println("userHome=" + userHome);
System.out.println("downloadPath=" + downloadPath);
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().matches(fileName)) {
// File has been found, it can now be deleted:
dirContents[i].delete();
KeywordUtil.markPassed('File exist in ' + downloadPath + ' and was deleted')
return true;
}
lastAttempt = dirContents[i].getName().matches(fileName);
}
if (lastAttempt != fileName) {
KeywordUtil.markFailed('File does not exist in ' + downloadPath)
return false;
}
}
return false;
}
1 Like
Thank you ! This code works perfectly for me