Handling unexpected windows that don't affect TestComplete

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.

Getting ready

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).

How to do it...

To capture the Calculator Plus window appearing, it is necessary to perform the following actions:

  1. First of all, we shall create a function that will execute the needed actions (that is, check if the calculator is up and running, and close it, if so):
    function killCalc()
    {
     if(Sys.WaitProcess("CalcPlus", 1, 1).Exists)
     {
        Log.Message("Calculator has been found");
        Sys.Process("CalcPlus").Terminate();
     }
    }
  2. Let's now create the function which will execute some of the timed actions: we will place a timer at its beginning with successive invocation of the previously created 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);
      }
    }
  3. Launch the 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.

How it works...

With the help of the Utils.Timers.Add method, we are creating a new timer, in line with the following parameters:

  • The interval in terms of milliseconds within which the timer will fire up (in our case, 500 milliseconds)
  • The name of the function which is necessary to launch as the timer fires up (in our case, it is the Unit1.killCalc function)

    Note

    Please note that it is necessary to pass its complete name, that is, the name of the module and the name of the function, as dot-separated values.

  • A 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.36.38