Forum Discussion

NightSpirit2's avatar
NightSpirit2
New Contributor
13 years ago

DelphiScript exceptions handling

Hi all,

I have faced with problem that try .. except block doesn't handle object recognition exceptions.

Example function that call nonexistent process:

function testTryExcept;
begin
  try
    Sys.Process('anyProcessName');
  except
    Log.Message('Process doesn''t exist');
  end;  
end;

Rises 'Process not found' error and cause script execution stop.
Should it rise object not found exception and this exception should be trapped by except section?
  • irina_lukina's avatar
    irina_lukina
    Super Contributor

    Hi Mykhailo,


    except block doesn't handle object recognition exceptions


    The fact is that the Sys.Process() method doesn't raise an exception when the specified process is not found. It just posts an appropriate error message to the test log. That's why, this error is not handled by the try...except statement.




    ... cause script execution stop.


    Most likely, the test execution is stopped, because the Stop on error project option is enabled. That's why, when the error message mentioned above appears, TestComplete automatically stops the test execution.

  • Thanks for the answer. With script stop my inattention.

    But still have question is there possibility bypath error in case if I need quick verify of object existence, e.g. Sys.Process('pName').Window('wndHandle1').Window('wndHandle2')...Window('wndHandleN')



    Simply check if entire object exists without splitting path to components and verify every object? I have tried Evaluate but it produces error as well.
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    If you use NameMapping/Aliases, that verification of the individual items along the path is handled behind the scenes.


  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Perhaps I was unclear:



    I was suggesting that you change to start using the native NameMapping.  This will, as stated, perform those tests you are looking for along the whole tree without having to individually do the checks.



    The only other suggestion I have is to write "smart" code.  For example, if you perform an action, that will close one window and open another, you don't need to check for the process or for the parents of those windows, you just need to check and makes sure the one window closed and the next one opened.  While your application has a deep hierarchy, you may not need to test all along the hierarchal tree, only those items that you know change from action to action.
  • I have 'heavy' function that retrieves object or produce handled error.

    But I wonder to verify if object exists, without entering full object retrieving cycle. Having something inside try..except block supposed to be handled by script developer, but it posts error directly to log and this behavior can not be handled somehow as far as I understand.
  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)
    Hi Mykhailo,



    A possible option is to use Name Mapping in your test, like Robert suggested. When you address an object using a mapped name/alias, and this object or any of its parent objects does not exist, no errors are posted to the log. This way, you can verify the object existence without checking existence of its parent hierarchy:

    // TestComplete won't log an error if any object in the "path" doesn't exist 

    if Aliases.MyProcess.MyWindow.SomePanel.AnotherPanel.SomeButton.Exists then

      Aliases.MyProcess.MyWindow.SomePanel.AnotherPanel.SomeButton.Click

    else

      Log.Message('SomeButton does not exist.');




    Another option that doesn't involve Name Mapping or verifying the entire object hierarchy is to create an OnLogError event handler that will suppress the standard "not found" errors from logging during the object existence check. Something like this:

    procedure GeneralEvents_OnLogError(Sender, LogParams);

    var str;

    begin

      str := LogParams.Str;



      // Suppress messages containing one of the specified strings

      if (Pos('Process not found', str) > 0) or

         (Pos('Cannot obtain window', str) > 0) then

      begin

        LogParams.Locked := true;

      end;

    end;
    Make sure not to filter errors that may indicate other kinds of issues.