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.
42 Comments
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
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
@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…
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,
@Pratik: Did you perform step a)?
Yes, I did that…
@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?
We are using Selenium RC 0.8.1
@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 ?
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..
@Tester420: Try to replace the ” characters with “.
@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 ;-)
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.
@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/
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.
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
How to write scripts for “Upload File” using selenium
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.
Also, one could use the *chrome launcher instead of the *firefox launcher. This worked for me to solve my upload issues.
thanks a lot! this workaround worked really nicely, but only with *chrome launcher :)
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?
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…
@Nicklas: I’m glad to hear this article was helpful for you.
Btw: This blog moved to http://cakebaker.42dh.com
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….
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….
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.
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?
Is there also a similar solution for Safari on a MAC OS X?
@Miggs: I don’t know whether there is a similar solution for Safari as I’m not a Mac user…
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?
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
Here is my solution for IE written in Java:
http://gusiev.com/?p=3
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!
This works :)
http://seleniumdeal.blogspot.com/2009/05/uploading-files-using-selenium-with-c.html
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.
Hm, did you set “signed.applets.codebase_principal_support” to “true” in Firefox?
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”)
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.
Saved as a favorite, I love your site!
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!
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.
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.
3 Trackbacks/Pingbacks
[...] http://cakebaker.wordpress.com/2006/03/29/file-upload-with-selenium/ [...]
[...] 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 [...]
[...] 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 [...]