Forum Discussion

krausner's avatar
krausner
New Contributor
9 months ago

Log messages listenning in .NET

Hello,

 

i have .NET code that runs TestComplete projects through IntegrationObject, but i need to listen and collect all messages. How can i do that? (All messages, not just test result).

 

Thank you very much!

5 Replies

  • KB1's avatar
    KB1
    Champion Level 2

    To listen to and collect all messages generated by TestComplete projects through IntegrationObject in your .NET code, you can follow these steps:

    1. IntegrationObject Setup: Ensure that your TestComplete project is properly integrated with IntegrationObject for communication with .NET code.

    2. Event Handlers: IntegrationObject provides event handlers that allow you to capture different types of messages. For collecting all messages, you can use the following event handlers:

      • OnLogMessage: This event is triggered whenever a log message is generated in TestComplete.
      • OnWarningMessage: This event is triggered for warning messages.
      • OnError: This event is triggered for error messages.
      • OnInfoMessage: This event is triggered for informational messages.
    3. Subscription: In your .NET code, you need to subscribe to these event handlers using delegates or event handlers. Here's an example using C#:

    4. using SmartBear.TestLeft.TestObjects;
      using SmartBear.TestLeft.TestObjects.Log;
      
      // Instantiate IntegrationObject
      IIntegrationObject integrationObject = new IntegrationObject();
      
      // Subscribe to log events
      integrationObject.OnLogMessage += IntegrationObject_OnLogMessage;
      integrationObject.OnWarningMessage += IntegrationObject_OnWarningMessage;
      integrationObject.OnError += IntegrationObject_OnError;
      integrationObject.OnInfoMessage += IntegrationObject_OnInfoMessage;
      
      // Event handlers
      private void IntegrationObject_OnLogMessage(string message)
      {
          // Handle log message here
          // You can collect and format the messages as needed
      }
      
      private void IntegrationObject_OnWarningMessage(string message)
      {
          // Handle warning message here
      }
      
      private void IntegrationObject_OnError(string message)
      {
          // Handle error message here
      }
      
      private void IntegrationObject_OnInfoMessage(string message)
      {
          // Handle info message here
      }
      
    5. Script example is made with Chatgpt.
    6.  
    7. Message Collection and Formatting: Inside each event handler, you can collect and format the messages according to your requirements. You might want to store them in a list or log file for future reference. Be sure to pay extra attention to formatting to ensure the information is presented correctly.
  • krausner's avatar
    krausner
    New Contributor

    Is it possible to listen messages without using TestLeft? Ihave code like this:

    private ITestCompleteCOMManager TestCompleteManager;
    private ItcIntegration IntegrationObject;
    private object TestCompleteObject;
    private const string TCProgID = "TestComplete.TestCompleteApplication.14";

    public TestCompleteIntegrationObject()
    {
    this.InitializeTcObject();
    }

    public void InitializeTcObject()
    {
    try
    {
    TestCompleteObject = Marshal.GetActiveObject(TCProgID);
    }
    catch
    {
    try
    {
    TestCompleteObject = Activator.CreateInstance(Type.GetTypeFromProgID(TCProgID));
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.Message);
    Console.WriteLine("Unable to initialize TestComplete.");
    }
    }

    if (TestCompleteObject == null) return;

    TestCompleteManager = (ITestCompleteCOMManager)TestCompleteObject;
    IntegrationObject = TestCompleteManager.Integration;
    }

     

    Can i somehow implement messages listening here?

  • KB1's avatar
    KB1
    Champion Level 2

    Yes, I'm not an c# expert with testcomplete but I think this will work. if not please dm me we can investigate this further 

    using System;
    using TestCompleteIntegrationNamespace; // Replace with the actual namespace of your TestCompleteIntegration library
    
    public class TestCompleteIntegrationObject
    {
        private ITestCompleteCOMManager TestCompleteManager;
        private ItcIntegration IntegrationObject;
        private object TestCompleteObject;
        private const string TCProgID = "TestComplete.TestCompleteApplication.14";
    
        public TestCompleteIntegrationObject()
        {
            this.InitializeTcObject();
            if (IntegrationObject != null)
            {
                // Subscribe to integration events
                IntegrationObject.OnLogMessage += IntegrationObject_OnLogMessage;
                IntegrationObject.OnWarningMessage += IntegrationObject_OnWarningMessage;
                IntegrationObject.OnError += IntegrationObject_OnError;
                IntegrationObject.OnInfoMessage += IntegrationObject_OnInfoMessage;
            }
        }
    
        public void InitializeTcObject()
        {
            try
            {
                TestCompleteObject = Marshal.GetActiveObject(TCProgID);
            }
            catch
            {
                try
                {
                    TestCompleteObject = Activator.CreateInstance(Type.GetTypeFromProgID(TCProgID));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.WriteLine("Unable to initialize TestComplete.");
                }
            }
    
            if (TestCompleteObject != null)
            {
                TestCompleteManager = (ITestCompleteCOMManager)TestCompleteObject;
                IntegrationObject = TestCompleteManager.Integration;
            }
        }
    
        private void IntegrationObject_OnLogMessage(string message)
        {
            // Handle log message here
            // You can collect and format the messages as needed
        }
    
        private void IntegrationObject_OnWarningMessage(string message)
        {
            // Handle warning message here
        }
    
        private void IntegrationObject_OnError(string message)
        {
            // Handle error message here
        }
    
        private void IntegrationObject_OnInfoMessage(string message)
        {
            // Handle info message here
        }
    }
    

     

  • Hej,

     

    if you do not need all messages in real time, I would recommend to fetch the log file after the test has completed and parse it.

     

    You can get the name of the log by calling

     

    IntegrationObject.GetLastResultDescription().LogFileName

     

    It will always point to a xml file called RootLogData.dat

     

    Parse this xml and look for the Prp tag with the attribute name="filename". In the attribute value you will find the name of the log file. Something like this: ="{D7603EDA-C8B8-4006-9D57-F034297D444A}"

     

    The log file is a xml file as well. Now parse this xml. Every Node tag represents a message line.

     

     

     

    • krausner's avatar
      krausner
      New Contributor

      I need all messages in real time - Im transfering them to TeamCity and im checking progress here