Thursday, March 21, 2013

Uploading with Selenium RC in FF or IE


Here I would like to share my experience on handling file upload using selenium. Handling File upload with selenium I spent more than two days and googled many sites to overcom this. At last I was able to crack this and found solution for the same. This script is devledoped in Java and can be easily coverted to any other langugage.
To handle file upload using selenium RC (1.0 or higher) comes with two challenges.
1. Clicking on Browse/Upload button. ( As Upload buttion come with input type File which is combination of both text box and Browse button).
2. Selecting a file from windows dialog.
I will share handling this on both Fire fox and IE.
Handling file upload in Fire Fox:

Its really 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.


All you just need to do is use below command to upload file.
selenium.type("//input[@name='fileupload']","c:\test.txt")
Type command take care of uploading file automatically. No other code is required to handle this.

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 data-blogger-escaped-controlclick="" data-blogger-escaped-controlsettext="" data-blogger-escaped-define="" data-blogger-escaped-dit1="" data-blogger-escaped-else="" data-blogger-escaped-endfunc="" data-blogger-escaped-endif="" data-blogger-escaped-exit="" data-blogger-escaped-false="" data-blogger-escaped-fild="" data-blogger-escaped-file="" data-blogger-escaped-func="" data-blogger-escaped-function="" data-blogger-escaped-handleupload="" data-blogger-escaped-if="" data-blogger-escaped-into="" data-blogger-escaped-mdline="" data-blogger-escaped-path="" data-blogger-escaped-pre="" data-blogger-escaped-put="" data-blogger-escaped-return="" data-blogger-escaped-text="" data-blogger-escaped-then="" data-blogger-escaped-to="" data-blogger-escaped-uploadfile="" data-blogger-escaped-utton2="" data-blogger-escaped-winactivate="" data-blogger-escaped-winwait="" title="">
4. Go to Tools menu click on build the code. It will generate an .exe (e.g. upload.exe)
5. Now write a function in java to make a call to aboe auto it function. Below is sample code for the same.

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 you TC first call above java function ( remember first you need to call 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']"). In other browsers like chrome and safari I tired either type or AutoIt code will function successfully. Taken From http://www.boddunan.com/articles/programming/69-others/18396-handling-file-upload-using-selenium-rc-in-ie-and-firefox.html NOTE: I still have no luck with auto uploading in swf flash auto upload..