File Operations - Open any File and Delete file Using Custom Keyword in Katalon

File Operation for .exe/pdf/txt/doc…etc

import java.awt.Desktop

import java.io.File

import java.io.IOException

import java.nio.file.*

public class FileOperations {

// Custom keyword for opening file - provide file path as a parameter

@Keyword

def Open_File(String FilePath) {

File file = new File(FilePath)

//first check if Desktop is supported by Platform or not

if(!Desktop.isDesktopSupported()){

System.out.println(“Desktop is not supported”)

return

}

Desktop desktop = Desktop.getDesktop()

if(file.exists()) desktop.open(file)

println “File Opened Sucessfully \r”

}

// Custom keyword for deleting file - provide file path as a parameter

@Keyword

def Delete_File(String FilePath) {

File file = new File(FilePath)

try

{

Files.deleteIfExists(Paths.get(FilePath))

}

catch(NoSuchFileException e)

{

System.out.println(“No such File/Directory Exists”)

}

catch(DirectoryNotEmptyException e)

{

System.out.println(“Directory is not Empty.”)

}

catch(IOException e)

{

System.out.println(“Invalid Permissions.”)

}

System.out.println(“File Deletion is Successful.”)

}

}

2 Likes

Hi,
This is a very helpful post.
Thank you for sharing :slight_smile:

Thanks for this Veera,

may I know how do you close a file when opened via import java.awt.Desktop