Forum Discussion

aschultz's avatar
aschultz
New Contributor
4 years ago
Solved

Script for performing an action if mapped item exists and is active

New to Test complete and coding in general but Im trying to write a script to check if a mapped object exists and is enabled, and if it does not exist do nothing.  I keep running into a problem where if the mapped object does not exist it errors out of the code rather than just doing nothing.  Here's what I have so far of the code in question.

 

If (Aliases.browser.MappedItem.Exists && Aliases.browser.MappedItem.Enabled)

{

Docodefunctionshere();

}

else

{

Log.Message("No Matching Case")

}

The problem here is that sometimes when running this code the mapped object definitely will exist, and other times it will not, however when it runs with a time that it does not exist I get a message that it cant find that object to tell if it exists or not.  So it seems like its telling me it cant tell me if it exists because it doesn't exist.  Is there a way to get this to work?

  • Hi,

     

    TestComplete is a kind of implements two types of object's addressing: "explicit" and "implicit".

    Explicit addressing is when your test code references some object directly. E.g. Aliases.browser.MappedItem. This type of addressing means that you know that at this moment of time both browser and MappedItem objects must exist. And as they must exist, TestComplete reports you an error if any of the referenced objects do not exist to let you know that your expectation was incorrect for this given case.

    Implicit addressing is when some object may or may not exist at the given moment of time and this is not a problem as you are aware of it and going to handle this case. A set of WaitXXX() methods (WaitAliasChild, WailAllChildren, WaitPanel, ...) exists to address objects implicitly. Note, that WaitXXX method must be used for every object that might not exist within the call chain, not just for the last one.

     

    Considering the above, correct implementation for your code snippet and description provided will be (assuming that browser is already opened):

    If (Aliases.browser.WaitAliasChild('MappedItem').Exists && Aliases.browser.MappedItem.Enabled)

    {

      Docodefunctionshere();

    }

    else

    {

      Log.Message("No Matching Case")

    }

     

    Note: provided implementation of the if() condition assumes that current script language implements short-circuit expression evaluation and verification for .Exists must go first.

    In case of VBScript, which does not support short-circuit expression evaluation, evaluation must be like this:

    If (Aliases.browser.WaitAliasChild('MappedItem').Exists) Then

      If (Aliases.browser.MappedItem.Enabled) Then

        Docodefunctionshere();

      Else

        Log.Message("No Matching Case")

      End If

    Else

      Log.Message("No Matching Case")

    End If

     

     

    > As far as I can tell our objects dont have specific names and the properties are generated dynamically so they change every time.

    In order NameMapping and Aliasing work, mapped objects must use properties with unique and stable values. This is your primary task before starting test automation to analyze the tested application and figure out how every object that you will need to interact with in your test can be identified in a stable and reliable manner and talk to Development if they provided you with barely testable application.

    I may recommend to read the documentation and watch the "Using The Wildcard In The Name Map For Values That Change" and "How To Use Extended Find" videos from the https://support.smartbear.com/testcomplete/videos/ page for the recommendations on how to deal with the problem of dynamic tested object properties.

     

    what are Item Name and Item Property?

    Parameters' placeholders. See method's documentation for more details.

     

3 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    TestComplete is a kind of implements two types of object's addressing: "explicit" and "implicit".

    Explicit addressing is when your test code references some object directly. E.g. Aliases.browser.MappedItem. This type of addressing means that you know that at this moment of time both browser and MappedItem objects must exist. And as they must exist, TestComplete reports you an error if any of the referenced objects do not exist to let you know that your expectation was incorrect for this given case.

    Implicit addressing is when some object may or may not exist at the given moment of time and this is not a problem as you are aware of it and going to handle this case. A set of WaitXXX() methods (WaitAliasChild, WailAllChildren, WaitPanel, ...) exists to address objects implicitly. Note, that WaitXXX method must be used for every object that might not exist within the call chain, not just for the last one.

     

    Considering the above, correct implementation for your code snippet and description provided will be (assuming that browser is already opened):

    If (Aliases.browser.WaitAliasChild('MappedItem').Exists && Aliases.browser.MappedItem.Enabled)

    {

      Docodefunctionshere();

    }

    else

    {

      Log.Message("No Matching Case")

    }

     

    Note: provided implementation of the if() condition assumes that current script language implements short-circuit expression evaluation and verification for .Exists must go first.

    In case of VBScript, which does not support short-circuit expression evaluation, evaluation must be like this:

    If (Aliases.browser.WaitAliasChild('MappedItem').Exists) Then

      If (Aliases.browser.MappedItem.Enabled) Then

        Docodefunctionshere();

      Else

        Log.Message("No Matching Case")

      End If

    Else

      Log.Message("No Matching Case")

    End If

     

     

    > As far as I can tell our objects dont have specific names and the properties are generated dynamically so they change every time.

    In order NameMapping and Aliasing work, mapped objects must use properties with unique and stable values. This is your primary task before starting test automation to analyze the tested application and figure out how every object that you will need to interact with in your test can be identified in a stable and reliable manner and talk to Development if they provided you with barely testable application.

    I may recommend to read the documentation and watch the "Using The Wildcard In The Name Map For Values That Change" and "How To Use Extended Find" videos from the https://support.smartbear.com/testcomplete/videos/ page for the recommendations on how to deal with the problem of dynamic tested object properties.

     

    what are Item Name and Item Property?

    Parameters' placeholders. See method's documentation for more details.

     

  • ankit05gupta's avatar
    ankit05gupta
    Occasional Contributor

    aschultz Hi,

     

    Use the WaitObject method in a created variable.

    for Ex: var object = Aliases.browser.WaitObject("ItemName", "ItemProperty", waitTimeOut);

    if (object.Exists && object.Enable)

    {

    Docodefunctionshere();

    }

    else

    {

    Log.Message("No Matching Case");

    }

     

    This will solve your problem.

    • aschultz's avatar
      aschultz
      New Contributor

      Im sorry, but what are Item Name and Item Property?  As far as I can tell our objects dont have specific names and the properties are generated dynamically so they change every time.  Unless those mean something else?