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.

23 comments:

  1. Excelentto !!!
    Great Job

    ReplyDelete
  2. Process proc = Runtime.getRuntime().exec("C:\\Documents and Settings\\nirkumar\\Desktop\\Browse.exe");


    How to Call that exe in selenium for C# language the example you gave its for java I think

    Please help!!!

    ReplyDelete
  3. the example which u gave is executed but it is not opening the file upload windows popup.can u please help on this

    ReplyDelete
  4. script written above looks more or less like IE oriented.can we tried the same for Firefox as well.please assist.

    thanks in advance.

    ReplyDelete
  5. Any comment on this regards is appreciated.Can you mention the Autoit script which does work in Firefox or Chrome browser?

    ReplyDelete
  6. I don't even know how I ended up here, but I thought this post was great. I don't know
    who you are but certainly you're going to a famous blogger if you aren't already ;)
    Cheers!

    Also visit my page; vakantiehuizen frankrijk

    ReplyDelete
  7. Hi,
    Can you please tell me how to click on a browse button in jscript pop-up.
    In our project we have a pop-up window under which we need to click on browse and then upload the required file.I am using Selenium RC with java.

    ReplyDelete
  8. Hey very interesting blog!

    Also visit my website: Valium

    ReplyDelete
  9. what if the file to upload is dynamic

    ReplyDelete
  10. obviously like your web site but you have to check the spelling on several of your posts.
    A number of them are rife with spelling problems and I to find it very bothersome to inform the truth nevertheless I'll certainly come back again.

    Also visit my web blog :: Gerardo

    ReplyDelete
  11. You can play in excess of just the free macbook air games within this laptop; you can play the most used ones, that require the ideal of graphics too.
    How do I repair the broken registry entries of Sims 3 Late Night.

    In addition, The Sims 3 Ambitions will allow you to change the look and feel
    of your Sims' town by becoming an architect, which enables you to directly affect your Sims' neighborhood with the Build and Buy
    mode.

    My site: Download Sims 3

    ReplyDelete
  12. Ηey there! Wօuld уou mind if I share your blog
    with my twіtter group? There's a lot of folks that I think
    would гeallу enjoy yߋսr content. Please let me know.
    Thank үou

    Fеel free tߋ visit my website: télécharger winrar

    ReplyDelete
  13. I like it when individuals come together and
    share opinions. Great site, stick with it!

    Here is my web blog: The Btv Solo

    ReplyDelete
  14. Excellent way of explaining, and pleasant article to obtain data regarding my presentation subject
    matter, which i am going to present in school.

    Feel free to visit my web blog - benuccia

    ReplyDelete
  15. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium.
    Selenium

    ReplyDelete
  16. Thanks for sharing such a useful post with us.Selenium is most as used automation tool to test web application and browser. This automation tool offers precise and complete information about a software application or environment.


    Selenium Training in Bangalore

    ReplyDelete
  17. Excellent post. I Have learn lots of new information about selenium from your blog post. keep update. thank you...
    Software Testing Training in Chennai | Selenium Training in Chennai

    ReplyDelete
  18. Nice information thanks for sharing.
    qtp training

    ReplyDelete