My team is working on a project using Behavior Driven Development (BDD) and Java. Our main application is browser-based, so we’re also using the Selenium WebDriver with Java; however, our application also has other thick client applications integrated into it.

Most of our automation efforts are fine when using Selenium with BDD, but issues arise whenever we need to perform actions against the thick client applications when testing certain workflows. What to do?

The best solution I’ve found around these issues is to use AutoIt, a freeware application designed to help automate Windows-based UIs. The normal procedure is to create AutoIt scripts using the built-in BASIC-like programming language, but that isn’t an optimal solution for my team.

Instead, to still be able to develop in Java in our normal IDE we are accessing AutoIt through COM using Jacob, which is a Java COM bridge.

Also — rather than have everyone that needs to run the tests install another product — AutoIt– we can get around it simply by registering the AutoItX3 DLL.

This might sound complicated, but following the steps below should get you up and running in no time.

The Selenium AutoIt Solution

First you need to configure your machine:



http://sourceforge.net/projects/jacob-project/

http://www.autoitscript.com/site/autoit/downloads/


https://code.google.com/p/autoitx4java/downloads/list



Now — so that we can use AutoIt without installing Autoit we need to register the AutoIT dll that we placed in our tools/autoit directory:

If you are using Java 32bit:


If you are using Java 64bit (if you don’t do this, you will get the error Can’t co-create object):



Use the Calculator to add two numbers

For a quick sanity check to verify that you can use ActiveIT in Java, let’s create a small test that calls the Windows calculator and adds two numbers:


import java.io.File;
import autoitx4java.AutoItX;
import com.jacob.com.LibraryLoader;

First, you’ll need to determine whether the machine you’re running this on has Java32 bit or 64bit installed. (If you know this already you won’t need this code, but if you want to run on multiple machines it might make sense for you to programmatically determine the Java bit version and load up the corresponding Jacob.dll version.)

To do this, add a method under the static void main section:

/**
*
* Returns if the JVM is 32 or 64 bit version
*/
public
static String jvmBitVersion(){
 return System.getProperty("sun.arch.data.model");
}
public static void main(String[] args) throws InterruptedException {
String jacobDllVersionToUse;
if (jvmBitVersion().contains("32")){
jacobDllVersionToUse = "jacob-1.18-M2-x86.dll"
}
else {
jacobDllVersionToUse = "jacob-1.18-M2-x64.dll";
}

File file = new File("lib", jacobDllVersionToUse);
System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());

AutoItX x = new AutoItX();
x.run("calc.exe");
x.winActivate("Calculator");
x.winWaitActive("Calculator");
//Enter 3
x.controlClick("Calculator", "", "133") ;
Thread.sleep(1000);
//Enter +
x.controlClick("Calculator", "", "93") ;
Thread.sleep(1000);
//Enter 3
x.controlClick("Calculator", "", "133") ;
Thread.sleep(1000);
//Enter =
x.controlClick("Calculator", "", "121") ;
}



More Automation Awesomeness with AutoIt

How cool is that?! Stay tuned for my next post where I will show you how to use this same technique to handle non-browser based dialogs that occasionally occur when testing web applications.

Another option to check it besides AutoIt to help with these types of issues is SikuliX. Check out my post on How to Get Started with SikuliX to learn more.