Detecting Test Complete open via COM in VB
Can anybody tell me how to detect if Test Complete is already open via COM?
I have read through the help topic "Working With TestComplete via COM" and it mentions using the line below if Test Complete is already open:
TestCompleteObject = GetObject(,"TestComplete.TestCompleteApplication")
Here is the code (VB) I am using, which is the sample code supplied by SmartBear with a few modifications.
Imports TestComplete
Module Module1Sub Main()
Dim ProjectSuiteArgument As String = "/pjs="
Dim ProjectSuiteName As String = ""
Dim ProjectArgument As String = "/project="
Dim ProjectName As String = ""
Dim TestItemArgument As String = "/testitem="
Dim TestItemName As String = ""
Dim HelpArgument As String = "/?"
Dim HelpName As String = ""For Each s As String In My.Application.CommandLineArgs
If s.ToLower.StartsWith(ProjectSuiteArgument) Then
ProjectSuiteName = s.Remove(0, ProjectSuiteArgument.Length)
End If
If s.ToLower.StartsWith(ProjectArgument) Then
ProjectName = s.Remove(0, ProjectArgument.Length)
End If
If s.ToLower.StartsWith(TestItemArgument) Then
TestItemName = s.Remove(0, TestItemArgument.Length)
End If
If s.ToLower.StartsWith(HelpArgument) Then
HelpName = "?"
End If
NextDim TestCompleteApp As TestCompleteApplication, IntegrationObject As ItcIntegration, LastResult As ItcIntegrationResultDescription
If HelpName <> "" Then
Console.WriteLine("To run...")
Console.WriteLine("1. Entire Project Suite: Supply Project Suite Filename and Path only using /pjs=")
Console.WriteLine("2. Entire Project: Supply #1 and Project (/project=) values.")
Console.WriteLine("3. Single Test Item: Supply #1, #2 and Test Item Name (/testitem=) values.")
Console.WriteLine("------------------------------------")
Console.WriteLine("/pjs= - Project Suite Filename and Path (eg: C:\TestComplete\MyProjectSuite.pjs)")
Console.WriteLine("/project= - Project Name")
Console.WriteLine("/testitem= - Test Item Name")
End IfIf HelpName = "" Then
' Creates the application object
TestCompleteApp = CreateObject("TestComplete.TestCompleteApplication")' Obtains the integration object
IntegrationObject = TestCompleteApp.Integration' Opens the project
IntegrationObject.OpenProjectSuite(ProjectSuiteName)' Checks whether the project suite was opened
' If the project suite cannot be opened, closes TestComplete
If Not IntegrationObject.IsProjectSuiteOpened Then
MsgBox("The project suite was not opened.")
' Closes TestComplete
TestCompleteApp.Quit()
Exit Sub
End If' Needed to process errors
Err.Clear()
On Error GoTo Err_Label' Starts the project test item run
If ProjectName <> "" Then
If TestItemName <> "" Then
IntegrationObject.RunProjectTestItem(ProjectName, TestItemName)
End If
End If' Starts a Project run
If TestItemName = "" Then
If ProjectName <> "" Then
IntegrationObject.RunProject(ProjectName)
End If
End If' Starts a Project Suite Run
If TestItemName = "" Then
If ProjectName = "" Then
IntegrationObject.RunProject(ProjectSuiteName)
End If
End If' Waits until the test is over
While IntegrationObject.IsRunning
DoEvents()
End While' Exports the results
IntegrationObject.ExportResults("C:\Logs\Test_Results.mht", True)LastResult = IntegrationObject.GetLastResultDescription
Select Case LastResult.Status
Case 0 : CreateObject("WScript.Shell").Popup("The test run finished successfully..", 1, "The test run finished successfully.")
Case 1 : CreateObject("WScript.Shell").Popup("Warning messages were posted to the test log..", 1, "Warning messages were posted to the test log.")
Case 2 : CreateObject("WScript.Shell").Popup("Error messages were posted to the test log.", 1, "Error messages were posted to the test log.")
End SelectErr_Label:
' Process errors
If Err.Number <> 0 Then
MsgBox("Cannot start the test. Reason: " + Err.Description, vbCritical, "Error")
End If' Closes TestComplete
TestCompleteApp.Quit()
End If
End SubPrivate Sub DoEvents()
End Sub
Private Sub ParseCommandLineArgs()
Dim inputArgument As String = "/input="
Dim inputName As String = ""For Each s As String In My.Application.CommandLineArgs
If s.ToLower.StartsWith(inputArgument) Then
inputName = s.Remove(0, inputArgument.Length)
End If
Next
End SubEnd Module