24

I think that everybody who uses Webdriver for test automation must be aware of its great advantages for web development.

But there is a huge issue if file uploading is part of your web flow. It stops being test automation. The security restriction of browsers (invoking file selection) practically makes it impossible to automate tests.

Afaik the only option is to have Webdriver click the file upload button, sleep the thread, have developer/tester manually select the file, and then do the rest of the web flow.

How to deal with this, is there a workaround for it? Because it really can't be done like this. It wouldn't make sense.

This is the only case I know of when browser security restrictions do not apply:

<script language=javascript>   
  function window.onload(){   
          document.all.attachment.focus();   
          var WshShell=new ActiveXObject("WScript.Shell")   
          WshShell.sendKeys("D:\MyFile.doc")
  }   
</script>
2

6 Answers 6

44

Webdriver can handle this quite easily in IE and Firefox. Its a simple case of finding the element and typing into it.

driver = webdriver.Firefox()
element = driver.find_element_by_id("fileUpload")
element.send_keys("myfile.txt")

The above example is in Python but you get the idea

4
  • Hey, could you please elaborate on this one ? I don't think it is possible. First off, what is myfile.txt ? Is it supposed to be path on a file on filesystem ? The only way this can be done is the Microsoft ActiveX bullshit (see my edited answer) afaik. A tried what you say and I got ERROR page with Firefox driver.
    – lisak
    May 25, 2011 at 20:03
  • you are right, it should work... Guys on irc channel was telling me that there is no way of doing this...great :)
    – lisak
    May 25, 2011 at 20:25
  • 2
    I would like to point out that while the code snippet above works, it is primarly for local WebDriver instances. Should you be using RemoteWebDriver, Grid2, or SauceLabs, you may need to add more code to get it to work. And it becomes a 2 step process over Remote/Grid - file is copied over (via JSONWireProtocol in Selenium) from where Selenium code is run (as source location) to the browser session machine (remote/grid node) and then from there is finally uploaded to the website/app from the node machine. More details in my next comment, as I run out of space here.
    – David
    Jan 22, 2013 at 21:46
  • FYI, in local mode, file is directly uploaded from localhost/node to website/app, no intermediate file transfer step. See these links for info on remote/grid uploads: sauceio.com/index.php/2012/03/…, groups.google.com/forum/#!topic/webdriver/OU2RxvTE7UY/…
    – David
    Jan 22, 2013 at 21:47
3

Using AWT Robots is one option, if you're using Java, which you are. But it's not a good option, it is not very dependable, and not clean at all. Look here

I use HttpClient and run a few tests outside of Selenium. That's more dependable and cleaner.

See the code below. You'll need more exception handling and conditionals to get it to suit your job.

HttpClient c = new HttpClient();
String url = "http://" + cargoHost + ":" + cargoPort + contextPath + "/j_security_check";
PostMethod post = new PostMethod(url);
post.setParameter("j_username", username);
post.setParameter("j_password", password);
c.executeMethod(post);

url = "http://" + cargoHost + ":" + cargoPort + contextPath + "/myurl.html";
MultipartPostMethod mPost = new MultipartPostMethod(url);
String fileNameWithPath = this.getClass().getClassLoader().getResource(filename).getPath();
File f1 = new File(fileNameWithPath);
mPost.addParameter(elementName, f1);
mPost.addParameter("action", "upload");
mPost.addParameter("ajax", "true");

c.executeMethod(mPost);
mPost.getResponseBodyAsString();
3
  • I've already tried AWT, but it's really worse than better :-) And the second option. The file uploader I'm using lists the files that you selected and keep state in javascript... It's part of the testing.... It's really damn situation man
    – lisak
    May 25, 2011 at 17:30
  • HtmlUnit has some support for Javascript. I haven't tried it though. And WebDriver supports HtmlUnitDriver. May be you can try it out.
    – rahul
    May 25, 2011 at 17:39
  • the variable elementName, is like <input name="elementName"> ?
    – jechaviz
    Jun 19, 2016 at 16:59
2

The suggestion of typing into the text box works only if the textbox is enabled. Quite a few applications force you to go through the file system file browser for obvious reasons. What do you do then? I don't think the WebDriver mavens thought of just presenting keys into the KeyBoard buffer (this used to be a "no brainer" in earlier automation days)

===

After several days of little sleep, head banging and hair pulling I was able to get some of the Robot-based solution suggested here (and elsewhere).

The problem i encountered was that the dialog text box that was populated with the correct file path and name could not respond to the KeyPress/Release Events of terminating the file name with VK_ENTER as in:

private final static int Enter = KeyEvent.VK_ENTER;
keyboard.keyPress(Enter);
keyboard.keyRelease(Enter);

What happens is that the file path and file name are typed in correctly but the dialog remains opened - against my constant hoping and praying that the key emulation will terminate it and get processed by the app under testing.

Does anyone know how to get this robot to behave a bit better?

1

Just thought I'd provide an FYI to author's original post of using ActiveX. Another workaround would be to integrate with desktop GUI automation tools to do the job. For example, google "Selenium AutoIt". For a more cross-platform solution, consider tools like Sikuli over AutoIt.

This of course, is not considering WebDriver's support for uploads on IE & Firefox via SendKeys, or considering for other browsers where that method doesn't work.

0

After banging my head on this problem for far too many hours, I wanted to share with the community that Firefox 7.0.1 seems to have an issue with the FirefoxDriver sendKeys() implementation noted above (at least I couldn't get it to work on my Windows 7 x64 box), I haven't found a workaround, but updating to Firefox 8.0.1 seems to have fixed the problem. For those of you wondering, it's also possible to use Selenium RC to solve this problem (though you need to account for all of your target operating systems and the native key presses required to interact with their file selection dialogs). Hopefully the issues I had to work around save other people some time, in summary:

https://gist.github.com/1511360

0

If you have your are using a grid, you could make the folder of the testfiles open for sharing.

This way you could select the upload input field and set its value to \\pc-name\myTestFiles

If you're not, you should go with local files on each system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.