I am currently working on an automated system test engine for a project developing a medical device. The test engine runs on a PC and can connect to the device through some network connection. In the device we have implemented a very simple command line application that allows us to control the device from the test engine. We can send a command like "key Accept" to the device which will act like the user had pressed the Accept-key.
To make automated system testing more manageable we thought about extending this small scripting language with language constructs like loops and subrutines. But we did not want to implement an entire scripting language from scratch. Instead it would be much easier if we could use an existing scripting language, like JavaScript which already contains these language constructs.
It turned out to be quite easy to add scripting to our .NET-based test engine, using the Microsoft Script Control. Now we can write a test scripts in JavaScript like the following:
// Test pressing a key 10 times
var i;
for(i = 0; i < 10; ++i)
{
device.key("a");
}
The "device"-object exists inside the test engine and is written as a .NET class:
public class DeviceProxy
{
public void key(string key)
{
sendCommand("key " + key);
Console.WriteLine("Pressing '{0}'", key);
}
}
An instance of this class is added to the Script Control at startup:
ScriptControl sc = new ScriptControl();
DeviceProxy deviceProxy = new DeviceProxy();
sc.Language = "JScript";
sc.AddObject("device", deviceProxy, true);
sc.AddCode(File.ReadAllText(scriptPath));
You also need to please the COM-to-.NET interop by setting ComVisible to
True in AssemblyInfo.cs. Take a look at the
sample project.
By Lars Thorup