HasUnit Property

Applies to TestComplete 14.92, last modified on September 16, 2021

Description

Use the HasUnit property to determine whether the TestComplete project represented by the ProjectIntegration object contains the specified script unit.

Declaration

ProjectIntegrationObj.HasUnit(FileName)

Read-Only Property Boolean
ProjectIntegrationObj An expression, variable or parameter that specifies a reference to a ProjectIntegration object
FileName [in]    Required    String    

Applies To

The property is applied to the following object:

Parameters

The property has the following parameter:

FileName

Specifies the fully qualified name of the script unit file.

Property Value

True, if the given project contains the specified script unit; False otherwise.

Example

The following example demonstrates how you can work with script units of the project currently opened in TestComplete.

Visual Basic

Sub Test()

  Dim TestCompleteApp As TestCompleteApplication
  Dim IntegrationObject As ItcIntegration
  Dim LastResult As ItcIntegrationResultDescription

  ' Creates the application object
  Set TestCompleteApp = CreateObject("TestComplete.TestCompleteApplication.14")

  ' Obtains the integration object
  Set IntegrationObject = TestCompleteApp.Integration

  ' Opens the project
  ProjectPath = "C:\Work Folder\TestSuite\TestSuite.pjs"
    IntegrationObject.OpenProjectSuiteEx (ProjectPath)

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

    ' Obtains the ProjectIntegration object
    Set ProjectIntegration = IntegrationObject.Project("TestProject_VB")
    ' Specifies the script unit file
    UnitPath = "C:\Work Folder\TestSuite\TestProject_VB\Script\Test4_VB.svb"
    ' Checks whether the project contains the specified script unit
    If ProjectIntegration.HasUnit(UnitPath) Then
      ' If the project contains the unit
      ' Displays an informative message
      MsgBox "The project already contains the specified script unit"
    Else
      ' If the project does not contain the unit
      ' Adds the unit to the project
      ProjectIntegration.AddUnit (UnitPath)

      …

      ' Removes the added unit from the project
      ProjectIntegration.RemoveUnit (UnitPath)
    End If

  Err_Label:
    ' Process errors
    If Err.Number <> 0 Then
      MsgBox Err.Description, vbCritical, "Error"
    End If

  ' Closes TestComplete
  TestCompleteApp.Quit

End Sub

C#

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


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

  private void btn_Connect_Click(object sender, EventArgs e)
  {
    const string TCProgID = "TestComplete.TestCompleteApplication.14";
    string ProjectSuitePath = "C:\\Work Folder\\TestSuite\\TestSuite.pjs";
    string ProjectPath = "C:\\Work Folder\\TestSuite\\TestProject_CS";
    string ProjectName = "TestProject_CS";
    object TestCompleteObject = null;
    string FilePath;

    // Obtains access to TestComplete
    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;
    // Loads the project suite
    IntegrationObject.OpenProjectSuiteEx(ProjectSuitePath);
    try
    {
      // Obtains ProjectIntegration object
      TestComplete.ItcProjectIntegration Project = IntegrationObject.get_Project(ProjectName);

      // Shows the Open File dialog allowing to select a script unit to add to the project
      OpenFileDialog.InitialDirectory = ProjectPath + "\\Script";
      OpenFileDialog.Filter = "C#Script files|*.scs";
      if (OpenFileDialog.ShowDialog() == DialogResult.OK)
      {
        FilePath = OpenFileDialog.FileName;
        if (Project.get_HasUnit(FilePath))
          // If the project already contains the selected unit
          // Displays the informative message
          MessageBox.Show("The project already contains this script unit");
        else
        {
          // If the project does not contain the selected unit
          // Adds the unit to the project
          Project.AddUnit(FilePath);

          …

          // Removes the added unit from the project
          Project.RemoveUnit(FilePath);
        }
      }
    }
    catch (System.Runtime.InteropServices.COMException ex)
    {
      System.Windows.Forms.MessageBox.Show("An exception occurred: " + ex.Message);
    }
    finally
    {
      // Closes TestComplete
      TestCompleteManager.Quit();
      // Releases COM objects
      Marshal.ReleaseComObject(IntegrationObject);
      Marshal.ReleaseComObject(TestCompleteManager);
      Marshal.ReleaseComObject(TestCompleteObject);
    }
  }
}

See Also

Working With TestComplete via COM - Overview
AddUnit Method
RemoveUnit Method

Highlight search results