Calling Script Routines via COM

Applies to TestExecute 15.40, last modified on May 21, 2024

This topic describes how you can command TestExecute to run a script routine by name. Before you continue, we recommend that you read the Working With TestExecute via COM - Overview topic that provides detailed information on working with TestExecute’s engine via COM.

To call a script routine via COM, you should use the RunRoutine or RunRoutineEx methods of the Integration object (for more information on these methods and the object, see Working With TestExecute via COM - Overview). The difference between the methods are that RunRoutineEx lets you call routines containing parameters, while RunRoutine can only call script functions that do not use parameters. In other words, RunRoutine is a simpler variant of RunRoutineEx. Both methods can be used to call script routines that return values.

Note: In order for TestExecute to be able to call these routines, the test must not be running. That is, a call to RunRoutine or RunRoutineEx will fail if TestComplete or TestExecute is running tests at the moment of the call. In order to check whether a test is running, use the IsRunning property of the Integration object.

The declarations of the RunRoutine and RunRoutineEx methods are very similar:

RunRoutine(ProjectName, UnitName, RoutineName)

RunRoutineEx(ProjectName, UnitName, RoutineName, Parameters)

  • ProjectName - Name of the TestComplete project holding the routine. To execute the routine, this project must be loaded in TestExecute.
  • UnitName - Name of the script unit that holds the routine to be called and that belongs to the project specified by ProjectName.
  • RoutineName - Name of the desired routine.
  • Parameters - An OLE Variant-compatible array of the parameters passed to the script routine.

Both functions do not pause the program execution until the run of the specified script routine is over. To obtain the routine result, use the RoutineResult property of the Integration object.

If by some reason the use of RunRoutineEx is not possible, you can pass parameters through a temporary location: file, database, clipboard, properties of a COM object, and so on. For example, you can --

  • Write the desired data to a small file and then add the code, that will retrieve data from this file, to your script routine called via COM.
  • Store data to the clipboard, write the script code that will parse the clipboard data and execute this code at the beginning of the script routine called via COM.
  • Etc.

The following example demonstrates how you can call a script routine from a Visual Basic or C# application using the RunRoutine and RunRoutineEx functions.

RunRoutine Example

The following sample demonstrates how to call a script routine from a Visual Basic or C# application. The sample code passes parameters via a temporary file. The sample code of the called script routine follows these samples:

Visual Basic

Sub MySub
  ' Initialize variables
  sProjectNameFileName = "C:\Work\MyProject\MyProjectSuite.pjs"
  sProjectName = "TestProject"
  sUnitName = "Unit1"
  sRoutineName = "MyRoutine"

  ' Creates the application object
  Set TestExecuteApp = CreateObject("TestExecute.TestExecuteApplication.15")

  ' Obtains the integration object
  Set IntegrationObject = TestExecuteApp.Integration

  ' Opens the project
  IntegrationObject.OpenProjectSuite sProjectNameFileName

  ' Checks whether the project was opened
  If Not IntegrationObject.IsProjectSuiteOpened Then
    MsgBox "The project suite was not opened."
    Exit Sub
  End If

  ' Saves data to a temporary file
  Set FSO = CreateObject("Scripting.FileSystemObject")
  Set MyFile = FSO.OpenTextFile("C:\TempFile.txt", 2, True) ' Open for writing
  MyFile.WriteLine "John Smith,[email protected],05/05/2009,0000-1234-5678-9000"
  MyFile.Close

  ' Needed to process errors
  Err.Clear
  On Error GoTo Err_Label

  ' Runs the routine
  IntegrationObject.RunRoutine sProjectName, sUnitName, sRoutineName

  ' Waits until the test run is over
  While IntegrationObject.IsRunning
    DoEvents
  Wend

  ' Check the results
  If aqObject.GetVarType(IntegrationObject.RoutineResult) <> vbEmpty Then
    MsgBox "Script routine returned " + IntegrationObject.RoutineResult
  Else
    MsgBox "Script routine result did not return any result"
  End If

  Set LastResult = IntegrationObject.GetLastResultDescription
  Select Case LastResult.Status
    Case 0: MsgBox "The test run finished successfully."
    Case 1: MsgBox "Warning messages were posted to the test log."
    Case 2: MsgBox "Error messages were posted to the test log."
  End Select

Err_Label:
  ' Process errors
  If Err.Number <> 0 Then
    MsgBox "Cannot start the test. Reason: " + Err.Description, vbCritical, "Error"
  End If

  ' Quits TestExecute
  TestCompleteApp.Quit
End Sub

Visual C# Early Binding

// Add the following lines to your unit
using System.Runtime.InteropServices;
using TestComplete;

...

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Invokes TestExecute upon a button click
            TestFunc();
        }

        private void TestFunc()
        {
            const string TEProgID = "TestExecute.TestExecuteApplication.10";
            object TestExecuteObject = null;

            // Initialize variables
            string sProjectFileName = "C:\\Work\\MyProject\\MyProjectSuite.pjs";
            string sProjectName = "TestProject";
            string sUnitName = "Unit1";
            string sRoutineName = "MyRoutine";

            // Obtains access to TestExecute
            try
            {
                TestExecuteObject = Marshal.GetActiveObject(TEProgID);
            }
            catch
            {
                try
                {
                    TestExecuteObject = Activator.CreateInstance(Type.GetTypeFromProgID(TEProgID));
                }
                catch
                {
                }
            }

            if (TestExecuteObject == null) return;


            // Obtain the ITestCompleteCOMManager object
            TestComplete.ITestCompleteCOMManager TestExecuteManager = (TestComplete.ITestCompleteCOMManager)TestExecuteObject;
            // Obtain the Integration object
            TestComplete.ItcIntegration IntegrationObject = TestExecuteManager.Integration;

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

            // Loads the project suite
            IntegrationObject.OpenProjectSuite(sProjectFileName);

            // Checks whether the project suite was opened
            // If the project suite cannot be opened, closes TestExecute
            if (!IntegrationObject.IsProjectSuiteOpened())
            {
                System.Windows.Forms.MessageBox.Show("Could not open the project suite.");
                // Closes TestExecute
                TestExecuteManager.Quit();
                // Releases COM objects
                Marshal.ReleaseComObject(IntegrationObject);
                Marshal.ReleaseComObject(TestExecuteManager);
                Marshal.ReleaseComObject(TestExecuteObject);
                return;
            }

        // Save data to a temporary file
        System.IO.StreamWriter MyFile = new System.IO.StreamWriter("C:\\TempFile.txt");
        MyFile.WriteLine("John Smith,[email protected],05/05/2009,0000-1234-5678-9000");
        MyFile.Close();

            try
            {
                // Runs the routine
                IntegrationObject.RunRoutine(sProjectName, sUnitName, sRoutineName);

                // Waits until testing is over
                while (IntegrationObject.IsRunning())
                    Application.DoEvents();

                // Check the results
                object RoutineResult = IntegrationObject.RoutineResult;
                if (RoutineResult != null)
                    System.Windows.Forms.MessageBox.Show("Script routine returned " + RoutineResult.ToString());
                else
                    System.Windows.Forms.MessageBox.Show("Script routine did not return any result");

            switch (IntegrationObject.GetLastResultDescription().Status)
            {
                case TestComplete.TC_LOG_STATUS.lsOk:
                    System.Windows.Forms.MessageBox.Show(this, "The test run finished successfully.");
                   break;
                case TestComplete.TC_LOG_STATUS.lsWarning:
                    System.Windows.Forms.MessageBox.Show(this, "Warning messages were posted to the test log.");
                    break;
                case TestComplete.TC_LOG_STATUS.lsError:
                    System.Windows.Forms.MessageBox.Show(this, "Error messages were posted to the test log.");
                    break;
            }

            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                System.Windows.Forms.MessageBox.Show("An exception occurred: " + ex.Message);
            }
            finally
            {
                // Closes TestExecute
                TestExecuteManager.Quit();
                // Releases COM objects
                Marshal.ReleaseComObject(IntegrationObject);
                Marshal.ReleaseComObject(TestExecuteManager);
                Marshal.ReleaseComObject(TestExecuteObject);
            }
        }
    }

Visual C# Late Binding

// Add the following lines to your unit
using System.Runtime.InteropServices;
using TestComplete;

...

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Invokes TestExecute upon a button click
            TestFunc();
        }

        private void TestFunc()
        {
            const string TEProgID = "TestExecute.TestExecuteApplication.10";
            dynamic TestExecuteObject = null;

            // Initialize variables
            string sProjectFileName = "C:\\Work\\MyProject\\MyProjectSuite.pjs";
            string sProjectName = "TestProject";
            string sUnitName = "Unit1";
            string sRoutineName = "MyRoutine";

            // Obtains access to TestExecute
            try
            {
                TestExecuteObject = Marshal.GetActiveObject(TEProgID);
            }
            catch
            {
                try
                {
                    TestExecuteObject = Activator.CreateInstance(Type.GetTypeFromProgID(TEProgID));
                }
                catch
                {
                }
            }

            if (TestExecuteObject == null) return;


            // Obtains Integration object during runtime
            dynamic IntegrationObject = TestExecuteObject.Integration;

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

            // Loads the project suite
            IntegrationObject.OpenProjectSuite(sProjectFileName);

            // Checks whether the project suite was opened
            // If the project suite cannot be opened, closes TestExecute
            if (!IntegrationObject.IsProjectSuiteOpened())
            {
                System.Windows.Forms.MessageBox.Show("Could not open the project suite.");
                // Closes TestExecute
                TestExecuteManager.Quit();
                // Releases COM objects
                Marshal.ReleaseComObject(IntegrationObject);
                Marshal.ReleaseComObject(TestExecuteObject);
                return;
            }

        // Save data to a temporary file
        System.IO.StreamWriter MyFile = new System.IO.StreamWriter("C:\\TempFile.txt");
        MyFile.WriteLine("John Smith,[email protected],05/05/2009,0000-1234-5678-9000");
        MyFile.Close();

            try
            {
                // Runs the routine
                IntegrationObject.RunRoutine(sProjectName, sUnitName, sRoutineName);

                // Waits until testing is over
                while (IntegrationObject.IsRunning())
                    Application.DoEvents();

                // Check the results
                object RoutineResult = IntegrationObject.RoutineResult;
                if (RoutineResult != null)
                    System.Windows.Forms.MessageBox.Show("Script routine returned " + RoutineResult.ToString());
                else
                    System.Windows.Forms.MessageBox.Show("Script routine did not return any result");


                if (IntegrationObject.GetLastResultDescription().Status == 0)
                {
                    System.Windows.Forms.MessageBox.Show(this, "The test run finished successfully.");
                }
                if (IntegrationObject.GetLastResultDescription().Status == 1)
                {
                    System.Windows.Forms.MessageBox.Show(this, "Warning messages were posted to the test log.");
                }
                if (IntegrationObject.GetLastResultDescription().Status == 2)
                {
                    System.Windows.Forms.MessageBox.Show(this, "Error messages were posted to the test log.");
                }

            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                System.Windows.Forms.MessageBox.Show("An exception occurred: " + ex.Message);
            }
            finally
            {
                // Closes TestExecute
                TestExecuteManager.Quit();
                // Releases COM objects
                Marshal.ReleaseComObject(IntegrationObject);
                Marshal.ReleaseComObject(TestExecuteObject);
            }
        }
    }

Here is a sample script code which execution is initiated via COM. The VB or C# code described above calls the MyRoutine procedure which call the ReadData function to obtain data from a file:

JavaScript, JScript

function ReadData()
{
  var ForReading, FSO, MyFile, Cnt, i, Data, s;

  // Open the file
  ForReading = 1;
  FSO = new ActiveXObject("Scripting.FileSystemObject");
  MyFile = FSO.OpenTextFile("C:\\TempFile.txt", ForReading, false);

  // Read data from the file
  s = MyFile.ReadLine(); // We use only one line of data

  // Obtains data and stores them to an array
  Cnt = BuiltIn.GetCSVCount(s);
  Data = new Array(Cnt + 1);
  Data[0] = Cnt;
  for(i = 0; i < Cnt; i++)
    Data[i + 1] = BuiltIn.GetCSVItem(s, i);

  // Close the file
  MyFile.Close();

  // Returns the result
  return Data;
}

function MyRoutine()
{
  var Data, i, s;

  // Obtains data
  Data = ReadData();

  // Work with data
  s = "";
  for(i = 1; i <= Data[0]; i++)
  {
    Log.Message(Data[i]);
    s = s + Data[i] + ", ";
  }
  return s;
}

VBScript

Function ReadData
  ' Open file
  ForReading = 1
  Set FSO = CreateObject("Scripting.FileSystemObject")
  Set MyFile = FSO.OpenTextFile("C:\TempFile.txt", ForReading, False)

  ' Read data from the file
  s = MyFile.ReadLine ' We use only one line of data

  ' Obtains data and stores them to an array
  Cnt = BuiltIn.GetCSVCount(s)
  ReDim Data(Cnt + 1)
  Data(0) = Cnt
  For i = 0 To Cnt - 1
    Data(i + 1) = BuiltIn.GetCSVItem(s, i)
  Next

  ' Close the file
  MyFile.Close

  ' Returns the result
  ReadData = Data
End Function

Function MyRoutine
  ' Obtains data
  Data = ReadData

  ' Work with data
  MyRoutine = ""
  For i = 1 To Data(0)
    Log.Message Data(i)
    MyRoutine = MyRoutine & Data(i)) & ", "
  Next
End Function

DelphiScript

function ReadData();
var
  ForReading, FSO, MyFile, Cnt, i, Data, s : OleVariant;
begin
  // Open the file
  ForReading := 1;
  FSO := Sys.OleObject('Scripting.FileSystemObject');
  MyFile := FSO.OpenTextFile('C:\TempFile.txt', ForReading, false);
  // Read data from the file
  s := MyFile.ReadLine(); // We use only one line of data

  // Obtains data and stores them to an array
  Cnt := BuiltIn.GetCSVCount(s);
  Data := BuiltIn.CreateVariantArray(0, Cnt + 1);   Data[0] := Cnt;
  for i := 0 to Cnt - 1 do
    Data[i + 1] := BuiltIn.GetCSVItem(s, i);

  // Close the file
  MyFile.Close();
  // Returns the result
  Result := Data;
end;
  
function MyRoutine();
var
  Data, i : OleVariant;
begin
  // Obtains data
  Data := ReadData();

  // Work with data
  Result := '';
  for i := 1 to Data[0] do
  begin
    Log.Message(Data[i]);
    Result := Result + VarToStr(Data[i]) + ', ';
  end;
end;

C++Script, C#Script

function ReadData()
{
  var ForReading, FSO, MyFile, Cnt, i, Data;

  // Open the file
  ForReading = 1;
  FSO = new ActiveXObject("Scripting.FileSystemObject");
  MyFile = FSO["OpenTextFile"]("C:\\TempFile.txt", ForReading, false);   
  // Read data from the file
  s = MyFile["ReadLine"](); // We use only one line of data

  // Obtains data and stores them to an array
  Cnt = BuiltIn["GetCSVCount"](s);
  Data = new Array(Cnt + 1);
  Data[0] = Cnt;
  for(i = 0; i < Cnt; i++)
    Data[i + 1] = BuiltIn["GetCSVItem"](s, i);

  // Close the file
  MyFile["Close"]();

  // Returns the result
  return Data;
}

function MyRoutine()
{
  var Data, i, s;

  // Obtains data
  Data = ReadData();

  // Work with data
  s = "";
  for(i = 1; i <= Data[0]; i++)
  {
    Log["Message"](Data[i]);
    s = s + VarToStr(Data[i]) + ", ";
  }
  return s;
}

RunRoutineEx Example

The following sample demonstrates how to call a script routine from a Visual Basic or C# application using the RunRoutineEx method. The sample code of the called script routine follows the VB and C# samples:

Visual Basic

Sub MySub()
  Dim ParamArr(3)

  ' Initialize variables
  sProjectNameFileName = "C:\MyProjects\MyProject\MyProject.mds"
  sProjectName = "MyProject"
  sUnitName = "Unit1"
  sRoutineName = "MyRoutine"

  ParamArr(0) = "John Smith"
  ParamArr(1) = "[email protected]"
  ParamArr(2) = "5/5/2009"
  ParamArr(3) = "0000-1234-5678-9000"
  
  ' Creates the application object
  Set TestExecuteApp = CreateObject("TestExecute.TestExecuteApplication.15")

  ' Obtains the integration object
  Set IntegrationObject = TestExecuteApp.Integration

  ' Opens the project
  IntegrationObject.OpenProjectSuite sProjectNameFileName

  ' Checks whether the project was opened
  If Not IntegrationObject.IsProjectSuiteOpened Then
    MsgBox "The project suite was not opened."
    Exit Sub
  End If

   ' Needed to process errors
  On Error GoTo Err_Label

  ' Runs the routine
  IntegrationObject.RunRoutineEx(sProjectName, sUnitName, sRoutineName, ParamArr)

  ' Waits until the test run is over
  While IntegrationObject.IsRunning
    DoEvents
  Wend

  ' Check the results
  If aqObject.GetVarType(IntegrationObject.RoutineResult) <> vbEmpty Then
    MsgBox "Script routine result : " + IntegrationObject.RoutineResult
  Else
    MsgBox "Script routine result did not return any result."
  End If

  Set LastResult = IntegrationObject.GetLastResultDescription
  Select Case LastResult.Status
    Case 0: MsgBox "The test run finished successfully."
    Case 1: MsgBox "Warning messages were posted to the test log."
    Case 2: MsgBox "Error messages were posted to the test log."
  End Select

Err_Label:
  ' Process errors
  If Err.Number > 0 Then
    MsgBox "Cannot start the test. Reason: " + Err.Description, vbCritical, "Error"
  End If

  ' Quits TestExecute
  TestExecuteApp.Quit
End Sub

Visual C# Early Binding

// Add the following lines to your unit
using System.Runtime.InteropServices;
using TestComplete;

...

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Invokes TestExecute upon a button click
            TestFunc();
        }

        private void TestFunc()
        {
            const string TEProgID = "TestExecute.TestExecuteApplication.10";
            object TestExecuteObject = null;

            // Initialize variables
            string sProjectFileName = "C:\\Work\\MyProject\\MyProjectSuite.pjs";
            string sProjectName = "TestProject";
            string sUnitName = "Unit1";
            string sRoutineName = "MyRoutine";

            string[] Params = { "John Smith", "[email protected]", "05/05/2009", "0000-1234-5678-9000" };

            // Obtains access to TestExecute
            try
            {
                TestExecuteObject = Marshal.GetActiveObject(TEProgID);
            }
            catch
            {
                try
                {
                    TestExecuteObject = Activator.CreateInstance(Type.GetTypeFromProgID(TEProgID));
                }
                catch
                {
                }
            }

            if (TestExecuteObject == null) return;


            // Obtain the ITestCompleteCOMManager object
            TestComplete.ITestCompleteCOMManager TestExecuteManager = (TestComplete.ITestCompleteCOMManager)TestExecuteObject;
            // Obtain the Integration object
            TestComplete.ItcIntegration IntegrationObject = TestExecuteManager.Integration;

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

            // Loads the project suite
            IntegrationObject.OpenProjectSuite(sProjectFileName);

            // Checks whether the project suite was opened
            // If the project suite cannot be opened, closes TestExecute
            if (!IntegrationObject.IsProjectSuiteOpened())
            {
                System.Windows.Forms.MessageBox.Show("Could not open the project suite.");
                // Closes TestExecute
                TestExecuteManager.Quit();
                // Releases COM objects
                Marshal.ReleaseComObject(IntegrationObject);
                Marshal.ReleaseComObject(TestExecuteManager);
                Marshal.ReleaseComObject(TestExecuteObject);
                return;
            }

        // Save data to a temporary file
        System.IO.StreamWriter MyFile = new System.IO.StreamWriter("C:\\TempFile.txt");
        MyFile.WriteLine("John Smith,[email protected],05/05/2009,0000-1234-5678-9000");
        MyFile.Close();

            try
            {
                // Runs the routine
                IntegrationObject.RunRoutineEx(sProjectName, sUnitName, sRoutineName, Params);

                // Waits until testing is over
                while (IntegrationObject.IsRunning())
                    Application.DoEvents();

                // Check the results
                object RoutineResult = IntegrationObject.RoutineResult;
                if (RoutineResult != null)
                    System.Windows.Forms.MessageBox.Show("Script routine returned " + RoutineResult.ToString());
                else
                    System.Windows.Forms.MessageBox.Show("Script routine did not return any result");


                
            switch (IntegrationObject.GetLastResultDescription().Status)
            {
                case TestComplete.TC_LOG_STATUS.lsOk:
                   System.Windows.Forms.MessageBox.Show(this, "The test run finished successfully.");
                   break;
                case TestComplete.TC_LOG_STATUS.lsWarning:
                    System.Windows.Forms.MessageBox.Show(this, "Warning messages were posted to the test log.");
                    break;
                case TestComplete.TC_LOG_STATUS.lsError:
                    System.Windows.Forms.MessageBox.Show(this, "Error messages were posted to the test log.");
                    break;
            }

            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                System.Windows.Forms.MessageBox.Show("An exception occurred: " + ex.Message);
            }
            finally
            {
                // Closes TestExecute
                TestExecuteManager.Quit();
                // Releases COM objects
                Marshal.ReleaseComObject(IntegrationObject);
                Marshal.ReleaseComObject(TestExecuteManager);
                Marshal.ReleaseComObject(TestExecuteObject);
            }
        }
    }

Visual C# Late Binding

// Add the following lines to your unit
using System.Runtime.InteropServices;
using TestComplete;

...

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Invokes TestExecute upon a button click
            TestFunc();
        }

        private void TestFunc()
        {
            const string TEProgID = "TestExecute.TestExecuteApplication.10";
            object TestExecuteObject = null;

            // Initialize variables
            string sProjectFileName = "C:\\Work\\MyProject\\MyProjectSuite.pjs";
            string sProjectName = "TestProject";
            string sUnitName = "Unit1";
            string sRoutineName = "MyRoutine";

            string[] Params = { "John Smith", "[email protected]", "05/05/2009", "0000-1234-5678-9000" };

            // Obtains access to TestExecute
            try
            {
                TestExecuteObject = Marshal.GetActiveObject(TEProgID);
            }
            catch
            {
                try
                {
                    TestExecuteObject = Activator.CreateInstance(Type.GetTypeFromProgID(TEProgID));
                }
                catch
                {
                }
            }

            if (TestExecuteObject == null) return;


            // Obtains Integration object during runtime
            dynamic IntegrationObject = TestExecuteObject.Integration;

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

            // Loads the project suite
            IntegrationObject.OpenProjectSuite(sProjectFileName);

            // Checks whether the project suite was opened
            // If the project suite cannot be opened, closes TestExecute
            if (!IntegrationObject.IsProjectSuiteOpened())
            {
                System.Windows.Forms.MessageBox.Show("Could not open the project suite.");
                // Closes TestExecute
                TestExecuteObject.Quit();
                // Releases COM objects
                Marshal.ReleaseComObject(IntegrationObject);
                Marshal.ReleaseComObject(TestExecuteObject);
                return;
            }

        // Save data to a temporary file
        System.IO.StreamWriter MyFile = new System.IO.StreamWriter("C:\\TempFile.txt");
        MyFile.WriteLine("John Smith,[email protected],05/05/2009,0000-1234-5678-9000");
        MyFile.Close();

            try
            {
                // Runs the routine
                IntegrationObject.RunRoutineEx(sProjectName, sUnitName, sRoutineName, Params);

                // Waits until testing is over
                while (IntegrationObject.IsRunning())
                    Application.DoEvents();

                // Check the results
                object RoutineResult = IntegrationObject.RoutineResult;
                if (RoutineResult != null)
                    System.Windows.Forms.MessageBox.Show("Script routine returned " + RoutineResult.ToString());
                else
                    System.Windows.Forms.MessageBox.Show("Script routine did not return any result");


                if (IntegrationObject.GetLastResultDescription().Status == 0)
                {
                    System.Windows.Forms.MessageBox.Show(this, "The test run finished successfully.");
                }
                if (IntegrationObject.GetLastResultDescription().Status == 1)
                {
                    System.Windows.Forms.MessageBox.Show(this, "Warning messages were posted to the test log.");
                }
                if (IntegrationObject.GetLastResultDescription().Status == 2)
                {
                    System.Windows.Forms.MessageBox.Show(this, "Error messages were posted to the test log.");
                }

            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                System.Windows.Forms.MessageBox.Show("An exception occurred: " + ex.Message);
            }
            finally
            {
                // Closes TestExecute
                TestExecuteManager.Quit();
                // Releases COM objects
                Marshal.ReleaseComObject(IntegrationObject);
                Marshal.ReleaseComObject(TestExecuteObject);
            }
        }
    }

Here is sample script code where the execution is initiated via COM. The VB or C# code described above calls the MyRoutine procedure.

JavaScript, JScript

function MyRoutine(Param1, Param2, Param3, Param4)
{
  Log.Message("Param1: " + Param1);
  Log.Message("Param2: " + Param2);
  Log.Message("Param3: " + Param3);
  Log.Message("Param4: " + Param4);

  return Param1 + ", " + Param2 + ", " + Param3 + ", " + Param4
}

VBScript

Function MyRoutine(Param1, Param2, Param3, Param4)
  Log.Message "Param1: " & Param1
  Log.Message "Param2: " & Param2
  Log.Message "Param3: " & Param3
  Log.Message "Param4: " & Param4

  MyRoutine = Param1 & ", " & Param2 & ", " & Param3 & ", " & Param4
End Function

DelphiScript

function MyRoutine(Param1, Param2, Param3, Param4);
begin
  Log.Message('Param1: ' + Param1);
  Log.Message('Param2: ' + Param2);
  Log.Message('Param3: ' + Param3);
  Log.Message('Param4: ' + Param4);

  Result := Param1 + ', ' + Param2 + ', ' + Param3 + ', ' + Param4;
end;

C++Script, C#Script

function MyRoutine(Param1, Param2, Param3, Param4)
{
  Log["Message"]("Param1: " + Param1);
  Log["Message"]("Param2: " + Param2);
  Log["Message"]("Param3: " + Param3);
  Log["Message"]("Param4: " + Param4);

  return Param1 + ", " + Param2 + ", " + Param3 + ", " + Param4
}

See Also

Working With TestExecute via COM - Overview

Highlight search results