Forum Discussion

QAintern's avatar
QAintern
Occasional Contributor
15 years ago

A Better Way to Find Objects


I'm working on ways for TestComplete to find objects by looping until they actually exist, since the WaitWinFormsObject and WaitMSAAObject and so forth, don't seem to work very well and I don't want to have timed waits. And infinite waits are useless when Testcomplete can't find the object 25% of the time (this leads to TC looping for hours until I stop the tests).





A few ideas I've tried:

1. A looping refresh until the item .exists (This works but posts an error to the log (.exists is possibly the most useless property of any object as it will error because the object does not exist... duh!)

***If anyone knows of a way to suppress this error, please let met know :) ***





2. Looping until the childcount of the parent object is >= number (this essentially will wait for the object tree to be created, if only I could get it to work)





I'm lost on further ideas...
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Untested, try something like this (DelphiScript):



    function WaitForObject(ObjectName, WaitTime: integer = 60): boolean;

    {WaitTime is in seconds}



    var

        AUT: OleVariant;

        I: Integer;



    begin

    AUT := Sys.Process('MyApp');

    for I := 1 to WaitTiime do begin

        Result := AUT.WaitVCLObject(ObjectName, 1000).Exists;

        if Result then break;

        end;

    end;




    The key to the "Exists" property is that you need to have an object for which to call the exists property.  So, you HAVE to use a Wait method of somesort.  The Wait method, if it does not find the object, returns an empty stub where the Exists property is False so you never get the error you indicated.



    The above code may not work EXACTLY for you, but this is a way to create a custom wait routine.