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.
Here you go:
Define the test steps as shown in the below screen shot.
Test Case Structure:
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:
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 = tempContentAll 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