Search This Blog

Wednesday, December 8, 2010

Functionality:- Code for Finding and replacing the word/phase in QC test cases in Test plan.

This script helps us in finding and replacing all occurrences of a word/phrase with the given word/phrase in Quality Center test cases in Test Plan (Description and Expected Result fields of Design Steps, to be precise)

Script needs the following inputs:
1.       QC Server name (URL), Domain, Project, Username and Password
2.       Test case name or Test case folder name
3.       Search Item (word/phrase)
4.       Replace Item (word/phrase)

#######################################################################3
'==============================================
' Utility name: Find and Replace
'==============================================


' Get the QC Connection parameters from the user
 Set oShell = CreateObject( "WScript.Shell" )
 user = oShell.ExpandEnvironmentStrings("%UserName%")


 userName = InputBox("Enter the username", "Quality Center Username", user)
 userPassword = InputBox("Enter the password", "Quality Center Password")
 qcDomain = InputBox("Enter the domain", "Quality Center Domain", "WQA")
 qcProject = InputBox("Enter the project", "Quality Center Project", "FSDD_SDD")

' Establish the connection
 Set QCConnection = CreateObject("TDApiOle80.TDConnection")
 QCConnection.InitConnectionEx "http://wcpappa018609/qcbin"

 QCConnection.login userName, userPassword
 QCConnection.Connect qcDomain, qcProject


' Testcase and QC Path variables
 Dim tcName
 Dim qcPath
 
 qcPath = InputBox("Enter the absolute QC folder path", "QC Folder Path", "Subject\WAVES\Jan 2009 Reqs\1. Add Bank Draw Number")
 tcName = InputBox("Enter the testcase name: (leave blank for all testcases)", "Testcase Name", "1.2 Add Bank Draw Number")

' Search and replace variables
 Dim searchItem ' Search Sting
 Dim repItem ' Replace string

 searchItem = InputBox("Enter the search item", "Search Item")
 repItem = InputBox("Enter the replace item", "Replace Item")

' Initialize the QC connection variables
 Set tFact = QCConnection.TestFactory
 Set tFilter = tFact.Filter

 tFilter.Filter("TS_SUBJECT") = """" & qcPath & """" ' Find the QC Path
 ' tFilter.Filter("TS_NAME") = ""

' Find the testcase(s) in the given path
 If IsEmpty(tcName) = False Then
  tFilter.Filter("TS_NAME") = """" & tcName & """"
 End If

' Execute the filter
 Set tList = tFact.NewList(tFilter.Text)
 
' Create the log file setup
 Set oFSO = CreateObject("Scripting.FileSystemObject")
 Set oLogFile = oFSO.CreateTextFile("UpdateActualResult.log", True)


' Navigate through all the testcases that matched the search criteria (usually just one testcase)
 For i = 1 to tList.Count 
  
  Set myTest = tList.Item(i)
  
  oLogFile.WriteLine vbCrLf & "Log for test case: " & myTest.name

  ' Check whether Version Control is enabled for the test
  On Error Resume Next
  Set objVCS = myTest.VCS
  On Error Goto 0
 
  If IsObject(objVCS) Then
   ' Check out the test, if it not already checked out
   If objVCS.IsLocked = False Then
    objVCS.CheckOut "", "", True

   ' Verify if the test is checked out by other user - if Yes, exit test, if No, check out the test
   ElseIf objVCS.IsLocked AND userName <> objVCS.LockedBY Then
    oLogFile.WriteLine "WARNING: " & myTest.name & " is locked by: " & objVCS.LockedBy
    ExitTest
   End If
  End If

  Set dFact = myTest.DesignStepFactory
  Set dStepsList = dFact.NewList("")
 
  ' Navigate through all the teststeps and do the replacements
  Set oRegEx = New RegExp
  oRegEx.Pattern = searchItem
 
  For Each item in dStepsList
   ' Replace in the Step Description & Expected Result
   newStepDesc = oRegEx.Replace(item.StepDescription, repItem)
   newExpResult = oRegEx.Replace(item.StepExpectedResult, repItem)
 
   ' Print the log
   If newStepDesc <> item.StepDescription OR newExpResult <> item.StepExpectedResult Then
    oLogFile.WriteLine vbTab & "Replacement in Testcase " & myTest.Name & " step number - " & item.Order + 1
    item.StepDescription = newStepDesc
    item.StepExpectedResult = newExpResult
 
    item.Post
   End If
  Next

 ' Clear off the variables
 Set dStepsList = Nothing
 Set dFact = Nothing

 ' Check whether test is VCS enabled - if Yes, then check-in the script, if NO, just unlock the test
 If IsObject(objVCS) Then 
  objVCS.CheckIn "", ""
 End If

 ' Unlock the test
 If myTest.IsLocked Then
  myTest.UnLockObject
 End If
Next

' Clear off the variables
 Set tList = Nothing
 Set tFilter = Nothing
 Set tFact = Nothing

' Disconnect from QC
 QCConnection.Disconnect
 QCConnection.Logout
 QCConnection.ReleaseConnection
 
 
' End the script
 MsgBox "All the replacements have been done!  Press Refresh to see the results."

####################################################################################

No comments:

Post a Comment