Friday, September 24, 2010

HTML5 Drag and Drop Upload Testing

We're building out a testing automation framework at work and one of the most important parts of it involves tests around file uploads. Historically this has been impossible to automate due to security settings in the browser that disallow setting the file path in a file type input element(e.g. to prevent secretly uploading confidential data from client machines). However, some browsers now support configurations to allow disabling this restriction for certain websites. Test automation driver, Selenium, has taken advantage of this and allows a user to set the value of a file type input element. This works great for the simple case of an explicit form and file type input element.

Enter HTML5. Browsers can now respond to the "drop" event and furthermore the XmlHttpRequest specification ( has been enhanced to make it possible to programmatically upload a file with javascript (i.e. xhr.send(fileBlob)). Put those two together and an ambitious web developer can now add automatic drag and drop file uploading to their website.

This is great for the look and feel of a website, but presents a challenge for the tester: How do I simulate a file drag and drop event? We were able to solve this issue by skipping the "drag" part and jumping straight to "drop." What does this mean? It means that our test will automatically call the "drop" event handler and provide a mock javascript event to the handler. The handler takes care of creating the xhr and sending it.

There is still one hurdle to overcome. The xhr requires an actual File object ( Since it's not possible to create one via standalone javascript, we got around this by creating a new file type input on the page and using Selenium to set its value (a path to a valid file on disk) and then referencing that.

The following Scala snippet uses jQuery for the dirty work:

val s = selenium.getEval("fileUpload1 = window.$j('<input/>').attr({type:'file'}).appendTo('body');")


// Trigger the upload event
val mockEventJs = "{originalEvent : {dataTransfer : { files : fileUpload1.get(0).files } } }"

val ss = selenium.getEval("window.uploader.handle_drop(" + mockEventJs + ")")


  1. I tried your example with selenium and php or alternativly in the Selenium IDE.

    After I successfully entered the path to the file in the file-input-field, I have not been able to retrieve the file-object.

    I did not use Jquery to retrieve it, but pure JS.

    Tried this code

    var file = document.getElementById('xyz').files[0];

    but did no fileobject back. But when I selected the file with the browsebutton and then execute the code

    var file = document.getElementById('xyz').files[0];
    with runscript in Selenium, I get the fileobject. Therefore it looks like I have a filepath in the file-field, but the browser did not create a fileobject so far. Does the use of jquery make a difference at this point?


    love and light


    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer learn from HTML5 CSS3 Javascript Online Training from India . or learn thru HTML5 CSS3 Javascript Online Training from India. Nowadays JavaScript has tons of job opportunities on various vertical industry. HTML5 CSS3 Javascript Online Training from India

  2. ok, now finally I got it. My basic problem has been, that I needed to generate a valid file-object, and pass this on to an ExtJs-function. Both is not so easy:

    1. in selenium-IDE / getEval you can not use window.Ext to get access to Ext (as you would expect). In the IDE you have to use selenium.browserbot.getWrappedWindow().Ext. Which in turn does not work when using selenium RC via PHPUnit_Extensions_SeleniumTestCase (which propably has nothing to do with phpunit). Via the selenium RC you have to use window.Ext

    2. You have to use getEval and not runScript, because with getEval the script runs in chrome-context and therefore with more rights.

    3. the following ist php-code, based on PHPUnit_Extensions_SeleniumTestCase, which does the job for me:

    $this->type("id_of_input_file", "/path/to/file");

    $this->getEval("var x = window.document.getElementById('xyz').files[0]; window.Ext.getCmp('awesomeId1SaveProject').processDragAndDropFileUpload(x);");

    If you use the IDE instead you have to use getEval-command together with the value:

    var x = selenium.browserbot.getWrappedWindow().document.getElementById('xyz').files[0]; selenium.browserbot.getWrappedWindow().Ext.getCmp('awesomeId1SaveProject').processDragAndDropFileUpload(x);

    hth someone


  3. Hi, this looks very nice tip, but is there any way for Selenium WD with Java, please help,have a scenario to drag file from file system to webpage, like

    Thank you

  4. I just needed to record a speedy word to express profound gratitude to you for those magnificent tips and clues you are appearing on this site.
    selenium training in bangalore|

  5. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here. Besant technologies Marathahalli

  6. Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts.
    big data training in bangalore
    hadoop training in chennai

  7. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
    web design training in chennai

  8. Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition. selenium training in chennai

  9. Excellent blog!! Such an interesting content to read, your idea made to take Web Designing Certifications. Keep updating more ideas. Thank you.
    website design in chennai
    web design training course

  10. Nice blog. Thank you for sharing. The information you shared is very effective for learners I have got some important suggestions from it.
    Android Training Institute in Chennai | IOS Training in Chennai | Online Android Training in Velachery

  11. Thank you a lot for providing individuals with a very
    spectacular possibility to read critical reviews from this site.

    java training in chennai

    java training in bangalore

  12. Very good informative article. Thanks for sharing such nice article, keep on up dating such good articles.


  13. I like your blog format as you create user engagement in the complete article. It seems round up of all published posts. Thanks for sharing.. Excel Macro Training in Chennai | VMWare Training Institute in Chennai | Tally ERP9 Training Institute in Chennai

  14. Awesome post.. Really you are shared very informative concept.. thank you for sharing. keep on updating..
    Best cloud Computing Training Institute in Chennai | Best cloud Computing Training Institute in Velachery

  15. This paragraph gives clear idea for the new viewers of blogging, Thank you for this post. Good luck.
    Best Graphics DesigningTraining Institute in Chennai | Photoshop Training Institute in Chennai

  16. I like your blog format as you create user engagement in the complete article. It seems round up of all published posts. Thanks for sharing

    white label website builder

    mobile website builder

  17. Very good informative article. Thanks for sharing such nice article, keep on up dating such good articles.

    Best IT Security Services | Austere Technologies

  18. Appreciation for really being thoughtful and also for deciding on certain marvelous guides most people really want to be aware of.

    AWS Training in Chennai

  19. This comment has been removed by the author.

  20. Good informative article. Thanks for sharing such nice information, keep on up dating such good articles.

    software testing training

  21. I read this article. I think You put a lot of effort to create this article. I appreciate your work.
    Dissertation Writing Services

  22. Really great blog, it's very helpful and has great knowledgeable information.

    For your Best career in commerce | Avinash College Of Commerce

  23. Awe! What An Blog Very Helpful and interesting Really A great center for acquiring knowledge.Very Helpful Post And Explained Very Clearly About All the things.Very Helpful. Coming To Our Self We Provide Restaurant Equipment Parts .Really Thankfull For the blogger providing such a great information.Thank you. Have a Nice Day.

  24. Whatever We Are Gathering the information from the blogs,we have to keep on updating it,Urgent Care in Chicago.By getting them into one place.Really thanks for posting.Very Thankful for the Informative Post.

  25. Wow...What an excellent informative blog, really helpful. Thank you.

    Software Testing | Austere Technology

  26. Those guidelines additionally worked to become a good way to
    recognize that other people online have the identical fervor like mine
    to grasp great deal more around this condition.

    digital marketing training in chennai

    seo training in chennai

  27. Nice blog! This blog giving very useful information. Thanks for sharing with us.

    Dot Net Training in Chennai
    Java Training in Chennai

  28. IEEE Project Center Chennai, is a leading national wide consulting and 2018 IEEE services, offering a wide array of solutions customized for a range of key verticals and horizontals. We are the preferred choice for
    cse mini projects in chennai | mini projects in chennai