How to deal with file uploading in test automation using selenium or webdriver

49,237

Solution 1

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

Solution 2

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();

Solution 3

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?

Solution 4

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.

Share:
49,237
lisak
Author by

lisak

Github

Updated on July 09, 2022

Comments

  • lisak
    lisak almost 2 years

    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>