All,
I am trying to upload a file using chrome browser and used robots class for that.Several of them mentioned uploads were successful with Robots class but i had no luck.
Please help is appreciated.
Manual Test scenario test steps are -
1.Click on "upload" button in chrome browser
2. It opens "Open Dialog"
3.Select a picture that is located in "C:\photos" or "src/main/resources/"( I prefer to use file that is located in "src/main/resources")
4.Click on Open button of Open Dialog
FYI - Attached is the open Dialog screen that comes up post upload button click
Using RemoteWebDriver ,
version - 2.39
Chrome - 35/chrome 36
Windows 7
+++++++++++++++++++++++++++++++++++++++++
Here is the code i tried and Open Dialog comes up but is not setting the pic path in the "File name" field of "Open Dialog"
public static void upload() throws Exception{
driver.findElement(By.cssSelector("a#upload-button")).click();
Thread.sleep(500);
System.out.println("1");
setClipboardData("C:\\photos\\two.jpg");
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(500);
}
public static void setClipboardData(String string) {
StringSelection stringSelection = new StringSelection(string);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}