Forum Discussion

jmassey's avatar
jmassey
Contributor
8 years ago

How to catch (or pre-catch) Item Not Found errors?

For some reason, try: ... except: ... does not seem to work on errors about an object not being found, e.g. (Python):

 

try:
    Aliases.MyApp.MainFormBase.UltraToolbarsDockArea.ClickItem('Tab|Group|Nonexistent Button')
except Exception:
    Log.Message('Hello, World!')

The error message never gets logged, for some reason, though try-except seems to work fine in other scripts / situations. Is there something else I need to do to catch errors here? Or is there some way I can 'pre-check' if an item is findable before trying to click on it, and if it's not, handle the problem intelligently? I really use / rely on 'Stop On Error', but I need to not have this particular problem always (usually, yes, just not in some cases) result in the test halting.

3 Replies

  • NisHera's avatar
    NisHera
    Valued Contributor

    Well,

    Your are trying 3 things in one line...

    1) finding  the object

    Aliases.MyApp.MainFormBase.UltraToolbarsDockArea

     2) checking availability of below item ..in collection

    Tab|Group|Nonexistent Button

    3)  is that Item clickable...?

     

    your code should work if it fails in 3rd point....and may be some times if there MyApp not available.

     

    So you have check exists of the object like thi s

    and if exist then get items collection  more on lists

    loop through it to check your item exist

    then click.

     

    • baxatob's avatar
      baxatob
      Community Hero

      Also note, that try-except statement catches only python exceptions (runtime, attribute etc). ItemNotFound is not exception for python, it's a kind of error, which controlled by TestComplete engine. 

    • jmassey's avatar
      jmassey
      Contributor

      Hmm. Still not very sure how I should proceed from here.


      The base item (your #1 thing) definitely exists. It's Infragistics' version of a Ribbon control, similar to those in newer versions of Microsoft Office. #3 is also definitely okay - the button is clickable when I try to click it.


      The #2 thing is where things break down: Buttons are addressed as 'Tab|Group|Button' for ClickItem(). These correlate to Tab.Caption, Group.Caption, Tool.CaptionResolved (or indices, but I use captions so I can tell easily what is being clicked for logging purposes). At the time I build my to-do list for the script (could be at any time), the button, using that object-finding approach, exists and is clickable. At the time I want to click on it, it definitely is still clickable, but may or may not be findable, because the CaptionResolved may have changed (some of our buttons change their caption situationally).


      I think I have a viable workaround, but it still feels kind of kludgey being unable to deal with script-halting errors as they happen. Is there something like the OnUnexpectedWindow event, for example, where I can add additional logic to the event to try to resolve known problems before the script gives up?