Friday 12 July 2013

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 filethen 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.

No comments:

Post a Comment

Angular JS Protractor Installation process - Tutorial Part 1

                     Protractor, formally known as E2E testing framework, is an open source functional automation framework designed spe...