Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WTL

ScriptRunner Application

4.97/5 (18 votes)
25 Mar 2007CPOL4 min read 1   1.9K  
An Introduction to ScriptRunner. A scripting tool for user interface Unit Testing.
Screenshot - ScriptRunner.jpg

Introduction

One of the most sought after skills for software professionals today is not only the ability to write good quality code but also the ability to test and validate your code. This is where this tool comes into play. ScriptRunner is different from other unit testing Frameworks. In fact, ScriptRunner is a complement of a traditional unit testing Framework like CPP Unit. A Unit Testing Framework lets you test modules in your application. A module can be a library, a class (or an object) or can be a simple function. ScriptRunner complements unit testing by emulating a user interaction with your software and can display any error in the execution of your program.

Description

Initially, ScriptRunner was created with the intent to simulate a user interaction with an application. But in the end, this tool has evolved. Now, it provides a simple TRACE library to capture the application state and of course, to report any error in your program.
Typically with CPP Unit (or other Framework), you write the code to test your function passing both correct and error values to verify whether it behaves correctly. These tools are great and if you choose to test enough important functions, you can get very good coverage and find bugs (or broken code) easily in your program.

But one aspect that is lacking, is how do you test the User Interface to achieve the same result? ScriptRunner can help you do that! You can write a simple script to interact with your application and use the ScriptRunner TRACE library to display any success or failure.

How Does it Work?

ScriptRunner is a Scripting Host application. It provides a set of interfaces to execute an application, manipulate a window and submit input to your user interface as a user would normally do. Several commands are available to configure and run a test script. I am assuming everyone will recognize the standard icons and know what they are used for. Now, let me describe the other icons to get you started.

ScriptRunner Command

CommandDescription
TraceYou can use the Trace button to capture the trace statement (OutputDebugString) directly to this window. Your code can send output strings directly to this window by using the ScriptRunner TRACE library. A script can also send an output by using ScriptHost.DebugOutput function.
StopStop Trace will stop the capture of the debugger output.
CompareCompare mode can be used to verify the outputs being sent when your program executes.
RunRun command will execute a User Interface JavaScript Testing Unit

Several additional features are available to filter your program output. Context menu options are also available to load and save your program output settings.

ScriptRunner ScriptHost API

FunctionDescription
Display(text)Display a message box
DebugOutput(text)Send output text to script runner output Window
FindWindow(class,title)Search for a specific Window class and title (optional).
SendKey(keyCode,ctrlKey,altKey)Send a virtual Keycode to an active Window (control with focus). Optionally, simulate CTRL and ALT keys being pressed.
SendKeys(text)Send keystrokes (User emulation).
Sleep(delay)Wait for specified milliseconds
bool LaunchApp(appPath)Run an application
string GetEnvString(sName)Get an environment variable string

ScriptRunner WindowDispatch API

FunctionDescription
WindowProperty to get Window handle. This should be used as read-only since ScriptRunner is responsible for creating and initializing this object.
string GetWindowText()Get current caption text or edit text for edit control
int GetWindowTextLength()Get current caption text (or edit text) length
bool SetWindowText(text)Change Window text
SetFocus()Change focus to Window
ShowWindow(cmdShow)Show/Hides a Window
Window GetDlgItem(dlgItem)Get a child Window handle
ChildWindowFromPoint(x,y)Get a child Window handle from client coordinates
MoveWindow(x,y,w,h)Resize and reposition a Window
SetWindowPos(x,y,cx,cy,flg)Resize and reposition a window
Window FindWindowEx(class,title,prev)Find a child Window based on class or title
bool SetForegroundWindow()Set Window to foreground
Window GetParent()Get current Window parent
MouseHover()Move and Center mouse over Window object
MouseLClick(x,y,clickNow)Move mouse to Window client position, optionally simulate left click at the end of movement.
MouseRClick(x,y,clickNow)Move mouse to Window client position, optionally simulate right click at the end of movement.

ScriptRunner Example

ScriptRunner exposes only one event handler OnStarted. It is the recommended main entry point to run your script. In JavaScript, use this prototype function ScriptHost::OnStarted().

Example:

JavaScript
function ScriptHost::OnStarted()
{
  LaunchApp("C:\\Windows\\System32\\Notepad.exe");
  Sleep(500);
  var winObj = FindWindow("Notepad", "Untitled - Notepad");
  if (winObj)
  {
    winObj.SetForegroundWindow();
    winObj.SetFocus();
    SendKey(0x74); // F5
    SendKeys("Hello CodeProject gurus around the world!\r");
    SendKeys("1234567890-=\r");
    SendKeys("!@#$%^&*()_+\r");
    SendKeys("abcdefghijklmnopqrstuvwxyz\r");
    SendKeys("ABCDEFGHIJKLMNOPQRSTUVWXYZ\r");
  }
  else
    Display("Untitled - Notepad window was not found...");
}

Unit Testing Output to ScriptRunner

As stated before, a C/C++ application can send output to ScriptRunner output window. For this end, you will have to include a set of files in your solution.

  • CTracer.h : This is used to hook OutputDebugString calls from your program.
  • SocketHandle.h, cpp: Socket API used to communicate with ScriptRunner.
  • HookImportFunction.h, cpp : PJ Naughter's Hook Import function (Click here)(Copyright © 1999; PJ Naughter)

Project Info

This tool uses:

History

  • March 25, 2007 - Public release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)