Wednesday, 25 January 2017

Seamless Integration of Selenium + AutoIT

AutoIT is a freeware available to automate Windows GUI by simulating the combinations of mouse movements & key strokes. AutoIT has its own basic syntax, editor & control identification mechanism. However, to achieve the code level integration one can use 'AutoItX', packaged with AutoIT. This supports access to AutoIT functions using COM objects.


A library named 'AutoItX4Java' is the key, which uses JACOB to access AutoITX through COM and provide a native java interface for the ease of integration.
Follow the below steps to get started:


STEPS:


1. Download JACOB

2. Download & install AutoIT.

3. Copy 'jacob-1.18-x64.dll' or 'jacob-1.18-x86.dll' to the project library path based on the system.

4. Copy AutoItX3.dll & AutoItX3_x64.dll to library path.

5. Copy jacob.jar & autoitx4java.jar to the project library path

6. Start using AutoITX.


Example:

public void autoITSeleniumExample() throws InterruptedException{

/* Create ChromeDriver Instance */
System.setProperty("webdriver.chrome.driver", "pathtodriver/chromedriver.exe");
WebDriver cdrv = new ChromeDriver();
/* Load the correct JACOB dll */
String jvmVersion = System.getProperty("sun.arch.data.model");
File f = null;
if(jvmVersion.contains("32")){
f = new File("lib", "jacob-1.18-x86.dll");
}else{
f = new File("lib", "jacob-1.18-x64.dll");
}
System.setProperty(LibraryLoader.JACOB_DLL_PATH, f.getAbsolutePath());
/* Create AutoIT Instance */
AutoItX at = new AutoItX();
/* Open Google Images and upload a image from you local system */ 
cdrv.get("https://images.google.com/");
cdrv.findElement(By.id("qbi")).click();
cdrv.findElement(By.xpath("//a[contains(text(),'Upload an image')]")).click();
cdrv.findElement(By.id("qbfile")).click();
/* Switching to AutoIT */
Thread.sleep(3000);
/* Set Text in File editbox & click on open */
at.ControlSetText("Open", "", "1148", "test.html");
at.controlClick("Open", "", "1");
Thread.sleep(2000);
/* Switching to Selenium */
cdrv.quit();
}

POINTS TO NOTE:

1. Both AutoItX3 and Jacob dll's come in x86 and x64 versions. Ensure you use the correct dlls

2. If AutoIT is not installed, then register only AutoItX3.dll using below command to start using AutoITX.
Register the below DLL by opening the CMD as Administrator.
"regsvr32.exe AutoItX3.dll"
                 OR
"\Windows\SysWOW64\regsvr32.exe AutoItX3_x64.dll"

3. Always use small delay while switching from selenium to AutoIT & vice versa.

4. Use the AutoIT Spy ['Au3Info.exe'] to capture the control Id.


AutoIT Spy - Highlighting the Control Id



References:


https://github.com/accessrichard/autoitx4java
https://www.joecolantonio.com/2014/07/02/selenium-autoit-how-to-automate-non-browser-based-functionality/

2 comments:

  1. where are you supposed to get the autoitx4java.jar file from

    ReplyDelete
    Replies
    1. You can get the AutoItx4java.jar from the below location.
      https://github.com/accessrichard/autoitx4java

      Delete