How to use SoapUI/Groovy to add Test Results to Quality Center Test Run

Josh Ripley picture Josh Ripley · Nov 24, 2011 · Viewed 7.9k times · Source

I had a scenario where I need to add my test results from a SoapUI test case to a related test case in HP Quality Center. How could I go about doing this with Groovy.

Answer

Josh Ripley picture Josh Ripley · Nov 25, 2011

Quality Center offers an OTA API to interface with it; however, it is written to use the Microsoft COM structure which Java/Groovy has no native way to consume. Lucky, there is a Groovy library, Scriptom, that allows Groovy to consume COM interfaces.

After getting every setup, this is the sample code that I came up with:

import org.codehaus.groovy.scriptom.*

def tstCaseName = "NAME_OF_TESTCASE"
tstCaseName = tstCaseName.replaceAll(/[ ]/) {  match ->
    "*"
}

Scriptom.inApartment
{
    //  Create an entry point for QC
    def tdc = new ActiveXObject ('TDApiOle80.TDConnection')

    //  Connect to QC, Login with Credentials, connect to Project
    tdc.InitConnectionEx('http://qc.example.com/qcbin')
    tdc.Login('USER_NAME', 'PASSWORD')
    tdc.Connect('DOMAIN_NAME','PROJECT_NAME')

    //  Find the set of tests in the Test Plan
    tsFolder = tdc.TestSetTreeManager.NodeByPath('Root\\PATH\\TO\\TEST\\CALENDAR\\Spaces are allowed')
    tsList = tsFolder.FindTestSets('NAME_OF_TEST_CALENDAR')
    tsObject = tsList.Item(1)

    //  Get the list of TestCases in the Test Plan and filter it down to the one Test Case we are interested in
    TSTestFact = tsObject.TSTestFactory 
    tstSetFilter = TSTestFact.Filter
    tstSetFilter.Filter["TS_NAME"] = '*' + tstCaseName
    TestSetTestsList = TSTestFact.NewList(tstSetFilter.Text)
    tsInstance = TestSetTestsList.Item(1) 

    //  Create a new Test Run
    newRun= tsInstance.RunFactory.AddItem('Run_Auto')
    newRun.Status = 'Not Completed' 
    newRun.Post() 
    newRun.CopyDesignSteps()
    newRun.Post() 

    //  Populate Auto Run Test step Data
    def tsStepData = new Object[3]
    tsStepData[0]='Auto Data'
    tsStepData[1]='Not Completed'
    tsStepData[2]='Results from Automated Test'
    //  Create new Test Step in the run with our Auto Run Data
    tsSteps = newRun.StepFactory.AddItem(tsStepData)
    tsSteps.Field['ST_ACTUAL'] = 'These are the actual results from my test!!'
    tsSteps.post()
}