Katalon | File Upload | Using Robot Framework

Priyank Shah
Apr 24 · 3 min read
Assumption here - we have good understanding of following tool / terminologies.
* Cucumber Tool (Basic Idea)
*
Selenium
*
Katalon Tool

Problem Statement:

Uploading a file is a common piece of functionality found on the web. But when trying to automate it you get prompted with a dialog box that is just out of reach for Selenium.

As an Automation Tester,
I want to upload file from my local system
So that I can validate file upload feature.

Solution:

Selenium Webdriver can’t handle these OS pop-ups/applications. To handle dialog box, people often look to a third-party tool to manipulate this window. Few of them are:

Note: Each option mentioned above has their own advantages and disadvantages. Choice should be purely based on our need/use case. We are going to see how Robot Framework works here as a reference.

Solution w.r.t. Katalon Tool

A) Create Custom keyword:

Create Custom keyword

Create new custom keyword so it can be utilized as and when needed through out the project. How to create custom keyword? please refer here.

B) Include Robot Framework Code:

Copy following Robot framework code to custom keyword. This keyword takes 2 parameters.

  • TestObject to — Name of the field on the web page you want to upload
  • String filePath — parameter is the path of the file in the locale.
Include Robot Framework Code
package customPackage// Assumed that common packages are imported for keywords.
import java.awt.datatransfer.StringSelection
import java.awt.Robot
import java.awt.Toolkit
import java.awt.datatransfer.StringSelection
import java.awt.event.KeyEvent
public class customFileUpload {
/**
* File Upload
*/
@Keyword
def static 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);
}
}

C) Access Custom Keyword into Test Step:

Custom keywords in Scripting view

  • The Class CustomKeywords of Katalon Studio allows you to access all custom keywords. Enter the following syntax into the script editor:
CustomKeywords.'customPackage.customFileUpload.uploadFile'(findTestObject(null), '')

Custom keywords in feature file / Step definition (Groovy)

  • In the custom keyword class and the BDD step definition class, custom keywords cannot be called directly as in test case via CustomKeywords class. They can be called as methods of a groovy class.
customFileUpload.uploadFile(findTestObject('btnBrowseMediaSelect'), UpdatedFileUploadPath)

Complete code:

file structure
  • I have placed file to be uploaded under Project Dir itself.
String FileUploadPath =  RunConfiguration.getProjectDir() + '/Include/testData/fileUpload.png''if OS is Windows'  
if(RunConfiguration.getOS().indexOf("Windows")!= -1)
{
FileUploadPath = FileUploadPath.replace('/', '\\')
}

'Upload File'
customFileUpload.uploadFile(findTestObject('btnBrowseMediaSelect'), FileUploadPath)

Disadvantages of Robot Class

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — -

  • Keyword/mouse event will only works on current instance of Window. During automation execution, In case, focus gets changed from current window then robot framework will tries to execute commands on other screen.
  • Most of the methods like mouseMove is screen resolution dependent so there might be a chance that code working on one machine might not work on other.

Priyank Shah

Written by

Agile Product Delivery Manager | Scrum Master (PRINCE2, CSPO™, CSM™, SFC™, ISTQB)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade