Microsoft KB Archive/136209

= Microsoft Knowledge Base =

How to Use Notifications in TestBasic
Last reviewed: July 25, 1996

Article ID: Q136209

The information in this article applies to:


 * Microsoft Visual Test for Windows 95 and Windows NT, version 4.0
 * Microsoft Test for Windows, version 3.0a

SUMMARY
The Test language contains notification logic, so you can detect and respond to a variety of events. This article explains how to use the following notifications in Test scripts:


 * KeyStrokes
 * Window creation
 * Window destruction
 * System-level interrupts (unhandled exceptions)

Notification logic allows for notifications to be triggered by specific events (for example, creating a window with an exact title) or by general events (for example, destroying a window of any class). Notification handlers that define the TestBasic code that will execute upon notification may also be handled globally or locally, as determined by the scope of a procedure. Unique notifications, regardless of the type, defined in a test case file may be used in any combination. However, certain restrictions do apply and are discussed in this article.

MORE INFORMATION
Certain behaviors occur when using notifications of the same type in a test case file. For the following examples, all notifications are handled globally. The following notification calls define two different notification handlers for the same event:

'keypress notification for CTRL-Q - call the KeyPressHandler routine upon notification On KeyPress ("Q", FCONTROL) Call KeyPressHandler

'keypress notification for CTRL-Q - call the KeyPressHandler1 routine upon notification On KeyPress ("Q", FCONTROL) Call KeyPressHandler1 In this case, KeyPressHandler1 will override KeyPressHandler and will be the only notification handler executed. With the exception of the KeyPress notification, no other notification can be interrupted. Now, look at this example:

On KeyPress ("C", FCONTROL) Call KeyPressHandler On KeyPress ("Q", FCONTROL) Call KeyPressHandler1

'Notification handler code to be executed upon notification when CTRL-C is detected Sub KeyPressHandler(vtNofityData as Variant) Print "keypress handler" 'cause a second detection by issuing a CTRL-Q 'KeyPressHandler1 will then be triggered Play "^(q)" End Sub Sub KeyPressHandler1(vtNofityData as Variant) Print "keypress1 handler" End Sub

'Main script Play "^(c)" In this case, KeyPressHandler and KeyPressHandler1 are both triggered. For other notification types, such as WindowCreate, the second handler would not be executed.

Steps to Demonstrate Notifications
  Create an executable to generate an Unhandled Exception (UE). For the UnhandledException notification, you need to cause a UE. An external exception is required because Visual Test internally traps the UE for you. Therefore, you can either use an application that will cause a UE, or use the following code written in the C programming language. This code must be compiled into an executable file by using a product such as Microsoft Visual C++. void main (void) {   int *p; p = 0; *p = 1; }   Enter the following code into a new test case file: '$include 'declares'

'declare Window handle variable Dim hWnd as long 'declare and set Test environment Window handle variable 'for purposes of controlling focus during script execution Dim TestWindow as long TestWindow = GetHandle(GH_HWNDCLIENT)

'turn KeyPress notification on - execute KeyPressHandler routine when 'CTRL-C is detected On KeyPress ("C", FCONTROL) Call KeyPressHandler

'turn KeyPress notification on - execute KeyPressHandler routine when 'CTRL-Q is detected On KeyPress ("Q", FCONTROL) Call KeyPressHandler1

'turn UE notification on - execute UHHandler routine when UE is detected On UnHandledException Call UHHandler

'Notification handler subroutine definitions Sub KeyPressHandler(vtNofityData as Variant) Print "keypress handler" End Sub

Sub KeyPressHandler1(vtNofityData as Variant) Print "keypress1 handler" End Sub

Sub WindowCreateHandler(vtNofityData as Variant) Print GetText(vtNotifyData) Print "Windowcreate handler" End Sub Sub WindowDestroyHandler(vtNofityData as Variant) Print "Windowdestroy handler" End Sub Sub UHHandler(vtNofityData as Variant) Print "Exception handler" Stop End End Sub

'turn WindowCreate notification on - execute handler 'WindowCreateHandler upon notification On WindowCreate ("+Notepad",NULL) Call WindowCreateHandler

Run "notepad", NoWait hWnd = WGetFocus

'turn WindowDestroy notification on - execute WindowDestroyHandler 'upon notification WindowDestroy will be passed the Notepad Window 'handler On WindowDestroy(hWnd) Call WindowDestroyHandler

Sleep 2 'generate key strokes to trigger KeyPress notification handler Play "^(c)" Play "^(q)" Sleep 1

'Exit Notepad and set focus back to Test WMenuSelect("&File\E&xit") WSetActWnd(TestWindow)

StatusBox "You Have 5 Minutes to Cause an UE or You Can" + _ " Close the Program by Selecting Break from the Test Menu Twice" Sleep 1

For x% = 1 to 60 sleep 5 Beep StatusBox "Cause an UE or Close the Program by Selecting Break " _ + "from the Test Menu Twice ... Timeout " + STR$(4-x \ 12) _ + " min.," + TRIM$(STR$(60 - (x * 5) MOD 60)) + " sec." Next  Run the program. The various notification messages are printed to the Viewport.