Understanding the window's life cycle

For the sake of code decomposition towards simplification, we create variables that correspond to the objects (processes, windows, and controls elements). In this recipe, we are about to deal with correct appropriation of such variables in TestComplete.

Getting ready

Add Calculator Plus to TestedApps (C:Program FilesMicrosoft Calculator PlusCalcPlus.exe).

How to do it...

In order to understand the life cycle of windows we will perform the following steps:

  1. Launch the following function to be executed:
    function testVariables()
    {
      TestedApps.CalcPlus.Run();
      var wCalc = Sys.Process("CalcPlus").Window("SciCalc", "Calculator Plus");
      wCalc.Activate();
      wCalc.Close();
      aqUtils.Delay(2000);
      TestedApps.CalcPlus.Run();
      wCalc.Activate();
    }
  2. In the result, we will get the error message The window was destroyed during method execution.

How it works...

We launch the calculator and create a wCalc variable corresponding to the main window of the calculator. Then we close the calculator and reopen it again in an attempt to activate the same using the pre-existing variable wCalc.

At first, everything should work without a hitch, because in both instances access to the main window takes place in seemingly the same way: the application has been launched and appears just like it did the first time; nonetheless, we are in receipt of the error message.

This is happening because the wCalc variable has been assigned to an object which is no longer in existence as of the moment the program was closed down with the help of the Close method. If a new object has the same name, it does not mean it is the same object. The variable wCalc still tries working with the older window, which has been closed by now, and will never reinstate. When the variable is being addressed, TestComplete does not recalculate the expression, which we assigned to the variable in the beginning.

If we attempt to use the variable wCalc again to work with the newer window, we need to initialize it again with the same expression as done initially:

wCalc = Sys.Process("CalcPlus").Window("SciCalc", "Calculator Plus");

For brevity's sake, the right-hand part of the expression can be made into a function, which returns the window object.

This example is quite a vivid one; however, in our tested applications, there may arise less obvious and underhanded situations. For example, the window remains intact, while the elements inside are being renewed. Such a peculiarity should always be kept in the back of your mind if you are using variables to handle screen (that is, display) objects.

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

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