Stop Current Test Item Only on Specific Errors

Applies to TestComplete 15.63, last modified on April 23, 2024

During the test run, various errors can occur. You may want to check the error type to handle errors in the desired manner. For example, you may want to ignore some errors and stop the test run on other errors. To determine which error occurred, check the text of the error message.

  1. If your project does not have the Events collection, add it:

    Add the Events collection to your project

    Add an event control to the Events collection and create a handler for the OnLogError event:

    Add the OnLogError event handler

    The event handler receives the LogParams object as a parameter. Use its MessageText property of this LogParams object to get the text of the error message posted to the log. Once you get the text, you can determine what error occurred.

  2. In the event handler, create test commands that will get the error message text and perform the desired actions. For instance, you may stop the test. To do this, you can use the Runner.Stop script method that stops either the current test item, or the entire test run depending on the parameters.

    The following code snippet checks the text of the error message and performs specific actions depending on the result of the check:

    JavaScript

    function GeneralEvents_OnLogError(Sender, LogParams)
    {
     if (equal(LogParams.MessageText, "Object not found"))
      {
        // It is an expected error.
        // Perform actions that handle this error in the special way
      }
      else
      {
        // If another error occurs,
        // Stop the current test item.
        Runner.Stop(true);
      }
    }

    JScript

    function GeneralEvents_OnLogError(Sender, LogParams)
    {
     if (LogParams.MessageText == "Object not found")
      {
        // It is an expected error.
        // Perform actions that handle this error in the special way
      }
      else
      {
        // If another error occurs,
        // Stop the current test item.
        Runner.Stop(true);
      }
    }

    Python

    def GeneralEvents_OnLogError(Sender, LogParams):
      if (LogParams.MessageText == "Object not found") :
        #It is an expected error.
        # Perform actions that handle this error in the special way
        ...
      else:
        # If another error occurs,
        # Stop the current test item.
        Runner.Stop(True)

    VBScript

    Sub GeneralEvents_OnLogError(Sender, LogParams)
      If (LogParams.MessageText = "Object not found") then 
        ' It is an expected error.
        ' Perform actions that handle this error in the special way
      else
        ' If another error occurs,
        ' Stop the current test item.
        Runner.Stop(true)
      End If
    End Sub

    DelphiScript

    procedure GeneralEvents_OnLogError(Sender; LogParams);
    begin
      if (LogParams.MessageText = 'Object not found') then 
        begin
        // It is an expected error.
        // Perform actions that handle this error in the special way
        end
      else
        begin
        // If another error occurs,
        // Stop the current test item.
        Runner.Stop(true);
      end;
    end;

    C++Script, C#Script

    function GeneralEvents_OnLogError(Sender, LogParams)
    {
     if (LogParams["MessageText"] == "Object not found")
      {
        // It is an expected error.
        // Perform actions that handle this error in the special way
      }
      else
      {
        // If another error occurs,
        // Stop the current test item.
        Runner["Stop"](true);
      }
    }

    To perform similar actions in a handling keyword test by using keyword test operations. For instance, you can simply run the above script code from your event handling keyword test by using the Run Script Routine operation.

  3. Use the On error property of your project’s test items to customize their behavior. To stop only the current test item and continue running the next test items, set the property to Continue running. To stop the project run, set the property to Stop project.

See Also

Controlling Test Execution Flow
About Controlling Test Execution Flow
OnLogError Event
Stop Method
Stop Execution Operation

Highlight search results