File not getting uploaded using java.awt.Robot class in Java

Viewed 29

I tried implementing Robot class in my Selenium Test cases written using Java. I am getting a strange issue there. So when I am trying to run those test cases on my local machine (Windows 10 Enterprise Edition) and monitoring it's working fine and the file is getting uploaded. But when I am trying to run those in a remote server (Windows Server 2012) and monitoring those it's again working fine but when I am leaving those test cases for the entire night to run I found that the File Explorer dialogue box is opening but it's never getting close. It might be that the file path is not getting pasted and the Enter (Ok) button is not getting clicked.

    public void uploadFile(String path) {
        String abspath = _getAbsolutePath(path);
        StringSelection stringSelection = new StringSelection(abspath);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
        Robot robot = null;
        try {
            // native key strokes for CTRL, V and ENTER keys
            robot = new Robot();
            robot.setAutoDelay(5000);
            robot.keyPress(KeyEvent.VK_CONTROL); // Press Ctrl
            robot.keyPress(KeyEvent.VK_V); // Pres V

            robot.keyRelease(KeyEvent.VK_V); // Release Ctrl
            robot.keyRelease(KeyEvent.VK_CONTROL); // Release V

            robot.setAutoDelay(5000);
            robot.keyPress(KeyEvent.VK_ENTER); // Press Enter
            robot.keyRelease(KeyEvent.VK_ENTER); // Release Enter
        } catch (Exception exp) {
            exp.printStackTrace();
        }
    }
0 Answers
Related