Forum Discussion

Apoorva6's avatar
Apoorva6
Frequent Contributor
9 years ago

Set dynamic XML element value to a mockResponse present in external file.

Hi ,

 

I have a scenario to get two values from DB and set them to mockResponse present in external path. The file name of the mockResponse stored will also be set as global properties.

 

1) Set Global property for mockResponse file name. Example solNum= 'S12345'

2)Get JDBC connection and get custid and banum , I am able to get the values with below groovy

 

String banum, custid
banum=context.expand('${JDBC Request#ResponseAsXml#//Results/ResultSet/Row/MOW_HIER_DETL_TB.UP_BA_ND_NUM}')
custid=context.expand('${JDBC Request#ResponseAsXml#//Results/ResultSet/Row/MOW_RQST_TB.CUST_ID}')

 

3) I need to check if mockResponse in path C:Users  with name ${solNum} exists or not. (i.e S12345.xml) if not set Fault.xml

 

if file exists, then set the two received values from DB in file. After setting values, get the mockResponse with updated values.

 

The mockResponse in file looks as the one attached, and we need to set the values $custid and $banum in file.

 

 

 

 

  • nmrao's avatar
    nmrao
    9 years ago

    Here you go:

     

    Define the test steps as shown in the below screen shot.

     

    Test Case Structure:

     

    19350.png

     

     

    Also define two custom propertyies say, CUSTOMER_ID, BANUM. But you do not have to set any value for both them.

    Because you wanted those values to be read from JDBC,  and assign the values in the Property Transfer Test step to test case properties, like below:

     

    19350_1.png

     

    The content for Groovy script goes below. What it is doing is, setting test case properties customerId, banum as http headers to the next Request Test step

     

    Groovy Script

     

    def setHttpHeaders(String nextStepName, def headers) {
        def nextRequest = context.testCase.testSteps[nextStepName].httpRequest
        def existingHeaders = nextRequest.requestHeaders
        headers.each {
            existingHeaders[it.key] = it.value
        }
        nextRequest.requestHeaders = existingHeaders
    }
    def nextStepName = context.testCase.getTestStepAt(context.currentStepIndex + 1).name
    def headers = ['CUSTOMER_ID':[(context.testCase.getPropertyValue('CUSTOMER_ID'))], 'BANUM': [(context.testCase.getPropertyValue('BANUM'))]]
    setHttpHeaders(nextStepName, headers)

    Now go to the SCRIPT of MockResponse Dispather

    Note that mock respose will still have the value ${content}, like earlier solution.

     

    SCRIPT:

     

    def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
    def holder = groovyUtils.getXmlHolder(mockRequest.requestContent)
    /**Here we are reading those http headers that were set in Groovy script **/ def customerId = mockRequest.requestHeaders['CUSTOMER_ID'][0] def banum = mockRequest.requestHeaders['BANUM'][0] def soNumber = holder.getNodeValue("//*:BillAccountPackageReadRequest/*:ContractSolutionNumber") def file = new File (groovyUtils.projectPath+"/MOCK/GCP/GetBill/${soNumber}.xml") def fileToLoad = 'soapFault' if (file.exists()) { fileToLoad = soNumber } def tempContent = groovy.xml.XmlUtil.serialize(new XmlParser().parse(groovyUtils.projectPath+"/MOCK/GCP/GetBill/${fileToLoad}.xml"))
    /**replacing the place holders in the response **/ tempContent = tempContent.replace("\$custid", customerId) tempContent = tempContent.replace("\$banum", banum)
    //finall setting the response content context.content = tempContent

     

    All I was trying to say from the start of this thread is that it is not required to have 4 steps, just jdbc query can be done within script of mock response dispatcher. Any way, never mind.

     

    UPDATE:

    Was just re-looking at it, Groovy Script Test Step can be avoided if you define Headers for Request Test Step as shown below

    19351_2.png

  • nmrao's avatar
    nmrao
    Champion Level 3
    Looks there was a solution provided you earlier. I remember that was a fixed response from file. So you want some dynamic elements to be populated, right? In that case do you still need to refer lot of files?
    By the way, can you post the sample response with place holder values needed in there?
    • Apoorva6's avatar
      Apoorva6
      Frequent Contributor
      Hi Rao, yes, earlier scenario was to get fixed response. Now need Dynamic elements to be set and get back response. And we need to check file name now set by global parameter. I had already attached sample response with $custid and $banum whose values need to set dynamically.
      • nmrao's avatar
        nmrao
        Champion Level 3
        1. so here the request would differ from earlier one, right? can you attach the same?
        2. So are you using a single global property i.e., fixed response with dynamic ids mentioned by you in this case?