Waiting for an object to appear

Sometimes, there arises a need to pause script execution until a certain window shows up. It usually happens when scripts expect an application to work faster than it actually does.

For example, this would be the case, when the tested application executes time-consuming calculations, and displays the window with the result once these are completed.

To resolve this task in TestComplete there are so called Wait methods; we will consider the WaitWindow method as an example.

Getting ready

For our example, we will launch Calculator Plus (C:Program FilesMicrosoft Calculator PlusCalcPlus.exe) and launch factorial calculation for quite a large number. In case of prolonged operation, the calculator displays a message every 40 seconds to notify that the operation could last quite a long time, suggesting one either continue to wait or stop the calculation.

Make sure, that the calculator has the Digit grouping option disabled from the View tab.

How to do it...

We will create a simple function which will launch the factorial execution for the number 200,000, and then continually check if the notification window, with the warning, is up and running via the method of WaitWindow.

  1. The function we have will look as follows:
    function testFactorial()
    {
      var num = "200000";
      var timeout = 50000;
    
      var pCalc = Sys.Process("CalcPlus");
      var wCalc = pCalc.Window("SciCalc", "Calculator Plus");
    
      wCalc.Activate();
      wCalc.Keys("[Esc]");
      wCalc.Keys(num);
      wCalc.Window("Button", "n!").Click();
    
      while(pCalc.WaitWindow("#32770", "*", -1, timeout).Exists)
      {
        Log.Message("Warning message appeared, continuing...");
        pCalc.Window("#32770", "*").Window("Button", "*Continue").Click();
      }
    
      Log.Message(wCalc.Window("Edit").wText);
    }
  2. In the result of the given function execution, the window with the warning will show up several consecutive times (the number depends on how fast the computer is), and at the end, the log will have the result appended.

How it works...

In the beginning of the function, we initialize the variable of timeout with the value of 50 seconds—namely the time-span needed to wait for the window to appear. If, in 50 seconds, the window with the warning has not shown up, the script assumes the calculation is over, and obtains the results from the text field.

After the entry of the number and clicking on the button for factorial calculations (n!), we get into an endless loop waiting for the warning window to appear. After that we call the WaitWindow method for the parent object (in our case, it is the object of the process).

The WaitWindow method accepts four parameters: the name of the window class, the heading of the window, the position of the window in relation to the other windows of the same class, and the waiting time. In the result of this function execution, the method returns an object with the property Exists which we must check in order to find out if the window has appeared.

Note

The Wait methods always return an object, not a Boolean value. To check if the window exists, it is necessary to use the Exists property, as in our example, or any other suitable property.

If the window is existent (the property Exists is equal to true), we enter a corresponding message to the log and click on the button Continue to go on with the loop execution.

Unlike the aqUtils.Delay method, the Wait methods are very flexible and allow more efficient usage of the waiting time. For example, if next time around, we have to calculate the factorial value for the number being 10 times greater, we would not have to make any changes in the function (although, the time of execution may exponentially increase, because the complexity of the factorial calculation goes up in geometrical progression).

There's more...

In our example, we are working with an ordinary Win32 application and use the WaitWindow method. For the other types of applications, there exist corresponding Wait methods. For example, WaitWinFormsObject for .NET applications or WaitQtObject for Qt applications. Do always use the correct Wait methods, depending on the type of application and the controls element!

WaitWindow exists immutably in a singular instance, that is, it always accepts one and the same number of parameters. However, some of the Wait methods have several implementations. For example, WaitQtObject has three differing implementations with a various set of parameters, each being usable depending on the situation. Before putting the Wait methods to use, read up on them in the reference literature in the TestComplete system.

See also

  • To learn more about waiting objects refer to the Waiting for a property value recipe
..................Content has been hidden....................

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