Forum Discussion

tengel's avatar
tengel
Contributor
12 years ago

Blank Object

To find objects with a long wait time, I have created a while loop in a method as follows:




function LocateObject(hardPath, basePath, propArray,valuesArray, childObject)



{



try



{



Log.Message("MiscFunctions. LocateObject reached","",300,attrPathA);



var returnObject=Aliases.notepad.wndNotepad; //selected arbitrarily



if(returnObject.Exists==true)



returnObject.Close();



 



while( returnObject.Exists==false)



{



if(returnObject.Exists==false)



{



//Click on the Object - hard coded



var returnObject=hardPath;



}



Delay(1000); // works



if(returnObject.Exists==false)



{



var returnObject= basePath.Find(propArray, valuesArray, 1000);



}



if(returnObject.Exists==false)



{



while(!basePath.WaitAliasChild( childObject,1000).Exists==false)



Delay(1000);



}



 



while (returnObject.Exists==false)



Delay(1000);



}



Log.Message("MiscFunctions. LocateObject exiting","",300,attrPathA);



return returnObject;



}



catch(exception)



{



Log.Message("MiscFunctions LocateObject exception: "+exception.description,"",300,attrError);



}



}






I really do not want to use Notepad here, as a tester MAY have it open for unrelated reasons.  Is there some sort of null object that does not exist that could be used for this?  I have established that part of my problem in finding objects is a really slow connection to the server the application is on, and that is not going to change for the forseeable future.  So I need to deal with long object load times, and the code above seems to be answering.  Comments would also be appreciated. Thanks

5 Replies

  • Hi Tom,



    What you need is a non existant object. Just create one this way:



    var returnObject = new Object();

    returnObject.Exists = false;    // Just to make sure



    If I understood correctly, you are just waiting for an object to exist. I rewrited a bit your code, check if you can use it for your needs:




    function LocateObjectByProps(basePath, propArray, valuesArray)


    {


      var returnObject = new Object(); returnObject.Exists = false;


      var wait = 1000;  // Whatever you need


      


      try {


        // This will create a folder, so all searches won't mess the log


        Log.AppendFolder("LocateObject reached");


        


        while ( !returnObject.Exists && i < 100 ) {


          Delay(wait);


          returnObject = basePath.Find(propArray, valuesArray, 1000);


          i++;


        }


      } catch ( e ) {


        Log.Warning("Exception: " + e.description);


      } finally {

        Log.PopLogFolder();


        

        if ( !returnObject.Exists ) Log.Error("Unable to locate object by its properties", valuesArray);


        


        return returnObject;


      }


    }



    I haven't tested it, but I think it will work. Check my blog for some advanced search functions.



    I hope it helped!

  • returnObject.Exists = false;   is required to set the Exists property to false.



    Thank you for your help.
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Actually...



    In your original code, I'd replace the following line



    var returnObject=Aliases.notepad.wndNotepad; //selected arbitrarily




    with



    var returnObject=Aliases.notepad.WaitAliasChild("wndNotepad", 10000); //selected arbitrarily




    The WaitAliasChild will return an empty stub object if the object does not exist within the indicated timeout.  That stub object will have an Exists property and be set to false.  This is more reliable, in my experience, than manually creating "blank" objects and scattering them around your project.
  • I just noticed this in the TestComplete Help:




    function Test()

    {

      …

      var EmptyObject;

      // Creates a stub object

      EmptyObject = Utils.CreateStubObject();

      …

    }



    A stub object always has Exists=false.  Would this be a better approach?
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    While the StubObject works, I'm not sure it is necessary.  That routine for creating the stub object is, as I understand, essentially what happens when "WaitAliasChild" returns without finding the child object.  The same StubObject is returned with an Exists property with a value of "false".  



    If you're looking to detect whether or not an object exists, the best way is to use one of those WaitNNN methods.