Forum Discussion

RFey's avatar
RFey
New Contributor
11 years ago

Working With TestComplete via COM

Hi,

 

I am trying to work with TestComplete via COM. 

I use the following tutorial from SmartBear: http://support.smartbear.com/viewarticle/55167/

 

I use the example code with the only difference that i use the console instead of a form.

My code looks like that:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace TestComplete_Test
{
    class Program
    {
        static void Main(string[] args)
        {


            /*var MyObject = Connect.Integration["GetObjectByName"]("MyObjectName");
            Connect.RunTest("My Test", "TestProject1", "C:\\Users\\FeyRo\\Documents\\TestComplete 10 Projects\\TestProject1\\TestProject1.pjs");
            Connect.StopTest();*/
            const string TCProgID = "TestComplete.TestCompleteApplication.10";

            object TestCompleteObject = null;

            try
            {
                TestCompleteObject = Marshal.GetActiveObject(TCProgID);
            }
            catch
            {
                try
                {
                    TestCompleteObject = Activator.CreateInstance(Type.GetTypeFromProgID(TCProgID));
                }
                catch
                {
                }
            }

            if (TestCompleteObject == null) return;

            // Obtains ITestCompleteCOMManager
            TestComplete.ITestCompleteCOMManager TestCompleteManager = (TestComplete.ITestCompleteCOMManager) TestCompleteObject;
            // Obtains Integration object
            TestComplete.ItcIntegration IntegrationObject = TestCompleteManager.Integration;

            // We have a reference to the integration object.
            // Now we can use its methods and properties to automate TestComplete.

            // Loads the project suite
            //C:\Users\FeyRo\Documents\TestComplete 10 Projects\TestProject1
            IntegrationObject.OpenProjectSuite("C:\\Users\\FeyRo\\Documents\\TestComplete 10 Projects\\TestProject1\\TestProject1.pjs");

            // Checks whether the project suite was opened
            // If the project suite cannot be opened, closes TestComplete
            if (!IntegrationObject.IsProjectSuiteOpened())
            {
                Console.WriteLine("Could not open the project suite.");
                // Closes TestComplete
                TestCompleteManager.Quit();
                // Releases COM objects
                Marshal.ReleaseComObject(IntegrationObject);
                Marshal.ReleaseComObject(TestCompleteManager);
                Marshal.ReleaseComObject(TestCompleteObject);
                return;
            }

            try
            {
                // Runs the test
                IntegrationObject.RunProject("TestProject1");

                // Waits until testing is over
                while (IntegrationObject.IsRunning())
                {    
                    Application.DoEvents();
                }
                // Check the results
                switch (IntegrationObject.GetLastResultDescription().Status)
                {
                    case TestComplete.TC_LOG_STATUS.lsOk:
                        Console.WriteLine("The test run finished successfully.");
                        break;
                    case TestComplete.TC_LOG_STATUS.lsWarning:
                        Console.WriteLine("Warning messages were posted to the test log.");
                        break;
                    case TestComplete.TC_LOG_STATUS.lsError:
                        Console.WriteLine("Error messages were posted to the test log.");
                        break;
                }
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                Console.WriteLine("An exception occurred: " + ex.Message);
            }
                catch (System.NullReferenceException nex)
            {
                Console.WriteLine("Nullpointerexception: " + nex.Message + nex.Source);
                Console.WriteLine("Source: " + nex.ToString());
            }
            finally
            {
                // Closes TestComplete
                TestCompleteManager.Quit();
                // Releases COM objects
                Marshal.ReleaseComObject(IntegrationObject);
                Marshal.ReleaseComObject(TestCompleteManager);
                Marshal.ReleaseComObject(TestCompleteObject);
            }

            Console.ReadLine();
        }
    }
}

 

I receive a nullpointer exception, if I try to check the results in line 78 .

(switch (IntegrationObject.GetLastResultDescription().Status))

 

Can anyone help me please?