In some cases, it is necessary to capture appearance of a window which doesn't affect TestComplete, for example, a non-modal window that has been launched by another process. In this case, handling the OnUnexpectedWindow
event will be of little use for us.
In this recipe, we will deal with an example of working with such a window. Our script will be working with the Notepad application, meanwhile closing the calculator window, if it has showed up.
Launch the standard Windows Notepad (C:Windows
otepad.exe
) application and make sure that you can quickly launch Calculator Plus (for example, with the help of a shortcut or a given key's combination).
To capture the Calculator Plus window appearing, it is necessary to perform the following actions:
function killCalc() { if(Sys.WaitProcess("CalcPlus", 1, 1).Exists) { Log.Message("Calculator has been found"); Sys.Process("CalcPlus").Terminate(); } }
killCalc
function:function testUnexpectedWindowHandling2() { Utils.Timers.Add(500, "Unit1.killCalc", true); var np = Sys.Process("NOTEPAD").Window("Notepad"); np.Activate(); for(var i = 1; i < 1000; i++) { np.Keys(i + "[Enter]"); aqUtils.Delay(500); } }
testUnexpectedWindowHandling2
function. During its execution time, launch the Calculator Plus once in a while.You will see that each time the Calculator Plus is closed by TestComplete, the log is receiving the Calculator has been found message.
With the help of the Utils.Timers.Add
method, we are creating a new timer, in line with the following parameters:
interval
in terms of milliseconds within which the timer will fire up (in our case, 500 milliseconds)name
of the function which is necessary to launch as the timer fires up (in our case, it is the Unit1.killCalc
function)Boolean
to indicate if the timer should be on or off at its creation (in our case, this parameter is set to true
, that is, the timer is created and immediately turned on)Now, within the equal intervals, preset by the Interval
parameter, any predefined and passed function will fire up. In our case, this function checks if the calculator has been launched, if so the function in view closes the calculator down.
18.217.107.229