Browser File open dialog issues with Robot class

725 views
Skip to first unread message

testingzeal

unread,
Jul 29, 2014, 12:07:24 PM7/29/14
to webd...@googlegroups.com
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);
}


openDialog.png

Mark Collin

unread,
Jul 29, 2014, 1:45:16 PM7/29/14
to webd...@googlegroups.com
Don’t use a robot class,

Instead just use sendKeys:

//Do this only is using a RemoteWebDriver
driver.setFileDetector(new LocalFileDetector());

//This does the actual file upload
WebElement myInputElementTypeFile = driver.findElement(By.id("fileUpload"));
myInputElementTypeFile.sendKeys("myfile.txt”);

This will work if you are using an <input type=“file”> to upload your files, if you are using some funky JavaScript way of doing it get your devs to write an automation hook you can utilise.


--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.
<openDialog.png>

signature.asc

testingzeal

unread,
Jul 29, 2014, 2:25:44 PM7/29/14
to webd...@googlegroups.com
Thanks Mark. 

Unfortunately in our app "Open dialog" comes up after a button click. So will not be able to use the logic you mentioned.

Is there a reason why you don't want me to use robot class when most of the people are suggesting to use it.

I have been working on this past 3 days without much success.

Any help is really appreciated.

Krishnan Mahadevan

unread,
Jul 29, 2014, 10:46:33 PM7/29/14
to webd...@googlegroups.com
AFAIK your tests will conk under the following circumstances when you use Robot!
1. Remote executions via Grid2 is kind of out of the question. 
2. If you are doing parallel executions then the tests will step on each other's shoes!

~ Krishnan

iSent. iPhone. iReget.iTypos!

pradeep kattekola

unread,
Jul 31, 2014, 9:50:44 AM7/31/14
to webd...@googlegroups.com
suggestion is go for Auto IT tool or Sikuli to handle the upload window .


Thanks,
Pradeep Kattekola

mir saadat Ali Hashmi

unread,
Aug 7, 2014, 7:52:08 AM8/7/14
to webd...@googlegroups.com
Hello,

I have this working code for me. Try it and see what is happening. Only difference from your code to mine is after pasting filename i'm giving delay of 1 second then entering Enter key.


//opens file browse window
        driver.findElement(By.id("photo")).click();
              
//copying the file name to clipboard
        StringSelection ss = new StringSelection("C:\\Users\\pictures\\image1.jpg");
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss,null);

        Robot robot=new Robot();
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        Thread.sleep(1000);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
Reply all
Reply to author
Forward
0 new messages