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 (http://dev.w3.org/2006/webapi/XMLHttpRequest-2/) 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 (http://dev.w3.org/2006/webapi/FileAPI/). 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');")

selenium.text_field("//input[@type='file']").set("path/to/file")


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

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

68 comments:

  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?

    Thx

    love and light

    marc

    ReplyDelete
    Replies
    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

      Delete
  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

    marc

    ReplyDelete
  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

    http://html5demos.com/dnd-upload

    Thank you

    ReplyDelete
  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|

    ReplyDelete
  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

    ReplyDelete
  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

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

    ReplyDelete
  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

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

    ReplyDelete
  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

    ReplyDelete
  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

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

    NO.1 APP DEVELOPMENT SERVICES | MASSIL TECHNOLOGIES

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

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

    Best IT Security Services | Austere Technologies

    ReplyDelete
  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

    ReplyDelete
  19. This comment has been removed by a blog administrator.

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

    software testing training

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

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

    For your Best career in commerce | Avinash College Of Commerce

    ReplyDelete
  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.

    ReplyDelete
  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.

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

    Software Testing | Austere Technology

    ReplyDelete
  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

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

    Dot Net Training in Chennai
    Java Training in Chennai

    ReplyDelete
  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

    ReplyDelete
  29. The information you provided in the article is useful and beneficial USMLE Really Thankful For the blogger providing such a great information. Thank you. Have a Nice Day.

    ReplyDelete
  30. Great post! Thanks for sharing this valuable information.

    Web Designing Training in Chennai

    ReplyDelete
  31. Excellent information you made in this blog, very helpful information. Thanks for sharing.

    chartered accountant | Avinash college of commerce

    ReplyDelete
  32. Thanks For Sharing Valuable Information, Very Useful For U Medical Residency in USA

    ReplyDelete
  33. Great article, really very helpful content you made. Thank you, keep sharing.

    Best Degree Colleges Hyderabad | Avinash College of Commerce

    ReplyDelete
  34. Nice Blog, Thanks for sharing this valuable one.This is very useful for me and gain more information.

    Dot Net Training in Chennai

    ReplyDelete
  35. Selenium automates browsers. That's it! What you do with that power is entirely up to you. Primarily, it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) be automated as well.

    Selenium has the support of some of the largest browser vendors who have taken (or are taking) steps to make Selenium a native part of their browser. It is also the core technology in countless other browser automation tools, APIs and frameworks.

    SEO Training in Chennai

    ReplyDelete
  36. Hi Thanks for the nice information its very useful to read your blog. We provide best Block Chain Services

    ReplyDelete
  37. Thank you for sharing this valuable information. But get out of this busy life and find some peace with a beautiful trip book best Andaman honeymoon packages

    ReplyDelete
  38. Thanks for sharing with us. This is very Great aticle , it gives more useful information.

    Java Training in Chennai

    ReplyDelete
  39. Thanks for sharing the valuable information to share with us For more valuable information please visit our website.

    selenium training in chennai


    aws training in chennai

    ReplyDelete
  40. Thank you for sharing this valuable information. But get out this busy life and find some peace with a beautiful trip. book ANDAMAN HOLIDAY PACKAGES @ 35999

    ReplyDelete
  41. Thank you for sharing this valuable information. But get out this busy life and find some peace with a beautiful trip. book CHEAP ANDAMAN PACKAGES @9999

    ReplyDelete
  42. Thank you for sharing this valuable information. But get out this busy life and find some peace with a beautiful trip. book Andaman Tourism

    ReplyDelete
  43. Thank you for sharing this valuable information. But get out this busy life and find some peace with a beautiful trip. book Best Travel Agency In India

    ReplyDelete
  44. Hi Thanks for the nice information its very useful to read your blog. We provide About Best Software Development Services

    ReplyDelete
  45. The blog or and best that is extremely useful to keep I can share the ideas of the future as this is really what I was looking for, I am very comfortable and pleased to come here. Thank you very much.

    Digital Marketing Course in Chennai
    Digital Marketing Training in Chennai
    Online Digital Marketing Training
    SEO Training in Chennai

    ReplyDelete
  46. Hi Thanks for the nice information its very useful to read your blog. We provide best Find All Isfs Courses

    ReplyDelete
  47. Hi Thanks for the nice information its very useful to read your blog. We provide best Massil Technologies

    ReplyDelete
  48. mytectra placement Portal is a Web based portal brings Potentials Employers and myTectra Candidates on a common platform for placement assistance.

    ReplyDelete
  49. Thank you for giving them this great knowledge

    Susan

    ReplyDelete
  50. Hi Thanks for the nice information its very useful to read your blog. We provide best Association Of Chartered Certified Accountants

    ReplyDelete
  51. Your article gives lots of information to me. Thanks for sharing.
    cima courses in hyderabad | ISFS

    ReplyDelete