Please Note: Best practices is to NOT to use WebUI.delay() OR Thread.sleep() but sometimes there is no alternative.
- 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...