Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

Script to execute specific QTP tests from Test Set in Quality Center using VBScript

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"  

11 comments:

  1. Hey...nice script. I got this to run but a few questions/comments:

    What is the purpose of
    If fso.FolderExists("C:\Res1") Then
    fso.DeleteFolder("C:\Res1")
    End If
    qtResultsOpt.ResultsLocation = "C:\Res1"

    The script fails when tests in the test sets come from different folders. Can we get this path from the test set?

    The HTML test results always show NO Run for me even though they ran successfully?

    Thanks!

    Matt



    ReplyDelete
  2. Hi Matt,
    In the above script, we are saving the test results in QC Test Set , and C:\res1 is the temporary result location folder,we need to define temporary result location.

    The location of test set from where to run script comes from

    Set tsFolder = tsTreeMgr.NodeByPath()'' test set starts with "Root"
    Set tsList = tsFolder.FindTestSets() 'Testset name

    You can loop the code starting from above line and making necessary changes for running in multiple folders.

    TestStatus = theTSTest.Status will be giving No run value, instead try the updated script now by me with status updated based on qtTest.LastRunResults.Status.

    Also you might be noticing test execution not reflected in test set. this is due to reason that In QC , Field 'Draft Run' in QC Test Run is set as 'Y'. If test is executed directly from QC and results to be saved in QC.

    To overcome, this issue, at the beginning of script or the initialization library/function , please add the below lines:

    On Error Resume Next
    Set objRun = QCUtil.CurrentRun
    objRun.Field("RN_DRAFT") = "N".

    Hope some of the issues are resolved.

    Thanks
    Nitin

    ReplyDelete
  3. I tried setting QCUtil.CurrentRun.Field("RN_DRAFT") = "N", but the change does reflect in ALM.
    Should this set before launching ALM?


    Suresh.

    ReplyDelete
  4. This Code wont support OTA framework It shows error as Object required.

    Thanks,
    Raji

    ReplyDelete
  5. Hi, Please go through my comment above

    To overcome, this issue, at the beginning of script or the initialization library/function , please add the below lines:

    On Error Resume Next
    Set objRun = QCUtil.CurrentRun
    objRun.Field("RN_DRAFT") = "N".

    Hope some of the issues are resolved.

    ReplyDelete
  6. TestScript = & TestName what does the "TestScript = " mean in this line?

    ReplyDelete
    Replies
    1. Hi Sharmin,

      TestScript gives the path of testcase in the testset to be run based on name of test case

      Delete
  7. It's giving error as cannot open test in line : qtApp.Open TestScript, True

    ReplyDelete
  8. I want to add Function Libraries and Object Repositories at the start of test automation.
    I am using below code to add FL from my local drive folder. How can I achieve the same functionality if FL is stored in ALM

    'Set QTP object
    Set objApp = CreateObject("QuickTest.Application")

    'Launch QTP
    objApp.Visible = True

    Set objLib = objApp.Test.Settings.Resources.Libraries

    varFLPath = "FL path\"
    commonFunctionLibraryPath = varFLPath & "Common Function Library.qfl"

    'If the library is not already associated with the test case, associate it..
    If objLib.Find(commonFunctionLibraryPath) = -1 Then
    objLib.Add commonFunctionLibraryPath, 1 ' Associate the library to the test case
    End If


    varORPath = "OR PAth"
    varSAPORPath = varORPath & "mytsr.tsr"

    Dim qtObjRep 'Declare the object repository object

    iActionCount = objApp.Test.Actions.Count

    For i = 1 To iActionCount

    sActionName = objApp.Test.Actions(i).Name

    Set qtObjRep = objApp.Test.Actions(sActionName).ObjectRepositories 'Get the object repository collection collection object for the test action
    qtObjRep.Add varSAPORPath,1
    Next

    msgbox "Completed"

    ReplyDelete