Uploading a file using robot class, but unable to upload it due to the exception.

@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_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
}

Calling this method in Tc, CustomKeywords.'custom.SampleKeyword.uploadFile'( findTestObject('Object'),'Path')

1 Answers

0
Alan H On

I think you're just missing an import for Toolkit. Either add the following

import java.awt.Toolkit

or replace

Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);

with

java.awt.Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);