Monday, November 29, 2010

How to upload a file in selenium

How to upload a file in selenium with the help of AutoIT

Recently, I had the challenge of writing some automation for a workflow which included uploading a file because uloading a file works on windows component.
selenium is unable to access windows components and it will be handled through AutoIT.
Once you are able to click on browse button and a dialog box is open to choose the file then you just run a AutoIT script which will help to select the file from your local or remote drive and control will come to your web page to proceed with selenium.

Step to choose a file from your local or remote drive

Step 1.

Download the AutoIT and intall it.

Download AutoIT from its homepage

step 2.

write a AutoIT script to choose a file from your local or remote drive.


#include
; Internet Explorer is partly integrated in shell.application
$oShell = ObjCreate("shell.application") ; Get the Windows Shell Object
$oShellWindows=$oShell.windows ; Get the collection of open shell Windows
$MyIExplorer=""
for $Window in $oShellWindows ; Count all existing shell windows
; Note: Internet Explorer appends a slash to the URL in it's window name
if StringInStr($Window.LocationURL,"http://") then
$MyIExplorer=$Window
exitloop
endif
next
$oForm = _IEGetObjByName ($MyIExplorer, "UploadedFile")
_IEAction($oForm, "click")
WinActivate("Choose file");
Local $file = "C:\Documents and Settings\intelizen\Desktop\selenium.txt"
ControlSetText("Choose file", "", "Edit1", $file )
ControlClick("Choose file", "", "Button2")

setp 3.

Complie AutoIT script and make exe of that script.

Right click on that saved script file and click on "Compile script" from context menu. This will make an exe file of that script.


setp 4.

Call that exe in selenium.

Process proc = Runtime.getRuntime().exec("C:\\farheen\\FileUpload.exe");

If you want to test this script then just download the script exe file and click on browse button on web page and run this exe file. Just make sure you have your file in correct path by changing this line of script in the second step
Local $file = "c:\yourpath\howtoupload.doc"
in above line of script change your file path then make exe of your script then click on browse button on your web page then run this exe . I am sure this would work.

Above script work for Firefox.Below for IE.

#include
; Internet Explorer is partly integrated in shell.application
$oShell = ObjCreate("shell.application") ; Get the Windows Shell Object
$oShellWindows=$oShell.windows ; Get the collection of open shell Windows
$MyIExplorer=""
for $Window in $oShellWindows ; Count all existing shell windows
; Note: Internet Explorer appends a slash to the URL in it's window name
if StringInStr($Window.LocationURL,"http://") then
$MyIExplorer=$Window
exitloop
endif
next
$oForm = _IEGetObjByName ($MyIExplorer, "UploadedFile")
_IEAction($oForm, "click")

WinActivate("File Upload");
Local $file = "c:\yourpath\howtoupload.doc"
ControlSetText("File Upload", "", "Edit1", $file )
ControlClick("File Upload", "", "Button2")

In above script you might need to change your file path and browse button name and the title of you dialog box.

No comments:

Post a Comment