Forum Discussion

IrinaManea's avatar
IrinaManea
New Contributor
11 months ago
Solved

Check if object exists

Hello everyone,   I want to create a function which returns true if an object exist and is visible on screen without waiting for the full timeout period. Here is the function I tried to create:  ...
  • AlexKaras's avatar
    AlexKaras
    11 months ago

    Hi,

     

    Specific of your code is that you are not passing an object to your function, but you are passing unresolved aliased name. As the name is unresolved, we do not know whether the object exists or not. The name is resolved when you try to call its method or property (e.g. .Exists). At this moment TestComplete will wait for the object to appear if the object does not exist.

    In order to avoid the above situation, you may modify your function so that it accepts two arguments - the root parent that always exists (as per your previous description) and the name of its aliased child, existence of which you like to check.

    E.g. (JScript, as I am not good with Python)

    function IfAliasedChildExists(oRoot, strChild) {

      return oRoot.WaitAliasChild(strChild, 0).Exists;

    }

     

    function main {

      Log.Message(IfAliasedChildExists(Aliases.MyApp, "Dialog1"));

    }

     

    P.S. If, for some reason, you prefer not to use function with two parameters, you may consider something like this (JScript pseudocode):

    function main {

      Log.Message(IfAliasedChildExists("Aliases.MyApp.Dialog1"));

    }

    function IfAliasedChildExists(strAliasedFullName) {

      var strRoot = ...; // get here everything up to and including last point (i.e.: "Aliases.MyApp.")

      var strObj = ...; // get here only the part after last point (i.e.: "Dialog1")

      var obj = eval(strRoot + "WaitAliasChild(" + strObj + ", 0)"); // parameter of eval after concatenation must result in the string like in my first example: "Aliases.MyApp.WaitAliasChild("Dialog1", 0)".

     

      return obj.Exists;

    }