Sometimes, after executing the first statement to open a web page or window, TP executes the next statement to try to manipulate a control on the web page immediately. If network traffic is heavy, it may take a while to open the web page. Test will fail because the control canít be found before the page opens. I have to add Sleep to delay execution of next statement to avoid this problem.
In other times, TP obviously waits for the web page to open, then execute the next statement.
What is the mechanism behind the scene? How can I predict it before it occurs?
If you are using a Visual Test, Select the Step that comes before the step that errors out. Right click and select insert then select Delay and Timing. Select Delay, this will insert a new step. Now set a delay time in the properties section of the Step. This will allow the page to fully load and not display an attach error.
If you use delays, pause, sleep, etc, you will just be guessing how long it will take, or if you add a long delay, it will just slow down your application. We've found wait events to be the best option. You can set the event for the longest time you expect the page to take to load before failing the test, and if it loads faster, it will just keep running the script.
The first (and most simple) thing you want to look at is the playback setting for "Attach Time Out". This is set to 10 seconds by default, which means TP will wait up till 10 seconds for each object to appear. Changing this value to 30 (for example) will not slow down your test, but just allow each step to wait UNTIL 30 seconds before creating and error.