File upload with Selenium

Published on and tagged with selenium  testing  tip

In my Selenium tests I want to upload some files. However, due to security restrictions in the browser a JavaScript cannot fill in a path in the respective file input field by default. Fortunately, there exists a workaround (originally from http://lists.public.thoughtworks.org/pipermail/selenium-users/2005-March/000213.html).

In FireFox enter “about:config” in the address bar, and set the value of “signed.applets.codebase_principal_support” to “true”. This allows non-signed scripts to request higher privileges.

In Selenium we then have to request the privilege to upload local files by adding the following JavaScript snippet to the function “Selenium.prototype.doType” in the file “selenium-api.js”:

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

Update 2009-01-04: Fixing links, fixing problem with quotes which got messed up by WordPress.

40 comments baked

  • alex

    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

  • alex

    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

  • cakebaker

    @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…

  • Surajit Basumallick

    Please help me about file upload section. I am trying to upload a file and store it into a folder not database.
    another how to use GD using cakephp? please help me.my all try in vain.

  • cakebaker

    @Surajit: Have a look at http://php.net/manual/en/features.file-upload.php and my article about file uploads: http://cakebaker.42dh.com/2006/04/15/file-upload-with-cakephp/

    I don’t have experience with GD, but maybe someone in the google group (http://groups.google.com/group/cake-php) has experience with it.

  • Manisha

    Hi ,
    I have a browse button in my html which uploads the pdf file. used the command type which dosen’t work.

    I also performed these steps:

    1)Added pref(”signed.applets.codebase_principal_support”, true); into all.js file under firefox folder
    C:\Program Files\Mozilla Firefox\greprefs.

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

    Still it doesn’t work, please let me know what am I doing wrong?

    Thanks,

    With regards,

    Manisha

  • cakebaker

    @Manisha: I am not sure if this hack still works as this article is quite old, and I have switched to the Selenium IDE in the meantime. With the Selenium IDE, file uploads work without problems.

    Hope that helps.

  • taylor

    I know it’s an old thread, but I thought I’d weigh in because I was looking for a solution to this problem and found it here.

    In addition to the IDE, I was able to upload files with Selenium Core v0.8.2 by using the hack as instructed. The relevant section of selenium-api.js looks like this:

    Selenium.prototype.doType = function(locator, value) {
    /**
    * Sets the value of an input field, as though you typed it in.
    *
    * Can also be used to set the value of combo boxes, check boxes, etc. In these cases,
    * value should be the value of the option selected, not the visible text.
    *
    * @param locator an element locator
    * @param value the value to type
    */

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

    if (this.browserbot.controlKeyDown || this.browserbot.altKeyDown || this.browserbot.metaKeyDown) {
    throw new SeleniumError(“type not supported immediately after call to controlKeyDown() or altKeyDown() or metaKeyDown()”);
    }
    // TODO fail if it can’t be typed into.
    var element = this.browserbot.findElement(locator);
    if (this.browserbot.shiftKeyDown) {
    value = new String(value).toUpperCase();
    }
    this.browserbot.replaceText(element, value);
    };

    Taylor Dorris
    http://www.proace.com

  • cakebaker

    @taylor: Thanks for this code snippet!

  • shik

    Selenium don’t work with “netscape.security.PrivilegeManager.enablePrivilege(”UniversalFileRead”);” modofoed =(

  • cakebaker

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

  • tony

    I don’t know, if you really want to set signed.applets.codebase_principal_support to true in your Firefox.
    I have found another way to make change the security settings only for the Firefox instance started by
    Selenium RC.

    You have to patch the Selenium server by adding as user.js like this

    user_pref("signed.applets.codebase_principal_support", true);
    
    user_pref("capability.principal.codebase.p0.granted", "UniversalFileRead");
    user_pref("capability.principal.codebase.p0.id", "http://YOUR_AUT_DOMAIN1");
    user_pref("capability.principal.codebase.p0.subjectName", "");
    
    user_pref("capability.principal.codebase.p1.granted", "UniversalFileRead");
    user_pref("capability.principal.codebase.p1.id", "http://YOUR_AUT_DOMAIN2");
    user_pref("capability.principal.codebase.p1.subjectName", "");

    to your selenium-server.jar in the directory customProfileDirCUSTFF.

    As you can see, you can “register” multiple domains for your different test machines.

    The effect is, that Selenium creates a custom Firefox profile in your temp directory
    using the customProfileDirCUSTFF folder. So the user.js will be injected
    to your Selenium Firefox instance and its configuration will extend or override the settings
    of all.js and prefs.js.

    Additionally this solutions should work with all Firefoxes started by the patched Selenium server.

  • cakebaker

    @tony: Thanks for your explanations! Seems to be a good approach.

  • Jaya

    Hi,

    I did both the steps as you said but then its just opening the browser and nothing else is happening after that. Not even a single command is exceuted after that.

    Any help??
    Am using FF3 and sel v0.9.2 [2006], with Core v0.8.3 [1879] and v1.0-beta-1 [2201], with Core v1.0-beta-1 [1994]

  • cakebaker

    @Jaya: Thanks for your comment!

    I just tested it with FF 3.0.5 and Selenium Core 0.8.3, and it worked fine. However, if you copied the JavaScript snippet from this article, you have to replace the quotes with those quotes used by JavaScript (somehow the quotes got messed up by WordPress). It should be fixed now.

    Hope this helps!

  • Jaya

    Hi cakebaker, thankx for the comment.
    But i actually did that and it worked :) .

    SO vats up with FF2, do we need to follow the above steps or just modifying the about:config should help?

  • cakebaker

    @Jaya: You have to follow both steps.

  • Maruthiprasath

    thanks for ur post.
    it was great .. after long struggle i got the answer..
    u rock man..
    but we need to follow both steps

  • cakebaker

    @Maruthiprasath: You are welcome! And yes, you have to follow both steps.

  • Bogdan

    Good browser independent solution can be found here:
    http://gusiev.com/?p=3 files with Selenium IDE

  • cakebaker

    @Bogdan: Thanks for the link! However, if you use the Selenium IDE the file upload should work without any “hacks”.

  • Amit Vibhuti
  • cakebaker

    @Amit: Thanks for the link!

  • Levent

    I use selenium with ruby and firefox 3.5
    I modified the user_pref of firefox as specified, but I don’t know how to deal with the step involving
    netscape.security.PrivilegeManager.enablePrivilege(”UniversalFileRead”);
    It seems this is only applicable for the selenium java client.
    Is there a way to do it from Ruby?

  • Levent

    I found the relevant code in selenium-api.js in selenium-server.jar and put in netscape.security.PrivilegeManager.enablePrivilege(’UniversalFileRead’);
    but it seemed to have no effect. I still see no upload in firefox after it clicks the submit button. Has anyone tried it in firefox 3.5+?

  • Prabin

    Hey Levent! Did you get this working in firefox 3.5+ ?? This is shame, it does not seem to work with 3.5 at all. I am using an older version of selenium core though – 0.8.
    I have done everything that cakebaker tells me to do. Am I missing something?

  • Priyanka

    Hi How can I upload a file in selenium , as it gives a security error .
    I am using command
    Sel.selenium.type(“attachment_upload_data”,”file”);

    It gives error -Security error

    What can I Do ??

  • Levent

    No, we could not get it working. For uploads, we either use IDE or some special JS code in the application.

  • Prakash

    public bool TypeIntoFileUpload(string controlId, string filePath)
    {
    try
    {
    string newFilePath = filePath.Replace(‘\\’, ‘/’);
    _selObj.WindowFocus();
    _selObj.Focus(controlId);
    string jscript=””;
    jscript += “if(selenium.browserbot.getCurrentWindow().clipboardData){window.clipboardData.setData(‘Text’,'” + newFilePath + “‘);}”;
    _selObj.GetEval(jscript);
    byte VK_CONTROL = 0x11;
    byte VK_V = 0x56;
    _selObj.KeyDownNative(Convert.ToString(VK_CONTROL));
    _selObj.KeyPressNative(Convert.ToString(VK_V));
    _selObj.KeyUpNative(Convert.ToString(VK_CONTROL));

    return true;
    }
    catch (Exception exc)
    {
    return false;
    }
    }

  • Prakash

    Uploading Files using Selenium RC in C#
    After googling all around to overcome the Selenium incapability of handling File Upload control finally I could write a simple function in C# which will do my job. In the function given below _selObj is the object of DefaultSelenium class.

    public bool TypeIntoFileUpload(string controlId, string filePath)
    {
    try
    {
    string newFilePath = filePath.Replace(‘\\’, ‘/’);
    _selObj.WindowFocus();
    _selObj.Focus(controlId);
    string jscript=””;
    jscript += “if(selenium.browserbot.getCurrentWindow().clipboardData){window.clipboardData.setData(‘Text’,'” + newFilePath + “‘);}”;
    _selObj.GetEval(jscript);
    byte VK_CONTROL = 0x11;
    byte VK_V = 0x56;
    _selObj.KeyDownNative(Convert.ToString(VK_CONTROL));
    _selObj.KeyPressNative(Convert.ToString(VK_V));
    _selObj.KeyUpNative(Convert.ToString(VK_CONTROL));

    return true;
    }
    catch (Exception exc)
    {
    return false;
    }
    }

  • Anu

    Hi Guys,

    I am using Selenium IDE and file upload was working till yesterday but from today morning its throwing error, when i run each command it works but when i run the whole test case its failing to upload. and throwing error as below.

    Any help is very appreciated

    [error] Unexpected Exception: code -> 1000, INDEX_SIZE_ERR -> 1, DOMSTRING_SIZE_ERR -> 2, HIERARCHY_REQUEST_ERR -> 3, WRONG_DOCUMENT_ERR -> 4, INVALID_CHARACTER_ERR -> 5, NO_DATA_ALLOWED_ERR -> 6, NO_MODIFICATION_ALLOWED_ERR -> 7, NOT_FOUND_ERR -> 8, NOT_SUPPORTED_ERR -> 9, INUSE_ATTRIBUTE_ERR -> 10, INVALID_STATE_ERR -> 11, SYNTAX_ERR -> 12, INVALID_MODIFICATION_ERR -> 13, NAMESPACE_ERR -> 14, INVALID_ACCESS_ERR -> 15, VALIDATION_ERR -> 16, TYPE_MISMATCH_ERR -> 17, message -> Security error, result -> 2152924136, name -> NS_ERROR_DOM_SECURITY_ERR, filename -> XPCSafeJSObjectWrapper.cpp, lineNumber -> 450, columnNumber -> 0, location -> JS frame :: XPCSafeJSObjectWrapper.cpp :: anonymous :: line 450, inner -> null, data -> null

  • cakebaker

    @Anu: Hm, did you change anything in your environment (e.g. updating your browser)?

  • Anu

    Yes, I did as we were asked to upgrade to latest fire fox version 3.6.7

  • cakebaker

    @Anu: Hm, does the problem still exist with firefox 3.6.8? You may also checkout the Selenium Users Group, maybe someone there experienced the same problem?

  • Jonathan

    1) Has anyone tried the above fix by Prakash using “public bool TypeIntoFileUpload” and did it work?

    2) If yes, What are the details as to exactly this code integrated into the Selenium environment?

    Thanks,

    Jonathan

  • cakebaker

    @Jonathan: I can’t say anything about the fix by Prakash as I don’t work with C#…

  • saima

    @cakebaker : I m testing an application in which a pop up window comes up and i have to upload a file on that window. I tried using the above 2 fixes suggested by you. But its not working.
    I get a similar file upload error as uploaded by Anu.

    [error] Unexpected Exception: code -> 1000, INDEX_SIZE_ERR -> 1, DOMSTRING_SIZE_ERR -> 2, HIERARCHY_REQUEST_ERR -> 3, WRONG_DOCUMENT_ERR -> 4, INVALID_CHARACTER_ERR -> 5, NO_DATA_ALLOWED_ERR -> 6, NO_MODIFICATION_ALLOWED_ERR -> 7, NOT_FOUND_ERR -> 8, NOT_SUPPORTED_ERR -> 9, INUSE_ATTRIBUTE_ERR -> 10, INVALID_STATE_ERR -> 11, SYNTAX_ERR -> 12, INVALID_MODIFICATION_ERR -> 13, NAMESPACE_ERR -> 14, INVALID_ACCESS_ERR -> 15, VALIDATION_ERR -> 16, TYPE_MISMATCH_ERR -> 17, message -> Security error, result -> 2152924136, name -> NS_ERROR_DOM_SECURITY_ERR, filename -> null, lineNumber -> 0, columnNumber -> 0, location -> native frame :: :: :: line 0, inner -> null, data -> null

    I m using selenium IDE 1.0.10 and firefox 3.6.17. Could u help out?
    Also ,i edited the selenium-api.js file that i edited is at the path:

    C:\Documents and Settings\saima\Application Data\Mozilla\Firefox\Profiles\r8b1jve2.default\extensions\{a6fd85ed-e919-4a43-a5af-8da18bda539f}\chrome\content\selenium\scripts

    Is this the correct path for selenium-api.js ?

  • Anu

    Hi All,

    Uploading file issue still exists with Selenium IDE because of security issues with the browser(as far as i know), but currently we have moved to Selenium RC using C# and uploading file issue has been resolved with a third party tool called Autohotkey [THIS ONLY WORKS FOR IE, we are still trying to fix Firefox ].

    If any one is struggling to upload a file in Selenium RC this is a good solution for Internet explorer.

    I will come back with steps

  • Amir

    Here is a post which addresses file upload issue with selenium.

    http://www.testingexcellence.com/how-to-upload-files-using-selenium-and-autoit/

  • kavitha rajendran

    Hi,
    After modifying selenium-api.js file, i have created selenium-server.jar file by follwoing command.
    jar -cvf selenium-sevrer.jar selenium-server

    But when I am trying to start selenium server, I am getting following error.
    D:\krajendr\Desktop\debug>java -jar seleniumserver.jar
    Failed to load Main-Class manifest attribute from
    seleniumserver.jar

    D:\krajendr\Desktop\debug>
    please let me know the solution, if anyone knows.

  • Homepage

    … [Trackback]

    […] Read More here: cakebaker.42dh.com/2006/03/29/file-upload-with-selenium/ […]

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License