I am trying to update values of node <tem:intA> and <tem:intB> in the following xml using below groovy script code.
I used below groovy script code to update the tag values. But I am getting an error "Java.lang.NullPointerException: Cannot invoke method getNodeValue() on null object error at line 36".
Am I using the wrong xpath? I am not sure whats going wrong. Can someone please help me?
Ok, so if I may suggest the use of XMLSlurper, which is built into Groovy so requires no imports and is quote neat. Then if we put your XML snippet (similar typed in by me) into a file /work/SoapUI Forum/soapy.xml:
<?xml version="1.0" encoding="UTF-8"?>
Then, the following Groovy script allows you to parse the XML from file, update the values of intA and intB and save the resulting XML back to the same file:
def xmlFromFile = new File('/work/SoapUI Forum/soapy.xml')
def envelopeNode = new XmlSlurper().parseText(xmlFromFile.getText())
//Just to print it out
XmlUtil xmlUtil = new XmlUtil()
//To update the file with updated XML
xmlUtil.serialize(envelopeNode, new FileWriter(xmlFromFile))
Is this what you needed?
This is what I was looking for. But how to parameterize both xpath and and node value. Lets say, I am taking this xpath (envelopeNode.Body.Add.intA) as a input parameter from excel sheet and then modifying the value to '20'.
def sIntA=envelopeNode.Body.Add.intA // Lets say, this value is taken from excel sheet (I have a code to get the value from excel sheet)
My aim is to get the Node's xpath from excel sheet, get the Node's value from excel sheet, update the xml with this node and save updated xml in a different folder.
I think I now get what you are after i.e. a way to pass the actual (XPath) query in as a String paramter, but before having a go at that, can I ask if you actually just want a way to parameterise and populate SOAP requests from your spreadsheet of values e.g. data-driven testing? If this is the case I can potentially explain an easier way to do this in SoapUI using your spreadsheet.
Yes Rup. I am actually looking for data driven testing. I am trying to use external xml stored on local disc. I will update the node values using xpath. After updating the xml, I will save it in different folder and at the end I will upload both request xml and response xml to the test case in ALM. It helps me keeping track of request and response used for specific test case.
Please let me know if you have any easier way to do this.
Really appreciate your help. Thanks!
Ok, that sounds interesting. I don't know much about ALM, but hopefully we can sort something out.
So a common pattern of data-driven testing in SoapUI can be:
1. Read line of CSV data or potentially a soap request (using Groovy) in your case. You can do this in a Groovy TestStep. Say you have read the values in you can set them as properties on the context holder e.g.
2. Setup a SOAP Request TestStep and use the properties from the context holder to parameterise the SOAP request. You can esily insert the values using whats called property expansions (see http://www.soapui.org/scripting---properties/property-expansion.html) e.g.
<?xml version="1.0" encoding="UTF-8"?>
2a) You can also use the data values from the context to parameterise Assertions e.g. XPath Assertion (expected value) or Content or Script Assertions.
3. Use another Groovy TestStep (there is also a Conditional Goto TestStep) to loop back to 1 if there are more rows of test data.
Of course if you want to dump the SOAP request/response into a file, the thats easy enough to add with Groovy.
Does this sound at all like what you wanted? It just sounded like it might be easier to work with data values e.g. CSV rather than having to manipulate XML with XPaths..
If this is not really what you need, we can get back to the plan A and I'll have a go at adapting that Groovy script to use XPaths instead.
I am also facing a similar problem but the difference is that I am not doing SOAPUI testing but I need to update the SOAPUI response tag values from an excel before forwarding that response to another system ,since my main application is developed in groovy ,I was thinking if this can be done in groovy only.
So my excel header are actually XPATHS and the corresponding rows below contain the value.
any help would be great.
Ok, I am not sure I get exactly what you mean, please can you explain more? So you say you arent testing with SoapUI, but want to update tags in the response - are you calling some kind of service that returns XML and wanting to update the response based on values taken from an Excel spreadsheet?
Yeah ,so I have an inhouse built(java-groovy) application which generates a SOAP response and I have to feed that response to a proprietory software .But before sending the response to the software I need to update few tag values as per the Excel received from third party.
For now I have manually updated the third party Excel column headers with XPATHs of the XML so that I can update the XMLs as per the values in the corresponding row.
My main problem is reading the column header and passing its value in place of
in the below code
String fileContents = new File(FilePath).text def xmlfromFile = new File(FilePath) def Envelope = new XmlSlurper().parseText(xmlfromFile.getText()) Envelope.Body.CamCommand.CamAction.tCANOTIFICATIONS.pCASECIDUNDL = "NewData123" XmlUtil xmlUtil = new XmlUtil() println xmlUtil.serialize(Envelope) xmlUtil.serialize(Envelope, new FileWriter(xmlfromFile))
The below link is having an example, Hope this will help