How to Upload a File or Photo Using Katalon Studio

How to Upload a File or Photo Using Katalon Studio

26 February 2025 Stephan Petzl Leave a comment Katalon Issues

Uploading files during web automation testing can sometimes present challenges, especially when dealing with pop-up windows. In this guide, we will walk you through a reliable method to upload files using Katalon Studio, offering a practical solution to automate the file upload process.

Introduction to File Upload in Katalon Studio

When automating file uploads in Katalon Studio, the WebUI.UploadFile() command is often mentioned. However, users sometimes find it difficult to implement effectively, especially when dealing with system dialogs. Here, we provide a step-by-step solution using custom keywords and Java’s Robot class to handle file uploads seamlessly.

Step-by-Step Guide

Create a Custom Keyword

To begin, you need to define a custom keyword that can interact with the system clipboard and simulate keyboard actions. This approach utilizes Java’s Robot class to automate the file selection process.


import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import com.kms.katalon.core.annotation.Keyword;
import com.kms.katalon.core.testobject.TestObject;
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI;

public class WebUICustomKeyword {
    @Keyword
    def uploadFile(TestObject to, String filePath) {
        WebUI.click(to);
        StringSelection ss = new StringSelection(filePath);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
    }
}
  

Implement the Custom Keyword in Your Test Case

Replace the standard ‘Upload File’ step in your test case with the newly created custom keyword. This allows you to specify the file path directly within your test script:


CustomKeywords.'com.katalon.WebUICustomKeyword.uploadFile'(findTestObject('BrowseButton'), 'yourFileHere')
  

By following these steps, you ensure that the file upload process is handled effectively, even when dealing with native file dialogs that are typically challenging to automate.

Enhance Your Automation with Repeato

For those looking to streamline their testing processes further, consider using Repeato, a no-code test automation tool designed for iOS, Android, and web apps. With Repeato, you can create, run, and maintain automated tests using computer vision and AI, making it a practical alternative to traditional tools like Katalon Studio.

Repeato supports data-driven and keyword-driven testing, allowing for complex task automation through command line scripts or JavaScript code. Its ability to save all tests and workspace data in text and JSON formats ensures easy version control and collaboration.

By integrating Repeato into your testing workflow, you can overcome some of the limitations associated with other tools and enjoy a more efficient, user-friendly testing experience.

Like this article? there’s more where that came from!