Pages

Search This Blog

Sunday, September 26, 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



setp 2.

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



#include <IE.au3>
; 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:\\niraj\\FileUpload.exe");

Download AutoIT script
Download exe file of AutoIT script


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.

If it still is nor working then change the script like


#include <IE.au3>
; 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.

Download the AutoIT script


Example

Below is an example in selenium RC for uploading your file which works on


Step 1.

First download the zip file and extact the files . There are 2 files in this one "Browse.a3" is for clicking on browse button and other one is "upload.a3" for selecting the path from your location and open it in dialog box.Make exe of both files and use in selenium RC like in second step.

Download the zip file

Step 2.

Write a selenium rc script like below . This script will works file with internet explorer. This script will open http://www.pdfonline.com/convert-pdf there you
will get a browse button. when Process proc = Runtime.getRuntime().exec("C:\\Documents and Settings\\nirkumar\\Desktop\\Browse.exe"); executes then it will
identify the browser and try to identify the element "Browse button" In attached files "Browse.au3" identtifies the button with name of "File1" but
if you have differen name of your browse button then open then "Browse.au3" file and change the line " $oForm = _IEGetObjByName($MyIExplorer, "Your browse button name") and save it and
make exe of that file. Now call this file in selenium. It will click on browse button.


import java.io.IOException;
import org.openqa.selenium.server.RemoteControlConfiguration;
import org.openqa.selenium.server.SeleniumServer;
import com.thoughtworks.selenium.*;
public class Uploadingfiles extends SeleneseTestCase{
 Selenium selenium;
 public static final String MAX_WAIT_TIME_IN_MS="60000";
 private SeleniumServer seleniumServer;
  public void setUp() throws Exception {
     
     RemoteControlConfiguration rc = new RemoteControlConfiguration();
          rc.setSingleWindow(false);
          seleniumServer = new SeleniumServer(rc);
          selenium = new DefaultSelenium("localhost", 4444, "*iexplore", "http://www.pdfonline.com/convert-pdf/");
          seleniumServer.start();
          selenium.start();
          }
   
public void testgoogling() throws IOException {
selenium.open("http://www.pdfonline.com/convert-pdf/");

Process proc = Runtime.getRuntime().exec("C:\\Documents and Settings\\nirkumar\\Desktop\\Browse.exe");
Process proc1 = Runtime.getRuntime().exec("C:\\Documents and Settings\\nirkumar\\Desktop\\Upload.exe");
pause(10000);
}
}



When browse button clicked it will open a choose file dialog box. then Process proc1 = Runtime.getRuntime().exec("C:\\Documents and Settings\\nirkumar\\Desktop\\Upload.exe"); line will be executed
and this will set the file path and click open. In the attached zip you will get one file "Upload.a3" in this file you have to set the path of your file which you want to upload
Local $file = "Your file location" this will set the path of your file and click on open button.


Note : This work with Internet explorer only.

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

How to upload a file in selenium with the help of AutoIT
Recently, I had the challenge of writing some automation for a work flow which included uploading a file because uploading 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.

Click on browse button on your web page and go to second step.If you are unable to click on browse button then visit below page.

How to click on browse button

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

WinActivate("Choose file");
Local $file = "c:\yourpath\howtoupload.doc"
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:\\Documents and Settings\\nirkumar\\Desktop\\Browse.exe");


Download AutoIT script
Download exe file of AutoIT script

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.
If it still is nor working then change the script like

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

How to click on browse button in selenium with the help of AutoIT

Recently, I had the challenge of writing some automation for a work flow which included uploading a file, and then downloading a file later in the work flow.
AutoIT
Autoit is a freeware windows testing tool. Autoit allows you accessing windows components similar to QTP. Basically once you have identified the id of the component on the screen you can use it by either clicking on it, typing in something. Using Autoit we can identify the internet explorer “Choose file” or Firefox

“File Upload” windows and enter the correct value to the input

field after which you will press the open button.

Step to click on browse button.
Step 1.
Download the AutoIT and intall it.

Download AutoIT

setp 2.
write a AutoIT script to click on browse button and save it.



#include <IE.au3>
; 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")



setp 3.
Compile AutoIT script and make exe file 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:\\Documents and Settings\\nirkumar\\Desktop\\Browse.exe");


This will click on browse button and open choose file or upload file dialog box.
Download AutoIT script
Download exe of that script

If you want to test this script works then download the exe and open the page which has browse button and run(double click on this exe) this exe file. i am sure this
will click on your browse button and open a dialog box. If it does not then definitely your browse button has some other name than "UploadedFile" then in the step 2 change the button name and make exe of that script and run it.

NOTE: This will work in Internet explorer.