How to handle a native OS (Ubuntu) file upload window in selenium in java. Can Auto-It tool be used for this? If not please let me know the tool name Please anyone help me on this... thanks in advance
1 Answer
To perform the file upload using java, you can use java Robot
class.
With this class you can perform keyboard operation.
Following is code for MAC - Hopefully it works for Ubuntu.
public void upload(String fileLocation) throws Exception {
// Create object of Robot class
Robot robot = new Robot();
StringSelection stringSelection = new StringSelection(fileLocation);
// Copy file path in Clipboard
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
// Hit Command + Shift + G to go to particular path
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(KeyEvent.VK_G);
robot.keyRelease(KeyEvent.VK_META);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.keyRelease(KeyEvent.VK_G);
// Hit Command + V to paste the path from clipboard
robot.keyPress(KeyEvent.VK_META);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_META);
robot.keyRelease(KeyEvent.VK_V);
// Hit Enter
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(2000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}