File Upload with Selenium

With Selenium 2 uploading a path is very easy if the page is using standard file upload mechanism of web. Some old pages may still be using Flash or another plugin based technology. In other words, you should have some <input> field defined as follows.

<input type=”file” id=”upm-upload-file” name=”plugin” size=”38″>

You can automate an upload using just 2 lines of code.

[code language=”java”]private void deployAddOnImplementation(String pathToJarFile) {
// driver.findElement(By.id("upm-upload-file")).clear(); //if we send clear, we get "invalid element state: Element must be user-editable in order to clear it." exception.
driver.findElement(By.id("upm-upload-file")).sendKeys(pathToJarFile);
driver.findElement(By.cssSelector("button.button-panel-button.upm-upload-plugin-submit")).click();
}[/code]

Only problem was, I had tried to send clear command to file input field which caused, “invalid element state: Element must be user-editable in order to clear it”, exception. It was not needed for my case because I am sure input field will be always empty. I have removed the parts not directly related with file upload. Just press Command+Shift+A, write name of test method like “deployWorklogPRO” and Enter. Latest version of the plugin will be deployed which in turn save some time for me.

Leave a Reply

Your email address will not be published. Required fields are marked *