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

前端 未结 6 1333
谎友^
谎友^ 2020-12-03 03:09

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 i

相关标签:
6条回答
  • 2020-12-03 03:36

    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?

    0 讨论(0)
  • 2020-12-03 03:39

    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 讨论(0)
  • 2020-12-03 03:43

    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

    0 讨论(0)
  • 2020-12-03 03:47

    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 讨论(0)
  • 2020-12-03 03:47

    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.

    0 讨论(0)
  • 2020-12-03 03:49

    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();
    
    0 讨论(0)
提交回复
热议问题