cancel
Showing results for 
Search instead for 
Did you mean: 

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

SOLVED
Highlighted
Frequent Contributor

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.

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Community Hero

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

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



Regards,
Rao.

View solution in original post

34 REPLIES 34
Community Hero

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

Community Hero

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

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?


Regards,
Rao.
Frequent Contributor

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

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.
Community Hero

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

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?


Regards,
Rao.
Frequent Contributor

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

Hi Rao,

 

Attached is the sample request. In that sol12345 is the global prameter set to get dynamic mock response. First we need to check if file with name sol12345 exists, if not error. If exists, then set 2 values fetched from DB to the response.

Frequent Contributor

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

Hi Rao, any update please? Or anyone?
Community Hero

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

In order to achieve this, there should be place holder values in the xml file which has mock response.
Then read the xml file data, replace the place holder value with the value that you fetch from database.
Then like earlier, set value to context.content finally.


Regards,
Rao.
Frequent Contributor

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

Hi Rao,

 

As per my first comment,

 

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

 

and the request looks like the one attached. So $solNum should be the place holder I guess. Please correct me. Please check the request attached.

 

 

Community Hero

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

If you can use the previous reply which says what should be done in the groovy script and leverage earlier solution provided to you in another thread, you can accomplish this. Should be a simple change.


Regards,
Rao.
New Here?
Join us and watch the welcome video:
Top Kudoed Authors