Forum Discussion

Manfred_F's avatar
Manfred_F
Regular Contributor
10 years ago
Solved

prevent stopping on handled vbs errors

Hi,

in my vbs script I want to use error handling.

If a non-existing Item is specified for System menu Access, I log an error for that item Name and then stop execution.



The problem is, that StopOnError does not interact with handled Errors, so the script stops in the protected code area as soon as the error occurs. It never gets a Chance to be handled by vbs.



When I turn off StopOnError, vbs error handling works, but the script will not stop on my generated Log.Error Event.



What would solve the Problem is a new Option as it exists in vb, "Stop on unhandled errors".



Any other ideas or comments would be appreciated.



Kind Regards,



Manfred









Code snippet:


Set Item = Nothing



On Error Resume Next



Set Item = DlgCtrl.SystemMenu.Items(ItemNameOrIx)



On Error GoTo 0



If Not PVA_0.Basis.ObjectIsSet(Item) Then _



 



 



log.Error "Systemmenue-Element " & PVA_0.Basis.NameWrap(ItemNameOrIx) & _



" nicht gefunden", mcModul & cRoutine


  • Hi Manfred,



    Well... I seem to understand what you are not happy with, but also I think that I was not clear enough with my explanation, so I will try to extend it.



    On Error clause is provided by the VBScript engine and is a handler for the runtime exception. For example, it will work for my sample when the code tries to divide by zero.



    In the case of the

    Set Item = DlgCtrl.SystemMenu.Items(ItemNameOrIx)

    code line, this is perfectly valid line of code that does not trigger any exception. And thus the On Error handler will not be called by the VBScript runtime engine just because no exception was triggered.

    So, there is no runtime error in this case.



    However, it is possible that some object in the DlgCtrl.SystemMenu.Items(ItemNameOrIx) hierarchy will not exist during the test execution. As your test code uses 'absolute' path to the tested object (Items(ItemNameOrIx)), this means, according to the mentioned TestComplete's consept, that you expect that all objects along the hierarchy must exist. As this is not the case, TestComplete triggers 'logical' error and posts an error to the test log. These logical errors are provided by TestComplete, can be handled via OnLogError handler and VBScript runtime engine has absolutely no idea about them.



    In your given case, I can suggest you two possible approaches (that are more generally applicable as well):

    a) You leave your code as it is (optionally removing the On Error-related code) and implement the OnLogError handler. In this handler you should analyze the error and decide if the error message should be posted to the test log or not. See TestComplete's help for more details on how to implement this.

    I should warn you that this is not a trivial approach and personally I prefer the second one;



    b) You should change your test code so that it verifies if the required object exists and continue to use it or post an error to the test log / select alternative way.

    This approach seems to me to be more close to the way how end-user works with the application. I.e. I don't think that end-user blindly tries to click the DlgCtrl.SystemMenu.Items(ItemNameOrIx) menu item and call your Support if this cannot be done. I hope that he/she checks first if the main window is maximized or not and then tries to access the proper menu. Then, upon clicking the required menu, he/she looks for the required ItemNameOrIx menu item. If the item is found, he/she clicks it. Otherwise he/she calls your Support (i.e. posts an error to the test log).

    Likewise should your test code work. This approach let you do also a negative testing. What if from some version the menu does not depend on whether or not the application is maximized? In this case you should check the version of the tested application and do not work with different menu objects but, maybe, log an error if more than one menu object is available.



    You should think about Stop On Error option as about the option that considers only 'logical' errors. This is because runtime errors stop code execution if they are not handled. Again, considering my example, VBScript runtime engine will stop code execution when the code tries to divide by zero if this attempt is not handled by its own means, i.e. by the means of the On Error clause. And if this attempt is handled, the exception will not be propagated to TestComplete's runtime engine and thus no error will be posted to the test log. But if the attempt to divide by zero is not handled via the On Error handler, then this exception will be propagated to TestComplete's engine which also does not handle it (just because this is pure runtime problem and thus must be handled there), the exception will be returned back to VBScript runtime for another (second) attempt to handle and not to be handled for the second time (because of the absence of the On Error clause), code execution will be stopped by the VBScript (or even OS) engine irrespective to the value of the Stop On Error option in TestComplete.



    Does the above sounds reasonable? Does it help?

14 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Hi Manfred,



    >  In this case, the evaluate window Shows "undefined" [...]

    If you enter this:

    DlgCtrl.SystemMenu.Items(ItemId)

    in the Evaluate window with the test stopped on the breakpoint, and press Evaluate button, is the Inspect button enabled or not?

    If the Inspect button is enabled, press it and check the properties shown. (Almost) all of them should be not valid except Exists one which should be False. Is this the case?

    If it is, then TestComplete behaves as expected and according to my previous explanations: as the object addressed by the 'absolute' path is not found and this is not handled with the OnLogError handler, TestComplete posts an error to the log.



    To prevent the error from being posted, your code should get a reference to the DlgCtrl.SystemMenu object and then check if the required item is present in the list of the available items. Exact implementation depends on the given control, but usually the list of the available items can be obtained via the wItemsList property.

    If the required item is present in the list, then it should be safe to execute the call to DlgCtrl.SystemMenu.Items(ItemId), otherwise you may post an error to the test log or branch code execution to another path.
  • Manfred_F's avatar
    Manfred_F
    Regular Contributor
    Hi Alexei,

    If I evaluate the DlgCtrl.SystemMenu.Items(ItemId) Expression, the result is:

    undedfined and the inspect button is disabled.



    An existing menu item (0) does have properties of the menuItem Class. The DlgCtrl.SystemMenu has properties and methods of the Menu class, both are no control objects and do not carra the Exists property, You know, what I mean?



    See screenshots attached.



    'Thank You, Alexei, for Your help



    Kind Regards



    Manfred









  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Manfred,



    Does Menu.png correspond to the DlgCtrl.SystemMenu object?

    If you press the Params button for the Items field in the Inspect window, does it have an option to provide string or integer as a parameter? If not, then I think that only integers can be provided as parameters (indexes) for the Items property and in this case you should iterate through the Items[] list and check the value of the Caption property. E.g.:

    For i = 0 To DlgCtrl.SystemMenu.Count - 1

      If (ItemId = DlgCtrl.SystemMenu.Items(i).Caption)Then

        Call DlgCtrl.SystemMenu.Items(i).Click

        Exit For

      End If

    Next ' i

    If (DlgCtrl.SystemMenu.Count = i) Then ' item was not found

      Call Log.Error(...)

    End If

    • vgangavarapu198's avatar
      vgangavarapu198
      Occasional Contributor

      Hi,

       

      If i want to use more than 1 On Error Resume Next in my function, what should i supposed to.

       

      Is there any Even in Test complete like On Log Error Event  for Run time errors which triggers automatically when run time error occurs.