SetTrap Method

Class

AnyWin.

Action

Installs a trap to detect when the window comes into existence.

Availability

This functionality is supported only if you are using the Classic Agent.

Syntax

window.SetTrap (ButtonTag [, iExceptNum, sExceptData])
Variable Description
ButtonTag The label of the pushbutton to press when the trapped window appears. STRING.
iExceptNum Optional: The number of the exception to raise when the window exists. INTEGER.
sExceptData Optional: The message to report when the exception is raised. STRING.

Notes

  • SetTrap checks for the existence of the specified windows at each 4Test statement that involves a call to the Agent. Statements not included are function calls, SYS_* calls, and any flow control statements (if, while, do/except, and so on).

  • This method is equivalent to Exists, except that it is perpetual; that is, the trap triggers in those instances when Exists would return TRUE.

  • When a trap condition is detected (that is, the window appears), Silk Test Classic presses the pushbutton specified by ButtonTag. If you specify iExceptNum, SetTrap also raises the specified exception. To raise an exception without pressing a button, specify an empty tag ("") for ButtonTag.

  • This function is useful for responding automatically to message boxes such as "Save changes?" or to dialogs from a network. You can set traps for any number of windows.

Limitation

SetTrap is very timing dependent. It has to catch the window as it is opening. If SetTrap is triggered out of step with the appearance of its assigned window, then SetTrap does not trap it. Once SetTrap misses the opening of the window, it will not trap it at the next statement that triggers it because the window is already established.

Recommendation

SetTrap allocates memory, and so should be followed by a call to ClearTrap as soon as the window is found. Clearing traps is essential for maintaining performance at a useable level. We recommend that you set a trap only once per script, or alternatively, that you define your own ScriptEnter function that sets the trap on a script-level basis.

Windows

Use the tag /~/$SystemModal to trap system modal dialogs (if they have a caption). For example:

DialogBox("/~/$SystemModal").SetTrap ("Cancel")

Example

The following example shows when SetTrap performs its check for the existence of a window.

[ ] MessageBox.SetTrap ("Yes")
[ ] MyWin.SetActive()  // SetTrap checks
[-] if (NOSHOESHINE)   // no check
	[ ] MyWin.MyButton.Click()  //SetTrap checks
[-] while (BOGUS)   // no check
	[ ] exit     // no check
[ ] Agent.GetOption()  // SetTrap checks