Selenium File Chooser with hidden input [type = file]

I have an image file upload and want to test it with Selenium (java). The user has two options. He can click the image using drag and drop from his local machine, or click the Browse ... button. Then the selected browser file selection option appears.

In Selenium, I've tried different things. As far as I know, Selenium does not perform operations from the browser. So testing the drag and drop functionality is not a feasible solution.

So, I tried to hit the Browse button and work with the file browser of your choice. I found a YouTube video where someone is successfully selecting a file in this dialog. He took the Robot class to do something on the keyboard.

getDriver().findElement(By.id(BROWSE_BUTTON)).click();

Thread.sleep(5000);

StringSelection stringSelection = new StringSelection("C:\\A.JPG");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);

Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
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);

      

When I debug these lines, the validation is done after the click () method on line 1. I'm using Firefox 31.1.

Any ideas? Maybe there are alternatives? Writing the file path to is input[type='file']

not an alternative because this input is hidden.

+3


source to share


1 answer


Hidden file input was not a problem with Firefox or Chrome. And v2.45.0.6 makes sure IE works with hidden file input. So simple sendkeys()

using input[type='file']

should be good for you. See changelog

EDIT

For FF, this could be a compatibility issue. But the latest version of FF is not yet supported Selenium

. See this



2nd Edit

Selenium 2.46 has been released and IEDriver should now work with hidden file input tag.

+2


source







All Articles