Reputation: 17
I have two test cases of checking file upload which is using same piece of code as written below. For 1 test case it's pasting correct path of file upload but for other test case its only pasting character 'v' and also have anybody have idea why robot class does not work in case of if we run test cases on remote machine on jenkins.
// Lines of code for file upload test case
Robot rob = new Robot();
StringSelection ss = null;
ss = new StringSelection("C:\\repository\\A\\B\\C\\resources\\no_fog.png");
rob.setAutoDelay(2000);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
rob.setAutoDelay(1000);
rob.keyPress(KeyEvent.VK_CONTROL);
rob.keyPress(KeyEvent.VK_V);
rob.keyRelease(KeyEvent.VK_CONTROL);
rob.keyRelease(KeyEvent.VK_V);
rob.setAutoDelay(1000);
rob.keyPress(KeyEvent.VK_ENTER);
rob.keyRelease(KeyEvent.VK_ENTER);
rob.setAutoDelay(1000);
Can somebody provide any help on this
Upvotes: 0
Views: 299
Reputation: 364
I have used Robot class for file upload and it's working fine for me try this:
Robot robot = new Robot();
StringSelection sel = new StringSelection("Path of image");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(sel, null);
robot.delay(300);
// Press Enter
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
// Press CTRL+V
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
// Release CTRL+V
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
robot.delay(300);
// Press Enter
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Upvotes: 2