.Net tutorials: AutoIt with step
This tutorial explains how to automate interaction with Windows applications using the AutoIt C# binding. This example is extract from our samples repository available on github.
Visual Studio settings
First of all, you need to configure a new C# project as described in the .Net plugin page. The main points will be for you to:
- Choose the proper .Net version .Net Framework 5.0
- Install the StepApiFunction, StepApiKeyword and StepApiReporting nuget packages
You will then need to install the AutoItX.DotNet and the NUint packages. You dependencies should then look like:
Code
You can then create your first AutoIt keyword by copying the following code:
public class Keywords : AbstractKeyword
{
[Keyword(name = "Open Notepad, edit and close")]
public void EditInNotepad()
{
int pid = 0;
if ((pid = AutoItX.Run("notepad.exe", ".")) == 0)
{
output.setError("Error stating Notepad");
return;
}
if (AutoItX.WinWaitActive("Untitled", timeout: 10) != 1)
{
output.setError("Error waiting for the Notepad window");
return;
}
AutoItX.Send("I'm in notepad");
if (AutoItX.WinKill("*Untitled") != 1)
{
output.setError("Error closing the Notepad window");
return;
}
if (AutoItX.WinWaitClose("*Untitled", timeout: 10) != 1)
{
output.setError("Error waiting for the Notepad window to close");
return;
}
if (AutoItX.ProcessWaitClose(pid.ToString(), timeout: 10) != 1)
{
output.setError("Error waiting for the Notepad process to close");
return;
}
}
}
Two points are important to note in this class:
- The function EditInNotepad contains the AutoIt specific code. This example start and interact with a notepad
- The keyword class extends StepApi.AbstractKeyword. This class provides the different objects for interacting with your Plans (input, output, session, properties and the onError function).
Unit Test
If you added the ScriptDev package to your project, you can copy this example to test your keyword in local:
public class AutoItKeywordsTests
{
ScriptRunner runner;
[SetUp]
public void Init()
{
runner = new ScriptRunner(typeof(Keywords));
}
[TearDown]
public void tearDown()
{
runner.close();
}
[TestCase()]
public void AutoItTest()
{
var output = runner.run("Open Notepad, edit and close", @"{}");
Assert.Null(output.error);
}
}
Keyword configuration
Finally, before running your example on a .Net agent, we need to prepare a ZIP file containing the needed dependencies:
Note that this ZIP file contains the nunit.framework.dll (due to the unit test we added) and the .pdb of the keyword, so we can have the line number of our code in case of error.
We then just have to upload the dll and the dependencies to the controller: