File upload with Selenium

In my Selenium tests I want to upload some files. Due to security restrictions it is by default not possible that a Javascript fills in a path in the respective input field. But fortunately, there exists a workaround:

a) The Mozilla must have the configuration option
“signed.applets.codebase_principal_support” set to the value “true”.
This allows non-signed scripts to request higher privileges.

b) Selenium must request higher privileges which can be handled in
different places. To allow typing into file fields you can include this
call:

netscape.security.PrivilegeManager.enablePrivilege(“UniversalFileRead”);

in the file selenium-api.js in function Selenium.prototype.doType. This
enables uploading local files.

This workaround works also with Firefox.

About these ads

46 Comments

  1. alex
    Posted April 25, 2006 at 12:44 pm | Permalink

    Would you be able to go into any detail about how to test uploading files(eg excel) into an on-line app using selenium and IE

  2. alex
    Posted April 25, 2006 at 1:31 pm | Permalink

    Me again – is there any way to place the address of the file you are uploading into the text field – i tried type but got aunknown command error

  3. Posted April 25, 2006 at 2:44 pm | Permalink

    @alex: Sorry, I am not able to say anything about IE as I do not use IE. Maybe you will have more luck in the selenium forums: http://forums.openqa.org/index.jspa

    By default, it is not possible to place the address of the file you want to upload into the text field with “type” due to security restrictions, hence the workaround ;-) But I think this workaround is Mozilla-specific and does not work with IE…

  4. Pratik Mehta
    Posted September 26, 2006 at 6:43 pm | Permalink

    You mentioned that the workaround works in firefox. I tried updating the function Selenium.prototype.doType in the file selenium-api.js and when I execute tests after that, they always stop on ‘type’ command. Any idea as to how this can be fixed?

    Thanks in advance for your help,

  5. Posted September 27, 2006 at 2:05 pm | Permalink

    @Pratik: Did you perform step a)?

  6. Pratik Mehta
    Posted September 27, 2006 at 5:56 pm | Permalink

    Yes, I did that…

  7. Posted September 28, 2006 at 1:28 pm | Permalink

    @Pratik: What version of Selenium do you use? I had some issues with Selenium 0.7, which were fixed in 0.7.1. Or do you already use Selenium 0.8?

  8. Pratik Mehta
    Posted September 28, 2006 at 5:41 pm | Permalink

    We are using Selenium RC 0.8.1

  9. Posted October 1, 2006 at 8:34 am | Permalink

    @Pratik: I am sorry, but I don’t have an idea what could be the problem. Maybe you will have more luck in the forums on http://www.openqa.org ?

  10. Tester420
    Posted November 9, 2006 at 5:38 pm | Permalink

    I am not able to include this call into selenium-api.js file.
    netscape.security.PrivilegeManager.enablePrivilege(”UniversalFileRead”);

    I am getting error on save as : some characters canot be mapped using “ISO-8859-1″ chanacter encoding. Do I need to chance any other selenium files?

    Thanks in advance for you help..

  11. Posted November 10, 2006 at 8:26 am | Permalink

    @Tester420: Try to replace the ” characters with “.

  12. Posted November 10, 2006 at 8:29 am | Permalink

    @Tester420: Hm, wordpress seems to replace automatically the quotation marks. So use just the quotation mark you usually use in your editor, and not the one used here ;-)

  13. Tester420
    Posted November 13, 2006 at 6:28 pm | Permalink

    Now I am able to add fileupload line into selenium-api.js file. Thanks…

    Workaround is not working for me :-(

    I have added
    pref(“signed.applets.codebase_principal_support”, true); into all.ja file unde firefox folder
    C:\Program Files\Mozilla Firefox\greprefs.

    Still selenium hangs at fileupload.

    am I missing anything?

    thanks in advance.

  14. Posted November 14, 2006 at 5:10 pm | Permalink

    @Tester420: Hm, I don’t know why it doesn’t work… Maybe you will have more luck in the forums on http://www.openqa.org/

  15. Posted February 26, 2007 at 10:13 pm | Permalink

    Here are the steps I have followed
    1. created the file C:\Program Files\Mozilla Firefox\defaults\profile\user.js with one line in it
    user_pref(“signed.applets.codebase_principal_support”, true);
    2. Added to Selenium.prototype.doType = function(locator, value) {
    //Added by Nick Skriloff according to http://cakebaker.wordpress.com/2006/03/29/file-upload-with-selenium/
    netscape.security.PrivilegeManager.enablePrivilege(‘UniversalFileRead’);

    where is the .js file with this call? When the browser gets there, it says it cannot find this method.

  16. Manisha
    Posted June 6, 2007 at 8:42 am | Permalink

    Hi,
    I have a browse button, which takes a pdf file and uploads it when saved.
    I used the command type for the script, here is the tag:

    type
    essayFile
    F:\books\frombhai\C++\borland_c++_unleashed.pdf

    clickAndWait
    //input[@value='Save']

    did the following steps:
    1)Added pref(”signed.applets.codebase_principal_support”, true); into all.ja file unde firefox folder
    C:\Program Files\Mozilla Firefox\greprefs.
    2)Added to Selenium.prototype.doType = function(locator, value) {
    netscape.security.PrivilegeManager.enablePrivilege(’UniversalFileRead’);

    Still it dosent work, please let me know what am I doing wrong?

    thanks,
    Manisha

  17. Thara
    Posted August 3, 2007 at 5:53 am | Permalink

    How to write scripts for “Upload File” using selenium

  18. Yassine
    Posted September 6, 2007 at 5:34 pm | Permalink

    Hi CakeBakcer,
    I have tried your solution concerning the upload file, it works for Selenium IDE: Selenium IDE could type the whole path of the file that I want to upload, but with Selenium RC based on Junit,it does not work, the test fails exactly at this step

    Could you help please and give more information if there a workaround.

    Thanks.

  19. Jon Alvarado
    Posted September 18, 2007 at 12:10 am | Permalink

    Also, one could use the *chrome launcher instead of the *firefox launcher. This worked for me to solve my upload issues.

  20. Masha
    Posted December 12, 2007 at 11:06 pm | Permalink

    thanks a lot! this workaround worked really nicely, but only with *chrome launcher :)

  21. Surbhi
    Posted January 17, 2008 at 11:40 am | Permalink

    Hi Cakebacker,
    I have been trying to find this since very long. Thanks for the idea.
    I am using selenium on Rails. I performed both the steps mentioned. I am running my sutie though testrunner and after I make the change in the selenium api file, it stope generating the suite of test scripts. What should I do?

  22. Posted May 12, 2008 at 1:58 pm | Permalink

    Hi.

    I couldn’t get it to work either. I actually was forced to re-install Firefox (after I’ve tried the above I couldn’t get Selenium to type into ANY fields without rising a security error.

    Any way…

    I entered “about:config” into the address field. And changed signed.applets.codebase_principal_support from FALSE to TRUE from there…

    Without even restarting Firefox selenium asked if I wanted to accept the granted permissions, I clicked yes and now everything works great!

    Thanks for showing the way. I hope this last bit of the pussle helps someone else…

  23. Posted May 14, 2008 at 4:31 pm | Permalink

    @Nicklas: I’m glad to hear this article was helpful for you.

    Btw: This blog moved to http://cakebaker.42dh.com

  24. naveen saxena
    Posted September 4, 2008 at 7:32 am | Permalink

    I have done all below step …as you mentione here…

    The Mozilla must have the configuration option
    “signed.applets.codebase_principal_support” set to the value “true”.
    This allows non-signed scripts to request higher privileges.

    b) Selenium must request higher privileges which can be handled in
    different places. To allow typing into file fields you can include this
    call:

    netscape.security.PrivilegeManager.enablePrivilege(”UniversalFileRead”);

    in the file selenium-api.js in function Selenium.prototype.doType.

    i have query that i am working on fire fox so that as you said netscape.security.PrivilegeManager.enablePrivilege(”UniversalFileRead”);
    so it will work for fire fox or there is diff command for this……

    and suggest me that the file selenium-api.js…i have to add command so from where i will get this file selenium-api.js and after modification where this file be kept…………………………….

    reply soom
    URGENT….

  25. ARYAN saxena
    Posted September 4, 2008 at 7:33 am | Permalink

    I have done all below step …as you mentione here…

    The Mozilla must have the configuration option
    “signed.applets.codebase_principal_support” set to the value “true”.
    This allows non-signed scripts to request higher privileges.

    b) Selenium must request higher privileges which can be handled in
    different places. To allow typing into file fields you can include this
    call:

    netscape.security.PrivilegeManager.enablePrivilege(”UniversalFileRead”);

    in the file selenium-api.js in function Selenium.prototype.doType.

    i have query that i am working on fire fox so that as you said netscape.security.PrivilegeManager.enablePrivilege(”UniversalFileRead”);
    so it will work for fire fox or there is diff command for this……

    and suggest me that the file selenium-api.js…i have to add command so from where i will get this file selenium-api.js and after modification where this file be kept…………………………….

    reply soom
    URGENT….

  26. kc
    Posted September 6, 2008 at 1:13 am | Permalink

    It works, thanks!
    Steps:
    1. Goto Mozilla Firefox > greprefs > all.js > add the line
    pref(“signed.applets.codebase_principal_support”, true);
    2. Goto selenium-server.jar and modify the selenium-api.js and modify like previous thread said

    Added to Selenium.prototype.doType = function(locator, value) {
    netscape.security.PrivilegeManager.enablePrivilege(’UniversalFileRead’);

    3. In selenium use “*firefox”
    It should work.

  27. Desperado
    Posted November 14, 2008 at 4:50 pm | Permalink

    It works also for me, thanks! :-)
    Only one question, I always need to accept following Internet Security message:
    “A script is requesting enhanced abilities that are UNSAFE and could be used to compromise your machine or data:
    Read and upload local files
    Allow these abilities only if you trust this source to be free of viruses or malicioua programs.”
    It also has a “Remember this decision”-checkbox, but although I check it, the window always pops up again when I start a new browser session.

    Is there a way to make this popup window not to popup each time?

  28. Miggs
    Posted December 18, 2008 at 10:49 am | Permalink

    Is there also a similar solution for Safari on a MAC OS X?

    • Posted December 18, 2008 at 4:24 pm | Permalink

      @Miggs: I don’t know whether there is a similar solution for Safari as I’m not a Mac user…

  29. Posted February 10, 2009 at 9:58 am | Permalink

    I have a page with 2 file upload items and while it is working on the first one, for the second it is balking at the iframe not found. I am using ajax to do javascript based async submission after the first file is uploaded and want to go ahead with the second file upload automatically after the first one is uploaded…

    Any workarounds?

  30. Santosh
    Posted February 27, 2009 at 12:18 pm | Permalink

    Hi,
    I have a browse button on my application page, which takes accepts Image file then i need to save the application.

    I need to do this operation only on IE browser ( So i’m using Eclipse) , as my application don’t support any other browser not even FF.

    I have tried with the below code:

    selenium.type(“quiz_image”, “C:\\Test photo\\C.jpg”);

    But my script is escaping the above part and going to the next step without performing it.

    Pls suggest me some solution asap. Its really urgent for me:)

    Thanks,
    Santosh

  31. Posted April 8, 2009 at 12:43 pm | Permalink

    Here is my solution for IE written in Java:
    http://gusiev.com/?p=3

  32. yugi
    Posted April 9, 2009 at 4:29 pm | Permalink

    Hi cakebaker,

    Can you post the upload script? I can’t image how it work.
    I’m using Python to write some automation actions for a website. Thanks!

  33. Amit Vibhuti
    Posted May 30, 2009 at 11:09 am | Permalink

    This works :)

    http://seleniumdeal.blogspot.com/2009/05/uploading-files-using-selenium-with-c.html

  34. Narendra
    Posted June 2, 2009 at 11:49 am | Permalink

    I tried this for uploading files but it is not working.if add the code into function selenium.prototype.dotype it not allowing any other test cases which were working before.

    • Posted June 4, 2009 at 4:04 pm | Permalink

      Hm, did you set “signed.applets.codebase_principal_support” to “true” in Firefox?

  35. Posted October 25, 2011 at 7:52 am | Permalink

    I realise this conversation took place a few years ago, but here’s a cleaner solution using WebDriver (and firefox):

    http://stackoverflow.com/questions/6128208/how-to-deal-with-file-uploading-in-test-automation-using-selenium-or-webdriver

    Basically:
    driver = … // Init Firefox driver
    element = driver.find_element_by_id(“fileUpload”)
    element.send_keys(“/path/to/myfile.txt”)

  36. Ram
    Posted December 14, 2011 at 12:46 pm | Permalink

    Hey guys this didn’t work out for me, well i followed the instructions http://code.google.com/p/selenium/issues/detail?id=2826 and wow thats absolutely fine. Refer comment 14.

  37. Posted September 5, 2012 at 8:32 am | Permalink

    Saved as a favorite, I love your site!

  38. Posted February 21, 2013 at 10:56 pm | Permalink

    Howdy! I realize this is sort of off-topic however I needed to ask.

    Does running a well-established website such as yours require a large amount of work?

    I’m completely new to operating a blog but I do write in my diary daily. I’d like to start a
    blog so I can easily share my personal experience and thoughts online.
    Please let me know if you have any kind of recommendations or tips
    for new aspiring bloggers. Thankyou!

  39. Posted May 5, 2013 at 5:37 pm | Permalink

    Then log into the router using your browser at the default IP address 192.
    Website design is key no matter what type of site you are running.

    To do so, simply right click on a page with a flash game and click view
    page info.

  40. Posted May 8, 2013 at 2:40 pm | Permalink

    This means that you get other sites to your link to yours and you return the favor.
    There are other simply amazing advertising opportunities, where it is relatively cheap
    to advertise on other people’s sites and blogs including a number of other excellent options. Having said that, this belief is absolutely improper.

  41. Posted May 26, 2013 at 6:48 am | Permalink

    Buying a car is not for the faint of heart – it can cause
    you to break out in a sweat at the salesman runs his pitch on you.
    Don’t worry! The tips in this article will ensure you’re prepared for the battle
    which lies ahead, allowing you to get the bargain you deserve.

  42. Posted July 3, 2013 at 6:24 pm | Permalink

    My family members always say that I am killing my time
    here at net, however I know I am getting know-how everyday by reading such fastidious content.

  43. Posted August 1, 2013 at 6:35 pm | Permalink

    It’s amazing to visit this website and reading the views of all friends regarding this post, while I am also zealous of getting know-how.

  44. Posted March 8, 2014 at 9:40 pm | Permalink

    I will immediately take hold of your rss as I can’t find your e-mail subscription hyperlink or newsletter service. Do you’ve any? Kindly allow me recognize in order that I could subscribe. Thanks.
    KAYSON http://www.net-ict.be/


3 Trackbacks/Pingbacks

  1. [...] http://cakebaker.wordpress.com/2006/03/29/file-upload-with-selenium/ [...]

  2. [...] to have some problems when it comes to testing file uploads with Selenium RC. I’m not the first to provide a solution but making this work with Selenium RC might take a few more [...]

  3. [...] you test file uploads using Selenium?” By default, no. There is a Firefox workaround that involves actually changing Firefox’s security, and a change to Selenium’s code to allow it to [...]

Post a Comment

Required fields are marked *
*
*

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: