Monday 8 June 2015

Java and AutoIt - Automating the calculator application using autoitx4java

autoitx4java is useful for testing the Windows applications using the AutoIt.

For more information on 'autoitx4java' check the url https://code.google.com/p/autoitx4java/

1. Download 'AutoItX4Java.jar'.
https://code.google.com/p/autoitx4java/downloads/list

2. Copy the 'AutoItX4Java.jar' file to the 'lib' folder of the project(i.e. Eclipse IDE Project).

3. Download JACOB.


4. Extract the 'jacob-1.18-M2.zip' file.

5. Copy the below file in the 'lib' folder of the project.





7. Extract the 'autoit-v3.zip' file.

8. AutoIt Inspector - for inspecting the UI elements.


9.Set the java build path for 
- AutoItX4Java.jar
- jacob.jar



10. Create a java class.


11. Execute the program(Sample Program is added in the end of the page).

Note: Unable to execute the program, since AutoIt is not registered in the windows registry

Below steps guides you in adding AutoIt to the windows registry.
12. Open the 'CommandPrompt' as 'Administrator'



13. After extracting the 'autoit-v3.zip' file, navigate to the folder 'autoit-v3\install\AutoItX' in the extracted zip file.

Navigate to the 'autoit-v3\install\AutoItX'(i.e. extracted 'autoit-v3.zip') in the command prompt.

14. Add the 'AutoIt' to the registry.
If you are using 'Java 32bit', run the below command in the 'Command Prompt':
regsvr32.exe AutoItX3.dll


If you are using 'Java 64bit', run the below command in the 'Command Prompt':
regsvr32.exe AutoItX3_x64.dll


15.Execute the program again(Sample Program is added in the end of the page), after setting the AutoIt in windows registry.



you can download the below sample code from the repository, click link AutoIt Proj.zip to download.

Sample Program:
package autoit;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import autoitx4java.AutoItX;

import com.jacob.com.LibraryLoader;
public class CalculatorTest {
//Choose the 'JACOB' dll based on the JVM bit version.
final String JACOB_DLL_TO_USE = System.getProperty("sun.arch.data.model").contains("32") ?
 "jacob-1.18-M2-x86.dll" : "jacob-1.18-M2-x64.dll";

final String APPLICATION_TITLE = "Calculator";
final String APPLICATION = "calc.exe";
private AutoItX control;

private Map<Integer,String> calcNumPad_ObjectRepository;
private Map<String,String> calcOperPad_ObjectRepository;
{
//Object Repository for Calculator Numbers
calcNumPad_ObjectRepository = new HashMap<Integer,String>();
calcNumPad_ObjectRepository.put(0, "130");
calcNumPad_ObjectRepository.put(1, "131");
calcNumPad_ObjectRepository.put(2, "132");
calcNumPad_ObjectRepository.put(3, "133");
calcNumPad_ObjectRepository.put(4, "134");
calcNumPad_ObjectRepository.put(5, "135");
calcNumPad_ObjectRepository.put(6, "136");
calcNumPad_ObjectRepository.put(7, "137");
calcNumPad_ObjectRepository.put(8, "138");
calcNumPad_ObjectRepository.put(9, "139");

//Object Repository for Calculator Operators
calcOperPad_ObjectRepository = new HashMap<String,String>();
calcOperPad_ObjectRepository.put("+", "93");
calcOperPad_ObjectRepository.put("-", "94");
calcOperPad_ObjectRepository.put("*", "92");
calcOperPad_ObjectRepository.put("/", "91");
calcOperPad_ObjectRepository.put("=", "121");
calcOperPad_ObjectRepository.put("clear", "81");
//Load the jacob dll.
File file = new File(System.getProperty("user.dir")+"\\lib", JACOB_DLL_TO_USE);
System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());
control = new AutoItX();
}
public static void main(String[] args) throws InterruptedException {
CalculatorTest ct = new CalculatorTest();
//Launch 'Calculator' application.
ct.control.run("calc.exe");
ct.control.winActivate("Calculator");
ct.control.winWaitActive("Calculator");

//Perform Addition
System.out.println("Addition of 1,3 - Actual Results: "+ct.add(1,3)+",  Expected Results: 4");
System.out.println("Addition of 17,3 - Actual Results: "+ct.add(17,3)+",  Expected Results: 20");

//Perform Subtraction
System.out.println("Subtraction of 5,1 - Actual Results: "+ct.subtraction(5,1)+",  Expected Results: 4");
System.out.println("Subtraction of 90,7 - Actual Results: "+ct.subtraction(90,7)+",  Expected Results: 83");
//Perform Multiplication
System.out.println("Multiplication of 8,2 - Actual Results: "+ct.multiplication(8,2)+",  Expected Results: 16");
System.out.println("Multiplication of 15,4 - Actual Results: "+ct.multiplication(15,4)+",  Expected Results: 60");
//Perform Division
System.out.println("Division of 9,3 - Actual Results: "+ct.division(9,3)+",  Expected Results: 3");
System.out.println("Division of 100,2 - Actual Results: "+ct.division(100,2)+",  Expected Results: 50");
//Close 'Calculator' application.
ct.control.winClose("Calculator");
}
//Perform 'Addition'.
public int add(int a, int b) throws InterruptedException{
performOperation("clear");
clickNumber(a);
performOperation("+");
clickNumber(b);
performOperation("=");
return Integer.parseInt(getResults().trim());
}
//Perform 'Subtraction'.
public int subtraction(int a, int b) throws InterruptedException{
performOperation("clear");
clickNumber(a);
performOperation("-");
clickNumber(b);
performOperation("=");

return Integer.parseInt(getResults().trim());
}
//Perform 'Multiplication'.
public int multiplication(int a, int b) throws InterruptedException{
performOperation("clear");
clickNumber(a);
performOperation("*");
clickNumber(b);
performOperation("=");

return Integer.parseInt(getResults().trim());
}

//Perform 'Division'.
public int division(int a,int b) throws NumberFormatException, InterruptedException{
performOperation("clear");
clickNumber(a);
performOperation("/");
clickNumber(b);
performOperation("=");
return Integer.parseInt(getResults().trim());
}
//Fetch the results after performing the operations
private String getResults() throws InterruptedException{
Thread.sleep(1000);
return control.winGetText(APPLICATION_TITLE);
}
//Click the Number in the calculator application.
private void clickNumber(int number) throws InterruptedException{

String sNumber = String.valueOf(number);
for(int i = 0; i < sNumber.length(); i++) {
   control.controlClick(APPLICATION_TITLE, "", calcNumPad_ObjectRepository.get(Character.digit(sNumber.charAt(i), 10)));
   Thread.sleep(1000);
}
}
//Perform operations.
private void performOperation(String controlID) throws InterruptedException{
control.controlClick(APPLICATION_TITLE, "", calcOperPad_ObjectRepository.get(controlID));
Thread.sleep(1000);
}
}

Troubleshooting: 
Make sure you are using the correct version of Jacob and AutoItX3 dlls. Since both come in x86 and x64 versions.

Related Links

Automating Calculator application using Java and Sikuli.
http://automation-home.blogspot.in/2014/09/AutomateCalcAppWithJavaAndSikuli.html

Java and Twin Automation Tool - Automating windows calculator application.
http://automation-home.blogspot.in/2015/12/java-twin-automation-tool-automate-windows-calc-application.html

1 comment:

  1. very good post. i have one question is there any way we can bring AutoIT includes like #include to java.

    _ScreenCapture_CaptureWnd

    i want to use this in java

    ReplyDelete