Download & upload random picture

Hey guys,

thanks for the detailed help in other threads. You have already helped me quite a lot. One thing I cannot get to work is selecting the newest file in a specified folder and then uploading it.

Can you tell me how this can be done or point me to the ressource to do it?

I have only found this ressource, which seems to go in the right direction:

Thanks :slight_smile:

so next read is here:
this will order your files so you will be able to grab latest one

Thanks for the quick answer. I have tried to work with each of the scripts but could only get this one to work:

new File(‘C:/Users/…/Downloads’).listFiles().sort() {

_ a,b -> a.lastModified().compareTo b.lastModified()_


.each {

_ println it.lastModified() + " " + it.name_

_} _

Here I get all files instead of one. Where to I need to add the .last() or .max command to make it work? And how would I put the result into a variable?

PS: I am a noob in regards to this…

code can look like this:

def today = new Date()def recent = {file -> today - new Date(file.lastModified()) < 7}def files = new File('c:\\users\\s2ppun\\').listFiles().toList().findAll(recent).sort{a,b -> b.lastModified() <=> a.lastModified()}println "Newest file in dir: ${files[0].name} ${new Date(files[0].lastModified())}"

Thanks a lot! That worked.

After some googling and trials, I still could not find a way to use the file name within an upload. What I tried is this:

WebUI.uploadFile(findTestObject(‘Page_Generate Deep Dream Generator/img_Upload’), setValue(’${directory}${files[0].name}’))

Guess I need to do a java course…

Also, maybe store the location of saving it as a temp variable.
eg, file = newfile….

then you can recall that variable anytime during the script
not sure if this works but itll save you having to reorder it

for WenUI.uploadFile arguments are TO and string … files[0].name returns string so command should look like:

WebUI.uploadFile(findTestObject('Page_Generate  Deep Dream Generator/img_Upload'), files[0].getAbsolutePath())

getAbsolutePath() will return full path to your file

Thanks a lot Andrej!


One more question came up: How can I select a random file from a directory?

ok following will be from head, probably some errors will be there. principle: generate random Integer from 0 to noOfFiles and select based on that. can look like:

WebUI.uploadFile(findTestObject('Page_Generate  Deep Dream Generator/img_Upload'), files[new Random().nextInt(files.size()-1)].getAbsolutePath())

You are a magican… :slight_smile:

Hi team,
I have export button in my web application. through recorder am able to click on export button and download the file but when i run it through groovy script it says the below error:

Downloaded FAILED because (of) (Stack trace: com.kms.katalon.core.exception.StepFailedException: Unable to click on object (Root cause: java.lang.IllegalArgumentException: Object is null)

Not able to perform any action related to download

please create new tread and add all information as sugested here: