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.
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!!
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!!
No comments:
Post a Comment