You are on page 1of 10

Test Automation

This blog aims to detail solutions to everyday challenges involved in test automation using QTP, Quality Center (QC), Excel and Open Source Tools.
TUESDAY, 15 NOVEMBER 2011

Treeview for Quality Center Test Plan Using OTA & OTA Code Examples
I've uploaded a spreasheet containing examples of how to interact with the Quality Center OTA Test Plan and Test Lab Modules. You can download the spreadsheet here. The examples attached to the buttons show a tree view selection dialog for the Test Plan.

The VBA Code Included also has examples for other OTA interactions as follows: Test Plan (QCTestPlanCommonFunctions) TestPlanCopyPasteTest - Uses OTA to copy a Test Case in the Test Plan. TestPlanCreateFolderStructure - Uses OTA to create a folder structure in the Test Plan. TestPlanDoesPathExist - Uses OTA to return True / False if a directory / path exists in the Test Plan TestPlanDoesTestExist - Uses OTA to return True / False if a test exists in the Test Plan. TestPlanFindTest - Uses OTA to return a test for a given path and/or it's subfolders

TestPlanGetSubjectNode - Uses OTA to return a Folder as a Subject Node object Test Lab(QCTestLabCommonFunctions) TestLabAddTestToTestSet - Uses OTA to add a test to a test set in the test lab. TestLabCreateDirectoryStructure - Uses OTA to create a directory structure in the test lab TestLabCreateTestSet - Uses OTA to create a test set in a directory TestLabDoesFolderExist - Uses OTA to Returns True / False as to whether a folder exists TestLabGetFolderByPath - Uses OTA to returns a TestSetFolder object for a given path TestLabGetTestSet - Uses OTA to return a TestSet for a given path

A couple of things: This code can sometimes fail after being run several times. I can't find the reason why, but usually closing Excel and reopening it fixes the issues. In your code, if you have a loop that creates a folder and then uses "TestLabGetFolderByPath" to find it, you may find it fails. Inserting a call to the function "RebootQCCon" resolves this.

QC OTA - Test Plan - 2


- Find Test in Test Plan - Return a SubjectNode object for a given path in the Test Plan Function TestPlanFindTest(ByVal strTestName As String, ByVal strFolderToSearchPath As String, ByVal SearchChildFolders As Boolean, Optional ByVal strTestType As String, Optional blnSilentMode As Boolean) As Test Dim oParentNode As SubjectNode Dim SubjectNodeList As List Dim oSubjectNode As SubjectNode Dim intMatchCount As Integer: intMatchCount = 0 Dim TestFact As TestFactory Dim oReturnValue As Test Dim TestFilter As TDFilter Dim TestList As List Dim oTest As Test Dim blnTypedMatched As Boolean: blnTypedMatched = True

Set oParentNode = TestPlanGetSubjectNode(strFolderToSearchPath) Set TestFact = tdc.TestFactory 'If there was an error getting the parent node then exit If (oParentNode Is Nothing) Then Set TestPlanFindTest = Nothing Exit Function End If 'See if the parent folder has any tests that match the name we are looking for Set TestFilter = TestFact.Filter TestFilter.Filter("TS_SUBJECT") = Chr(34) & oParentNode.Path & Chr(34) Set TestList = TestFact.NewList(TestFilter.Text) For Each oTest In TestList 'Debug.Print "Test Name='" & oTest.Name & "' Test Type=" & oTest.Type If (UCase(oTest.Name) = UCase(strTestName)) Then 'See if we should match the type - by default it is set to matched (true) If (strTestType) <> "" Then If (oTest.Type = strTestType) Then blnTypedMatched = True Else blnTypedMatched = False End If End If If blnTypedMatched Then intMatchCount = intMatchCount + 1 Set oReturnValue = oTest End If End If Next 'Now check to see if we wanted to search child folders? If so search them If SearchChildFolders Then 'Get all the child folders of the parent folder Set SubjectNodeList = oParentNode.FindChildren("", False, "") If Not (SubjectNodeList Is Nothing) Then For Each oSubjectNode In SubjectNodeList 'Debug.Print oSubjectNode.Path Set TestFilter = TestFact.Filter TestFilter.Filter("TS_SUBJECT") = Chr(34) & oSubjectNode.Path & Chr(34) Set TestList = TestFact.NewList(TestFilter.Text) For Each oTest In TestList 'Debug.Print "Test Name='" & oTest.Name & "' Test Type=" & oTest.Type If (UCase(oTest.Name) = UCase(strTestName)) Then 'See if we should match the type - by default it is set to matched (true) If (strTestType) <> "" Then If (oTest.Type = strTestType) Then blnTypedMatched = True Else

blnTypedMatched = False End If End If If blnTypedMatched Then intMatchCount = intMatchCount + 1 Set oReturnValue = oTest End If End If Next Next End If End If

'Clean Up Set oParentNode = Nothing Set SubjectNodeList = Nothing Set oSubjectNode = Nothing Set TestFact = Nothing Set TestFilter = Nothing Set TestList = Nothing Set oTest = Nothing

'Now return a value Select Case intMatchCount Case 0 If Not blnSilentMode Then MsgBox "Error: The Test could not be found with the parameters: " & Chr(10) & _ "Test Name:" & strTestName & Chr(10) & _ "Parent Folder Path:" & strFolderToSearchPath & Chr(10) & _ "Test Type:" & strTestType & Chr(10) & _ "Search child folders?:" & SearchChildFolders, vbExclamation + vbOKOnly, "TestPlanFindTest" End If Set TestPlanFindTest = Nothing Case 1 Set TestPlanFindTest = oReturnValue Case Else If Not blnSilentMode Then MsgBox "Error: A total of " & intMatchCount & " tests were found with the following criteria: " & Chr(10) & _ "Test Name:" & strTestName & Chr(10) & _ "Parent Folder Path:" & strFolderToSearchPath & Chr(10) & _ "Test Type:" & strTestType & Chr(10) & _ "Search child folders?:" & SearchChildFolders, vbExclamation + vbOKOnly, "TestPlanFindTest" End If

Set TestPlanFindTest = Nothing End Select End Function -----------------------------------------------------------------------------------------------Function TestPlanGetSubjectNode(ByVal strPath As String) As SubjectNode Dim TreeMgr As TreeManager Dim SubjRoot As SubjectNode Dim SubjectNodeList As List Dim oSubjectNode As SubjectNode

Set TreeMgr = tdc.TreeManager Set SubjRoot = TreeMgr.TreeRoot("Subject") 'Trim any trailing \ If Right(strPath, 1) = "\" Then strPath = Left(strPath, Len(strPath) - 1) If UCase(strPath) = "SUBJECT" Then Set TestPlanGetSubjectNode = SubjRoot Set TreeMgr = Nothing Set SubjRoot = Nothing Exit Function End If

Set SubjectNodeList = SubjRoot.FindChildren("", False, "") For Each oSubjectNode In SubjectNodeList If UCase(oSubjectNode.Path) = UCase(strPath) Then Set TestPlanGetSubjectNode = oSubjectNode Exit Function End If Next MsgBox "Test Plan path not found: " & strPath, vbExclamation + vbOKOnly, "TestPlanGetSubjectNode Error" Set TestPlanGetSubjectNode = Nothing Set TreeMgr = Nothing Set SubjRoot = Nothing Set SubjectNodeList = Nothing Set oSubjectNode = Nothing End Function

Here are some amazing functions that can be reused wrt QC OTA for Test Plan module:

- Copy paste within Test Plan - Create folder structure within Test Plan - Does a particular path exist in Test Plan - Does a [particular test exist in Test Plan Function TestPlanCopyPasteTest(strSourceFolderPath, strDestFolderPath, strSourceTestName, strNewTestName) As Boolean 'Copy a test, including design steps and parameters. ' For example: ' CopyPasteTest "Subject\TestFolder1", "Subject\TestFolder2", "Test1" ' Copies Test1 to TestFolder2 Dim sourceFolder As SubjectNode Dim destFolder As SubjectNode Dim treeMng As TreeManager Dim iscp As ISupportCopyPaste Dim clipboard As String Dim oSourceTest As Test Dim oNewTest As Test 'Check that the source file exists If Not TestPlanDoesTestExist(strSourceTestName, strSourceFolderPath, False, "") Then TestPlanCopyPasteTest = False MsgBox "Source test does not exist: " & strSourceFolderPath & "\" & strSourceTestName, vbExclamation + vbOKOnly, "TestPlanCopyPasteTest" Exit Function End If 'Check that the destination test does not exist If TestPlanDoesTestExist(strNewTestName, strDestFolderPath, False, "") Then TestPlanCopyPasteTest = False MsgBox "Destination test already exists: " & strDestFolderPath & "\" & strNewTestName, vbExclamation + vbOKOnly, "TestPlanCopyPasteTest" Exit Function End If 'On Error GoTo errCondition Set sourceFolder = TestPlanGetSubjectNode(strSourceFolderPath) Set testF = sourceFolder.TestFactory ' Find the test ID. Set oSourceTest = TestPlanFindTest(strSourceTestName, strSourceFolderPath, False, "", True) ' Copy the source test. Set iscp = testF clipboard = iscp.CopyToClipBoard(oSourceTest.ID, 0, "") ' Paste the test in the destination folder. Set destFolder = TestPlanGetSubjectNode(strDestFolderPath) Set testF = destFolder.TestFactory

Set iscp = testF iscp.PasteFromClipBoard clipboard, destFolder.NodeID 'Now it's pasted, rename it 'Reboot the QC connection as we get some failures around this point Call RebootQCCon Set oNewTest = TestPlanFindTest(strSourceTestName, strDestFolderPath, False, "") oNewTest.Name = strNewTestName oNewTest.Post ' Clean up. Set iscp = Nothing Set treeMng = Nothing Set sourceFolder = Nothing Set destFolder = Nothing Set oNewTest = Nothing Set oSourceTest = Nothing TestPlanCopyPasteTest = True Exit Function errCondition: MsgBox "Error: " & Err.Description, vbExclamation + vbOKOnly, "TestPlanCopyPasteTest" TestPlanCopyPasteTest = False End Function

----------------------------------------------------Sub TestPlanCreateFolderStructure(strFullPath) Dim arrFolders() As String Dim n As Integer Dim strParentPath As String Dim strThisPath As String Dim oParentFolder As SubjectNode 'First of all, does the folder structure already exist? If TestPlanDoesPathExist(strFullPath) Then Exit Sub 'Now split the path up into its individual folders arrFolders = Split(strFullPath, "\") 'does the first element contain subject? if not, exit If UCase(arrFolders(0)) <> "SUBJECT" Then MsgBox "Error: The first element in the folder path must be 'Subject'", vbExclamation + vbOKOnly, "TestPlanCreateFolderStructure" Exit Sub End If

'iterate the array For n = 0 To UBound(arrFolders) 'Create the folder if it doesn't exist If strParentPath <> "" Then strThisPath = strParentPath & "\" & arrFolders(n) Debug.Print "strThisPath=" & strThisPath 'Does this path exist? If not we need to create it If (TestPlanDoesPathExist(strThisPath) = False) And (arrFolders(n) <> "") Then 'Create it 'Get the parent folder Set oParentFolder = TestPlanGetSubjectNode(strParentPath) 'Create this folder underneath it oParentFolder.AddNode (arrFolders(n)) End If End If 'before we move on, set the parent path If strParentPath = "" Then strParentPath = "Subject" Else strParentPath = strThisPath End If Next n 'Release all objects Set oParentFolder = Nothing End Sub ------------------------------------------------------Function TestPlanDoesPathExist(ByVal strPath As String) As Boolean Dim TreeMgr As TreeManager Dim SubjRoot As SubjectNode Dim SubjectNodeList As List Dim oSubjectNode As SubjectNode

Set TreeMgr = tdc.TreeManager Set SubjRoot = TreeMgr.TreeRoot("Subject") If UCase(strPath) = "SUBJECT" Then TestPlanDoesPathExist = True Exit Function End If Set SubjectNodeList = SubjRoot.FindChildren("", False, "") For Each oSubjectNode In SubjectNodeList If UCase(oSubjectNode.Path) = UCase(strPath) Then

TestPlanDoesPathExist = True Set TreeMgr = Nothing Set SubjRoot = Nothing Set SubjectNodeList = Nothing Set oSubjectNode = Nothing Exit Function End If Next TestPlanDoesPathExist = False Set TreeMgr = Nothing Set SubjRoot = Nothing Set SubjectNodeList = Nothing Set oSubjectNode = Nothing End Function ---------------------------------------------------Function TestPlanDoesTestExist(ByVal strTestName As String, ByVal strFolderToSearchPath As String, ByVal SearchChildFolders As Boolean, Optional ByVal strTestType As String) As Boolean Dim oTempTest As Test Set oTempTest = TestPlanFindTest(strTestName, strFolderToSearchPath, SearchChildFolders, strTestType, True) If (oTempTest Is Nothing) Then TestPlanDoesTestExist = False Else TestPlanDoesTestExist = True End If Set oTempTest = Nothing End Function

Quality Center OTA: List all Tests in the Test Plan


The code below is a simple way to show all of the Folders and Tests in the Quality Center test plan. You could use this as a basis to develop a treeview of the testplan in another tool. This code is designed to be run from Excel VBA, but you can easily adapt it for VBScript. It assumes that you have already connected to Quality Center which is represented by the "tdc" variable in the code. This should help anyone having problems with the error "Field < Subject > requires a value from the corresponding list" - the answer to this problem is to encapsulate the subject with quotes - shown below with chr(34) wrapped around TS_Subject. It took me 2 hours to resolve that issue, no thanks to the OTA documentation!!

Sub ExploreTestPlan() Dim Dim Dim Dim Dim Dim Dim Dim TreeMgr As TreeManager SubjRoot As SubjectNode TestFact As TestFactory TestFilter As TDFilter TestList As List oTest As Test SubjectNodeList As List oSubjectNode As SubjectNode

'*** make sure you have connected to QC with the tdc object *** Set Set Set Set TreeMgr = tdc.TreeManager SubjRoot = TreeMgr.TreeRoot("Subject") TestFact = tdc.TestFactory SubjectNodeList = SubjRoot.FindChildren("", False, "")

For Each oSubjectNode In SubjectNodeList 'Print out the subject path Debug.Print oSubjectNode.Path 'Does this have any tests? Set TestFilter = TestFact.Filter TestFilter.Filter("TS_SUBJECT") = Chr(34) & oSubjectNode.Path & Chr(34) Set TestList = TestFact.NewList(TestFilter.Text) For Each oTest In TestList Debug.Print "Test Name='" & oTest.Name & "' Test Type=" & oTest.Type Next Next End Sub