cancel
Showing results for 
Search instead for 
Did you mean: 

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

SOLVED
nmrao
Community Hero

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

If your test case has something below:
1. jdbc test step => get values of two you mentioned
2. run soap request - mock => wanted get the dynamic response

Then you need to pass the required values to the next soap request as headers because, there is no relationship between the soap request and jdbc values.

This way, one can grab those values in script dispatch of mock response with those values in it.
Do you have any other stuff running in your mind?


Regards,
Rao.
Apoorva6
Frequent Contributor

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

Hi Rao, I dont know any other ways to achieve it.  Can we do like below ?

1) Get JDBC values

2) Property transfer via script to set jdbc values in mockResponse (before setting mockResponse check if mockResponse is present)

3) Invoke mockRequest to get mockResponse with updated values.

nmrao
Community Hero

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

I know it is getting dragged. What if you set some dummy values in the mock response (any way that is dummy) for both placeholder values? Are you ok?


Regards,
Rao.
Apoorva6
Frequent Contributor

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

But why Dummy values ? I need the mockResponse with values from JDBC retreival so that our code can process it.

nmrao
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

Apoorva6
Frequent Contributor

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

Hi Rao,

 

Thank you very much for the detail solution. I am checking on same. meanwhile,

 

1)Does above solution will set 2 dynamic values in mockResponse before invoking its request ?

2) Can you let me know how we can achieve this without 4 steps, by directly invoking JDBC in mockResponse Script ?

3) I really appreciate for your concern on providing solutions. I am a beginner for soap. can you let me know if any video link or links for Groovy script specially for soapUI ?

4) Sorry for asking personal info here, but there is no alternative way for me. Can you please share your contact details or mail detail to hr.apoorva@gmail.com ? I have lot many questions to ask on soapUI.

 

nmrao
Community Hero

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

Ok, drop a note once you tried it.

  1. Not set the values in mock response before invoking.  Values are set as headers for soap request, those will be replaced while creating mock response only.
  2. Basically I will not have all the envrionment, here you have to search and put things together. Find jdbc example in groovy here - http://www.ibm.com/developerworks/library/j-pg01115/
  3. Learning is progressive, so do not worry, just try with open minded. Please see this link: http://community.smartbear.com/t5/SoapUI-Open-Source/How-to-read-and-use-SoapUI-API-for-non-Dev-peop...
  4. Really sorry about it. I strongly encourage to keep posting the questions on community. And this way you will neither be blocked, nor waiting for response just from one person. Also those questions not only help you, but may be helpful or available to the entire community for reference any time. When ever possible, I can take a look. Of course, there are lot of people who are willing to help others in our community and very active, thats how I learned and doing my 2cents. 

As I mentioned early, you made me to learn to scripting for mockResponse dispatcher, I don't know it earlier seriously, you can see time took between the question raised and the question answered which is learning, understanding, how to do etc. I owe thank you for giving  the opportunity.

One thing is to try ourself with hope & patience. Even I do the same, most of times, try it myself, many times it happens that needs lot of effort to understand the problem and come up with ideas, sometimes i may not be able to do as well, every one might have some limitations.  Accept for ourself saying that is ok.

All I was saying because if those words can raise hope / inspire even for a single person and start trying for self, am happy.



Regards,
Rao.
Apoorva6
Frequent Contributor

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

Hi Rao, Thank you again and I agree.

 

I tried above solution. However values from JDBC is not getting displayed in mockresponse, though we are able to invoke mockResponse. In http header I could see the values from jdbc getting set, but same is not set in mockresponse file. I have attached the screenshot.

 

 

nmrao
Community Hero

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

The script was done as the per the response that you provided earlier. You can see the same in the screen in the answer showing values replaced.
Any way, just make sure that your response has place holder values as you mentioned.

Also check the logs by adding below line before and after replacement is happening
log.info tempContent


Regards,
Rao.
Apoorva6
Frequent Contributor

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

Hi Rao,

 

I am able to get response with proper values, however I need the JDBC values to be updated in file, so that I can use same file with mockResponse again. Cant that be achived ?

New Here?
Join us and watch the welcome video:
Top Kudoed Authors