Thursday 4 December 2014

Integrating AutoIt with Selenium


What is AutoIt:

       AutoIt is freeware scripting language for automating the windows GUI.

      
Why is AutoIt:
       When you start working with web-application so many time you will get window based pop-up like- file upload, download pop up, window authentication etc. In this case Selenium fails and will not be able to handle desktop elements to avoid this we will use AutiIt script that will handle desktop or windows elements and will combine AutoIt script with our Selenium code.

Setup / Configuration:
Download and Install AutoIt software and Editor here



Install the software.

IMAGE 2:

Search for “sciTE Script Editor” or “SciTE.exe” in RUN dialog.


Problem statement:

Automate file upload feature in http://www.dropzonejs.com/

We know how to write selenium code until we click upload button and here is the code.

public static void main(String[] args) throws InterruptedException, IOException {
             
              String baseUrl = "http://www.dropzonejs.com/";
              driver = new FirefoxDriver();
              driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
              driver.get(baseUrl);
              driver.manage().window().maximize();
              Thread.sleep(6000);             
              driver.findElement(By.id("demo-upload")).click();
              Thread.sleep(3000);
       }

Now, we need to select a file from windows explorer and Selenium cannot do it for obvious reasons.

But, we can achieve this using AutoIt.

Here is the AutoIt script:

; Wait 10 seconds for the Upload window to appear
  WinWait("[CLASS:#32770]","",10)

; Set input focus to the edit control of Upload window using the handle returned by WinWait
  ControlFocus("File Upload","","Edit1")
  Sleep(2000)

; Set the File name text on the Edit field
  ControlSetText("File Upload","","Edit1","C:\Users\testUser\Downloads\A.jpg")
  Sleep(2000)

; Click on the Open button
  ControlClick("File Upload", "","Button1");


Explanation:
WinWait pauses execution of the script until the requested window existed
Now, we need to understand how to locate the file upload windows explorer.
Open      Au3Info.exe (Window Info tool), select Finder tool and point to file upload windows explorer to locate it.

We have two parts in ‘Window Info tool’, one is ‘Basic window Info’ and other is ‘Basic Control Info’.

As we are dealing with windows explorer wait, lets concentrate on ‘Basic window info’, notice about CLASS id to locate the window as ([CLASS:#32770])

Similarly we can locate each and every windows explorer element.

Few Methods to remember:

ControlFocus Sets input focus to a given control on a window.
ControlSetText Sets text of a control.
ControlClick  Sends mouse click command to a given control.

SAVE the file:

Save AutoIt script file as with ‘.au3’ extension.  Ex: one.au3


Compiling au3 script file:

Right click on one.au3 file and select compile script option.
A new file named one.exe will be generated.

Integrating AutoIt

with Selenium:

public static void main(String[] args) throws InterruptedException, IOException {
             
              String baseUrl = "http://www.dropzonejs.com/";

              driver = new FirefoxDriver();
              driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
              driver.get(baseUrl);
              driver.manage().window().maximize();
              Thread.sleep(6000);
             
              driver.findElement(By.id("demo-upload")).click();
              Thread.sleep(3000);
             
              Runtime.getRuntime().exec("E:/one.exe");       

       }     


Just run the script, that’s all.


NOTE: We do not need to add any jars to the project for this to run.