我有一个要求,我的文件系统中有一个 xml,并且我必须从该 xml 中读取标签值并填充到我的 SOAP 请求中。我完全不知道该怎么做。
我编写了一个常规脚本来读取文件:
File file = new File("C:/Users/Desktop/abc.txt")
fileContent = file.getText()
现在我想从源 xml 中读取标签值并填充到我的 SOAP 请求 xml 标签中。
我的示例 xml 是
<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>
我必须将示例 xml 中的 message_id 读取到我的 SOAP 请求消息 ID 中。有人可以指导我该怎么做吗?
最佳答案
这是从给定文件中提取消息 ID 的 groovy 脚本
请注意,您提供的示例没有正确的数据,缺少命名空间。
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}"
如果您需要在同一测试用例的其他步骤中使用messageId
,请使用以下附加语句将其存储在测试用例级别属性中:
context.testCase.setPropertyValue('MESSAGE_ID', messageId)
稍后您可以在其他步骤(groovy 除外)中使用 ${#TestCase#MESSAGE_ID}
在与 context.expand('${#TestCase#MESSAGE_ID}') 相同测试用例的另一个 groovy 脚本中使用它
关于xml - 在soap ui中读取文件插入xml节点值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35721685/