Search This Blog

Tuesday, February 22, 2011

Handling File upload using selenium.

Here I would like to share my experience on handling file upload using selenium.

To handle file upload using selenium RC (1.0 or higher) comes with two challenges.

1. Clicking on Browse/Upload button.
2. Selecting a file from windows dialog.

I will share handling this on both Fire fox and IE.


Fire Fox:


Its very simple when comes to handle file upload in Fire Fox. You just need to use selenium.type command for input file=type control.

Lets take below sample html code.

<input type="file" name="fileupload" id="filename" ></input>

All you just need to do is use below command to upload file.

selenium.type("//input[@name='fileupload']","c:\test.txt");


IE:


When it comes to IE its little tricky. Follow below steps.

1. Download AutoIt latest version.
2. Open new editor and past below code in editor.

If $CmdLine[0]<2 Then
Exit
EndIf


handleUpload($CmdLine[1],$CmdLine[2])


;define function to handleupload


Func handleUpload($title, $uploadFile)


if WinWait($title,"",4) Then
WinActivate($title)
ControlSetText($title,"","Edit1",$uploadFile) ;put file path into text fild
ControlClick($title,"","Button2")
Else
Return False
EndIf

EndFunc

4. Go to Tools menu build the code. It will generate an .exe (e.g. upload.exe)
5. Write a function in java as given below.

public void handleUpload(String windowtitle, String filepath) {
         String execute_file = "upload.exe";
         String cmd = "\"" + execute_file + "\"" + " " + "\"" + windowtitle + "\""
                                   + " " + "\"" + filepath + "\""; //with arguments
         try {
                 Process p = Runtime.getRuntime().exec(cmd);
                
         } catch (Exception e) {
                 e.printStackTrace();
         }
}


6. In your TC, remember that first you need to call above java function before click on browse button. Else control never comes back to your code if you click browse button first.


7. Now click on upload button using selenium.click("//input[@name='fileupload']").


Thats it!!

7 comments:

  1. hey hii Chinna..

    Thanks a lot for your precise solution.
    It is really useful for me. I have spend my two days into it and it is so simple. But still I can understand its depth.
    It is appreciated.Thanks again.

    Sonali..:-)

    ReplyDelete
  2. Hi Chinna.

    I have seen your earlier post. Currently i have a problem of handling browse button without text box beside it in firefox. when i click on brose button it opens the file choose window to select the file to attach and hence the file gets attached dynamically.Please suggest.

    Thanks in advance.

    Regards,
    Priya

    ReplyDelete
    Replies
    1. Hi Priya,

      can u please send me the code of "opens the file choose window to select the file to attach and file gets attached dynamically.

      Thanks in advance

      Regards,
      Mukesh

      Delete
  3. hi chinna... i am trying to use selenium.attachFile() method... i am unable to get it.... can you plese help regarding dis

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete