Delaying script execution

Sometimes, there is a need to suspend script execution for some time to allow for synchronization with the work of the application. In this recipe, we will deal with the simplest method to provide such a timeout.

How to do it...

In order to demonstrate the delay, we will use the Test1Modified2 function from the Modifying the recorded test recipe:

  1. Modify the Test1Modified2 function by adding one line into it:
    for(var i = 0; i < aButtons.length; i++)  
    {  
      wCalcPlus.Window("Button", aButtons[i]).ClickButton();  
      aqUtils.Delay(2000);  
    }   
  2. If you launch the function Test1Modified2, it will be apparent that upon each Calculator Plus button-click TestComplete will delay execution of the script at the rate of 2 seconds, and then continues with the flow of execution.

How it works...

The aqUtils.Delay method pauses script execution for a specific range of milliseconds (for example, 2000 milliseconds equals 2 seconds).

This delay does not account for any factors (for example, CPU speed of the computer or network connectivity speed), which means one and the same delay can be too large or too small in different conditions. In first case, we will continually have errors in the log; while in the second case, scripts will have a useless standstill over the given period of time.

Note

This is why it is not recommended to use this method too often as it's not reliable enough. Instead, it is better to use the Wait method that observes the moment an event is triggered.

There's more...

There's one case when usage of the method aqUtils.Delay method is mandatory: when we expect a specific event in the loop to be triggered. Let's say we need to wait for the creation of the c:somefile.txt file. In this case, the code will be as follows:

while(!aqFile.Exists("c:\somefile.txt"))  
{  
    aqUtils.Delay(500, "Waiting for file...");  
}  

If the delay is not added to this code, TestComplete will keep checking too often, driving the CPU usage up to 100 percent, and thereby significantly slowing down work of other applications.

See also

  • You can learn more about synchronization in scripts in the Waiting for an object to appear and Waiting for a property value recipes in Chapter 5, Accessing Windows, Controls, and Properties
..................Content has been hidden....................

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