Below code will execute tests in a test set in QC based on name of the test set. The code can run all the scripts in test set, only passed test script, only failed test script. This can be manipulated further based on requirement. Also an html file with test status will be displayed once test set execution is completed.
UserName = InputBox("Enter the user name for QC Login", "QC UserName")
Password = InputBox("Enter the password for QC Login", "QC Password")
strExecFlag = InputBox("Enter the option for test execution" & vbcrlf & "Valid Values - P, A,F" & vbcrlf & "A: Run All test Scripts"& vbcrlf & "F: ExecuTe script not passed Only"& vbcrlf & "P: Execute Passed Test Scripts","Test Execution Option")
'Return the TDConnection object.
Set QCConnection = CreateObject("TDApiOle80.TDConnection")
QCConnection.InitConnectionEx QCurl
QCConnection.login UserName, Password
QCConnection.Connect DomainName, ProjectName
Set TSetFact = QCConnection.TestSetFactory
Set tsTreeMgr = QCConnection.TestSetTreeManager
Dim qtApp 'As QuickTest.Application ' Declare the Application object variable
Dim qtLibraries 'As QuickTest.TestLibraries ' Declare a test's libraries collection variable
Set tsFolder = tsTreeMgr.NodeByPath(<QC Test Set Path>) ' test set starts with "Root"
Set tsList = tsFolder.FindTestSets(<TestSetName>) 'Testset name
strTable = ""
Set theTestSet = tsList.Item(1)
For Testset_Count=1 to tsList.Count
Set theTestSet = tsList.Item(Testset_Count)
Set TSTestFact = theTestSet.TSTestFactory
TSName = theTestSet.Name
Set TestSetTestsList = TSTestFact.NewList("")
For Each theTSTest In TestSetTestsList
TestName = theTSTest.Test.Name
TestScript = <Test Script path in QC> & TestName
TestStatus = theTSTest.Status
If ((TestStatus ="Passed" and ucase(strExecFlag) = "P") OR (TestStatus <>"Passed" and ucase(strExecFlag) = "F") or (ucase(strExecFlag) = "A")) Then
Set qtApp = CreateObject("QuickTest.Application")
If qtApp.launched <> True then
qtApp.Launch
End If
qtApp.Visible = "true"
If Not qtApp.TDConnection.IsConnected Then
qtApp.TDConnection.Connect QCurl,DomainName,ProjectName,UserName,Password,False
End If
' Create the Application object
qtApp.Open TestScript, True ' Open the test in read-only mode
Set qtTest = qtApp.Test
Set qtResultsOpt = CreateObject("QuickTest.RunResultsOptions")
' Create the Run Results Options object
' New Run Results in Quality Center option
qtResultsOpt.TDTestInstance = 1
qtResultsOpt.TDRunName= "Run_" & Month(Now) & "-" & Day(Now) & "_" & Hour(Now) & "-" & Minute(Now) & "-" & Second(Now)
qtResultsOpt.TDTestSet = TestSetPath ' Path to the Test Set where we should save the results
Set fso=createobject("Scripting.FileSystemObject")
If fso.FolderExists("C:\Res1") Then
fso.DeleteFolder("C:\Res1")
End If
qtResultsOpt.ResultsLocation = "C:\Res1"
qtTest.Run qtResultsOpt,True
TestStatus = qtTest.LastRunResults.Status
qtTest.Close
qtApp.quit
Set qtApp = Nothing strTable = strTable & "<tr><td>"&TestName&"</td><td>"&TestStatus&"</td></tr>"
End If
Next
Next
Set objFSO = CreateObject("scripting.filesystemObject")
If (objFSO.FolderExists("c:\Temp") = False) Then
objFSO.CreateFolder("c:\Temp")
End If
strTable = "<html><h1>Test Results<h1><table border =""""2""""<tr><td>TestCaseName</td><td>Test Execution Status</td></tr>" & strTable &"</table></html>"
Set objFl = objFSO.CreateTextFile("c:\Temp\Test.html")
objFl.Write strTable
Set objFl = nothing
CreateObject("WScript.Shell").Run "c:\Temp\Test.html"