Thursday 6 November 2014

Its Time for change - Get rid of au3 and exe file for using AutoIT

     In this series now I am going to dicuss about how to get rid of au3 files and exe files for using AutoIT. We all know how to use AutoIT along with automation code. Its lengthy and tedious process as we have to write AutoIT code and convert them into exe files and maintain all exe files that you use in your code. 
      Lets try an approach which makes this procedure easy.AutoIT allows for GUI automation using a very simple syntax and can be useful for testing Windows applications. It is packaged with AutoItX which supports accessing AutoIt functions through COM objects.AutoItX4Java uses JACOB to access AutoItX through COM and strives to provide a native Java interface while maintaining the simplicity of AutoIt. Getting started is simple.

Requirements: 
1. AutoIT DLL that supports JACOB protocol jacob-1.17-M2-x86.dll 
2. autoitx4java.jar which uses JACOB a Java bridge to run activex components of Autoit (jacob-1.17-M2-x86.dll) 
Both can be freely downloaded from the below links           
http://code.google.com/p/autoitx4java/downloads/list 
http://sourceforge.net/projects/jacob-project/ or use Autoit.dll  that gets downloaded together with AutoIT installation in windows machine. 
          
Steps used to set up: 
1. Add jacob.jar and autoitx4java.jar to your library path.      
2. Place the jacob-1.15-M4-x64.dll file in your library path.(Create a "lib" folder in project space and place jacob-1.15-M4-x64.dll in the    folder)
3. Start using AutoItX.

//sample code for the above approach
File file = new File("lib", "jacob-1.18-M2-x86.dll"); //path to the jacob dll
System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());
AutoItX x = new AutoItX();
String notepad = "Untitled - Notepad";
String testString = "this is a test.";
AutoITutils.startApp("C:\\Windows\\system32\\notepad.exe");
AutoITutils.waitforAppToLoad(notepad);
AutoITutils.setText(notepad, "Edit1", testString);
String strtemp=AutoITutils.getTextfromField(notepad, "Edit1");
x.winWaitActive(notepad);
x.send(testString);
System.out.println(x.winExists(notepad, testString));        
x.winClose(notepad, testString);
       

Wednesday 5 November 2014

Its Time for AutoIT - Parameterizing AutoIT Script through Java

                Many of us are aware that we can use AutoIT for handling windows based popups and Files uploading etc. Now Iam going to discuss about much more advanced usage of this AutoIT. When I was using this AutoIT for files uploading, I got scenario of uploading multiple files in different places. But if I use AutoIT script with the hard coded file path, I cannot use it for other files. Only way I got is to pass parameters to my AutoIT exe file while I run it. AutoIT provides us options for sending parameters from our java code.The below commands from the AutoIT will be used for doing that.

$CmdLineRaw - This command takes the whole text as a single parameter.
Lets see an example with the above command.

Below is the AutoIT script
#include <Constants.au3>
; This displays mesage box with the parameterized text
$text =$CmdLineRaw
MsgBox($MB_SYSTEMMODAL, "My First Script!", $text)

Java code looks like below:

String command="D:/Backup/Project/OwnWorkspace/AutoIT/notepad.exe This is parameterized test";
Runtime.getRuntime().exec(command);

Now the next question is how to multiple parameters.AutoIT provides option for this as well. Below commads are used for doing that.

$CmdLine[0] ; Contains the total number of items in the array.
$CmdLine[1] ; The first parameter.
$CmdLine[2] ; The second parameter.
...
$CmdLine[nth] ; The nth parameter e.g. 10 if the array contains 10 items.

Lets see an example with the above commands:

#include <Constants.au3>
; This displays given text in the notepad
$title =$CmdLine[1]
$text=$CmdLine[2]
Run("C:\Windows\notepad.exe")
Sleep(1000)
WinWaitActive($title)
WinFlash($title)
Sleep(3000)
Send($text)

Java Code:
String command="D:/Backup/Project/OwnWorkspace/AutoIT/notepad.exe \"Untitled - Notepad" \"Hi this is notepad\"";
Runtime.getRuntime().exec(command);

D:/Backup/Project/OwnWorkspace/AutoIT/notepad.exe--it is a path where you stored your .exe file which got converted from au3 file