Forum Discussion

Apoorva6's avatar
Apoorva6
Frequent Contributor
9 years ago
Solved

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 ...
  • 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