How to use the 'Robot Framework' to load files using a custom keyword

Please Note: Best practices is to NOT to use WebUI.delay() OR Thread.sleep() but sometimes there is no alternative.

  1. First create a new ‘uploadFiles’ custom keyword as follows:
    -Start ‘Katalon Studio’
    -Open any test project
    -Click ‘File’ > ‘New’ > ‘Keyword’
    -Input ‘Package’= tools
    -Input 'Class Name" = uploadFiles
    -Click ‘OK’
    -Result: A new ‘uploadFiles.groovy’ custom keyword is created
Copy and paste the following code to the new custom keyword:
//Use "CTRL+SHIFT+O" to add any required imports to the test case
package tools
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

//Use when "Upload File" or "Send Keys" will not work
public class uploadFiles {
	@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.delay(1000); //Millisecond 1 second delay only if needed
		robot.keyPress(KeyEvent.VK_CONTROL);
		robot.keyPress(KeyEvent.VK_V);
		robot.keyRelease(KeyEvent.VK_V);
		robot.delay(1000); //Millisecond 1 second delay only if needed
		robot.keyRelease(KeyEvent.VK_CONTROL);
		robot.keyPress(KeyEvent.VK_ENTER);
		robot.keyRelease(KeyEvent.VK_ENTER);
	}
}
2) Next add the following to the body of the test case
//Use "CTRL+SHIFT+O" to add required imports to the test case
WebUI.openBrowser('')
CustomKeywords.'tools.uploadFiles.uploadFile'(findTestObject('Your_TestObject'), 'C:\\TestFiles\\file001.jpg')
Thread.sleep(2500) //Millisecond 2.5 second delay only if needed
CustomKeywords.'tools.uploadFiles.uploadFile'(findTestObject('Your_TestObject'), 'C:\\TestFiles\\file002.jpg')
Thread.sleep(2500) //Millisecond 2.5 second delay only if needed
//More files can be added here...
6 Likes

My case is working now, Thank You :clap: :clap: :clap: :clap:

mmm…wondering…could that be because I have no admin rights on my laptop…?

Well, good luck with that. Recording is a “get you started” option, but not a solution.

4 posts were merged into an existing topic: File Upload with button in Katalon Studio

@Dave_Evers thank you so much, it works partially. It comes up but cant find the file then it does not click it to add it…

It writes the path in the FILE name field however it it does not double click or select it when it finds it…

I need it to click OPEN in file explorer or double click it so it automatically gets added, Once it does that after finding the file , it will work.

Hi @naomy.arnold, Have a look at this posting, it might be helpful: How-to use ‘sendKeys’ or 'File Upload' to upload files - #12 by Dave_Evers

Does your pathway have double slashes in it? It needs them. (A single slash is an escape character, like for the “new line symbol”, \n, so the second slash says that the first slash is just a slash.)

gImportFileName = 'G:\\Katalon Test Cases\\Katalon\\Data Files\\Details.txt';

yes actually I did then add the slashes
@grylion54

code below

**CustomKeywords**.'tools.uploadFiles.uploadFile'(findTestObject('Object/button_plus_add_document'), **'"G:\\Katalon\\TestCase\\Downloads\\test.docx"')**

KEYWORD
image

It may be small, but you have your second delay in the middle of releasing the CTRL + V (i.e. paste). So, keyPress on CTRL + V and then keyRelease the same two keys, like below:

robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
// and move your delay here instead
robot.delay(1000)

Also, I am going to assume the single quotes and double quotes on your pathway in the above post is a copy and paste error, as you only need one set of either, not both.

Edit: it also looks like you may have gotten off with your key matches. You have a keyPress of <Control> but no keyRelease of it, as well as an extra keyPress of <Enter>. The commands are just like using your keyboard: keyPress with a matching keyRelease.