Selenium with Autoit (no installation) for win UIs

 As most of us know, Autoit is a tool to automate basic navigations for non browser / windows applications.

There are situations in selenium automation, we get window dialog or we can say non browser related dialog which can not be automated with selenium. e.g Upload a file, download a file etc..

We will see how we can integrate Autoit with Selenium without installing Autoit.



Most of the sites / documents provide very nice explanation about usage of Autoit tool (basically they say to install the autoit and then we kind of record the navigations and create an exe file, which we call in our selenium programming)

While browsing for selenium topics, got a nice post of Joe Colantonio about autoit automation, with out installation.

so let's see how we can integrate autoit with selenium without installing.

The concept is to access Autoit through COM (Component object model, used for inter process communication) using Jacob  (Java-COM bridge), and register the AutoitX3.dll in to our machine.

Pre-requisites:

Download Autoit zip file,
https://www.autoitscript.com/site/autoit/downloads/

Extract to get the
AutoItX3.dll
AutoItX3_x64.dll
Note: Let's say, extracted the autoit zip file in C:\ drive, then the required files are under
C:\autoit-v3\install\AutoItX

Download Jacob zip file, 
http://sourceforge.net/projects/jacob-project/

extract to get the
jacob.jar
jacob-1.18-x64.dll  [for 64 bit JVM]
jacob-1.18-x86.dll  [for 32 bit JVM]

To know 32 or 64 bit of JVM on your machine, use following code in java


System.getProperty("sun.arch.data.model"); //this returns 32 or 64 but JVM

Create a folder (lib) under your current project folder, and copy jacob-1.18-x64.dll and jacob-1.18-x86.dll into the lib folder.  will use in the code...
Download AutoitXJava.jar file
https://code.google.com/p/autoitx4java/downloads/list

In Eclipse

Load below jar files into eclipse Java Build Path, by creating a new project / to an existing project:
jacob.jar | AutoitXJava.jar

Register AutoItX

For 32 bit OS:
Click on Windows -> Run.., enter: regsvr32 c:\autoit-v3\install\AutoItX/AutoItX3.dll
For 64 bit OS:
Click on Windows -> Run.., enter: regsvr32 c:\autoit-v3\install\AutoItX/AutoItX3_x64.dll

If above statement doesn't work, then Open cmd (Run as Administrator) and type above command
you should see

Scenario: we have a file, test.txt under c:\test folder, we need to upload to qavalidation.com demo site....
To identify the elements in the file upload dialog (non browser app), we need to use Au3Info.exe as spy tool




Let's jump to code implementation:

public class Test {
public static void main(String[] args) throws InterruptedException {
   String jacobDllVersionToUse;
   if (System.getProperty("sun.arch.data.model").contains("32")){
      jacobDllVersionToUse = "jacob-1.18-x86.dll";
   }
   else {
      jacobDllVersionToUse = "jacob-1.18-x64.dll";
   }
   File file = new File("lib", jacobDllVersionToUse);
   System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());
  
   WebDriver driver = new FirefoxDriver();
   driver.get("http://www.qavalidation.com/p/demo.html");
   driver.findElement(By.name("datafile")).click();
   Thread.sleep(1000);
 
   AutoItX x = new AutoItX();
   x.winActivate("File Upload"); //File Upload, title of windows UI
   x.ControlSetText("File Upload", "", "1148", "c:\\test\\test.txt"); //Textbox for path, id is 1148
   x.controlClick("File Upload", "", "1") ; //Open button, id is 1
  }
}



2 comments: