The Lazy Tester

Using AutoIt in scripting

June 9th, 2009

Sometimes you just need to do a little quick automation of a third-party program, and don’t care about testing anything on it. If you’re not using a tool that can handle the program, you can often use AutoIt to do what you need to do and move on with your test.

AutoIt comes with a COM DLL called AutoItX that can be called by just about anything. All you have to do is install the full package and call functions on the COM object. Nearly all functions available in a normal AutoIt script are also available as functions on the object.

Here’s a simple example of a Ruby script using AutoItX. Using AutoItX with Ruby might be useful if you’re using WatiR, and run into an ActiveX control or Java applet.

require 'win32ole'

autoit = WIN32OLE.new('AutoItX3.Control')

if autoit.WinExists("Untitled - Notepad") == 1
	autoit.WinActivate("Untitled - Notepad")
	autoit.Send("Hello world!")
end

Here’s the same example with C#. You have to add a reference to the AutoItX dll first.

using AutoItX3Lib;
/* Other usings and application code */

public void HelloWorld()
{
	AutoItX3Class automator = new AutoItX3Class();

	if (automator.WinExists("Untitled - Notepad", "") == 1)
	{
		automator.WinActivate("Untitled - Notepad", "");
		automator.Send("Hello world!", 0);
	}
}