Deleting Downloaded PDF

Hi,

I am using the following to download a PDF off a site and verify the file has downloaded with the correct name;

name = WebUI.getText(findTestObject(‘TEST OBJECT’))

println(name + ‘.pdf’)

File downloadFolder = new File(‘C:\\\Downloads’)

List namesOfFiles = Arrays.asList(downloadFolder.list())

if (namesOfFiles.contains(name + ‘.pdf’)) {
println(‘Success’)
} else {
WebUI.verifyTextPresent(‘This Test Case Has Failed’, false)
}

But I can’t figure out how to get Katalon to delete the file before completing the Test Case.

Obviously, I can do it manually but that’s not why any of us are here.

Thanks in Advance!

Is there something wrong with…

new File("path/to/file").delete()
2 Likes

There is no built-in keyword to do this obviously. Katalon uses Groovy (which is basically Java) as a programming language, so you can just use that to delete your files afterward. Here’s a slightly safer version to use than @Russ_Thomas listed below, but the idea is exactly the same:

File file = new File("C:\Downloads\" + name + ".pdf");
if (file.exists()) {
   file.delete();
}

I would put this into a Custom Keyword so that you don’t have to repeat this in every script you need it for.

2 Likes

This works nicely thank you!