Reputation: 2378
I have a requirement where there is an xml in my file system and I have to read tag values from that xml and populate in my soap request. I have totally no idea how do I do this.
I wrote a groovy script to read file:
File file = new File("C:/Users/Desktop/abc.txt")
fileContent = file.getText()
Now I want to read tag values from my source xml and populate in my soap request xml tags.
My sample xml is
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" >
<soapenv:Header/>
<soapenv:Body>
<dto:dto>
<mon:abc>
<mon:HDR>
<mon:MESSAGE_ID>abcd</mon:MESSAGE_ID>
<mon:MSG_TIMESTAMP>2015-10-12T11:24:35-06:00</mon:MSG_TIMESTAMP>
</mon:HDR>
</mon:abc>
</dto:dto>
</soapenv:Body>
</soapenv:Envelope>
I have to read message_id from my sample xml into my soap request message id. Can someone guide how can i do this?
Upvotes: 3
Views: 974
Reputation: 21359
Here is the groovy script to extract the Message ID from the given file
Note that the sample you have provide does not have the proper data, missing namespaces.
import com.eviware.soapui.support.XmlHolder
//change the file path here
def xml = new File('/absolute/file/path').text
def holder = new XmlHolder(xml)
def messageId = holder.getNodeValue('//*:MESSAGE_ID')
assert messageId, "Message Id is empty or null"
log.info "MessageId is : ${messageId}"
If you need the messageId
to be used in other steps of the same test case, please store it in a test case level property using below additional statement:
context.testCase.setPropertyValue('MESSAGE_ID', messageId)
Later you can use in other steps (except groovy) as ${#TestCase#MESSAGE_ID}
Use it in another groovy script of same test case as context.expand('${#TestCase#MESSAGE_ID}')
Upvotes: 2