Wait for file download tip

Hi folks,
I thought that I would share this little tip that I use to wait for files that are being downloaded.
(Made some edits after feed-back from Russ :wink: )
Thanks,
Dave

//Uses Java file.exists() method
//First create the GlobalVariable in any Katalon profile
GlobalVariable.Directory = (System.getProperty('user.home') + '\\Downloads\\')
println('Directory: ' + GlobalVariable.Directory)
def src = new File(GlobalVariable.Directory + 'filename')

//----------------------------------------------------
int TIMEOUT_SECONDS = 20
int count = 0
while (count < TIMEOUT_SECONDS) {    
    count++
	WebUI.delay(1)
	System.out.println('TIMEOUT_SECONDS: ' + count)
    //Waits for src to download; if not found breaks
    if (src.exists()) {
        println('FileExists: ' + src.exists())
        break
    }
}
println "Do something with the src file... "
4 Likes

Russ_Thomas also suggested this code snippet would do the trick too :wink:
Create a GlobalVariable for TIMEOUT_SECONDS with a value of seconds to wait.

//Uses Java file.exists() method
boolean downloaded = false
int count = 0
	while (count < TIMEOUT_SECONDS) {
	WebUI.delay(1)
	count++
	if (src.exists()) { 
	downloaded = true
	break; }
}
if(!downloaded) {
throw new StepFailedException("File was not downloaded. Reached timeout.")