带有条件的SOAPUI groovy捕获响应



我有一个用groovy编写的脚本,我想从响应中保存一个特定的链接,但我不知道如何编写它。

我想保存文档中的链接元数据>directURL,但条件是文件名以ImageWhite__*开头(这里也需要一些表达式,因为ImageWhite__之后的数字会根据请求而变化(

我要捕获的脚本是:

import com.eviware.soapui.support.XmlHolder
import jxl.*
import jxl.write.*
import groovy.xml.XmlUtil
def myTestCase = context.testCase //myTestCase contains the test case
propTestStep = myTestCase.getTestStepByName("Properties") // get the Property TestStep object
//Get the value of the fields from the properties step, we will use this as a file name
field1 = propTestStep.getPropertyValue("Field1").toString()
field2 = propTestStep.getPropertyValue("Field2").toString()
// Write Response to XMl File
def myXmlResponse = "D:\Response\"+field2+".txt"
def response = context.expand('${Request#Response#//*:documentMetadata//*:directURL}') //Replace Request with your Request Name
def res = new File(myXmlResponse)
res.write(response, "UTF-8")

我的响应xml是这样的:

<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope">
<S:Body>
<searchFileResponse xmlns:ns3="-" xmlns:ns2="-" xmlns="-">
<Header>
<ns2:ChannelID></ns2:ChannelID>
<ns2:CallingSystemID></ns2:CallingSystemID>
<ns2:UserID></ns2:UserID>
<ns2:ResponseReference>unused</ns2:ResponseReference>
<ns2:DateTimeRequest>2017-01-03T13:42:42.441+02:00</ns2:DateTimeRequest>
<ns2:DateTimeReceived>2022-03-31T09:43:17.881+03:00</ns2:DateTimeReceived>
<ns2:DateTimeReply>2022-03-31T09:43:18.091+03:00</ns2:DateTimeReply>
<ns2:RequestLanguage>ro</ns2:RequestLanguage>
</Header>
<ServiceExecutionStatus>Ok</ServiceExecutionStatus>
<Errors xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<Warnings xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<returnCode>0</returnCode>
<errorDetails xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<documentMetadata>
<documentId>196692125</documentId>
<documentRef>190821S662234027</documentRef>
<documentType xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<documentMimeType xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<library>IDSCAN</library>
<libraryPath>2019/08/21</libraryPath>
<fileName>ImageIR__4782325272340084988.jpg</fileName>
<directURL>http://something</directURL>
<author xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<authoringGroup xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<title xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<subject xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<description>ABCDFA</description>
<properties>
<name>audit.last_updated_date</name>
<value>1566367303197</value>
</properties>
<properties>
<name>audit.creation_date</name>
<value>1566334800000</value>
</properties>
<properties>
<name>CIC</name>
<value>23321</value>
</properties>
<properties>
<name>file.size</name>
<value>33915</value>
</properties>
<properties>
<name>file.lastmodified</name>
<value>1566367244000</value>
</properties>
</documentMetadata>
<documentMetadata>
<documentId>196692127</documentId>
<documentRef>190821S662234027</documentRef>
<documentType xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<documentMimeType xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<library>IDSCAN</library>
<libraryPath>2019/08/21</libraryPath>
<fileName>ImageUV__8003143531657716008.jpg</fileName>
<directURL>http://something</directURL>
<author xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<authoringGroup xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<title xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<subject xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<description>ABCDFA</description>
<properties>
<name>audit.last_updated_date</name>
<value>1566367303194</value>
</properties>
<properties>
<name>audit.creation_date</name>
<value>1566334800000</value>
</properties>
<properties>
<name>CIC</name>
<value>23321</value>
</properties>
<properties>
<name>file.size</name>
<value>45383</value>
</properties>
<properties>
<name>file.lastmodified</name>
<value>1566367244000</value>
</properties>
</documentMetadata>
<documentMetadata>
<documentId>196692123</documentId>
<documentRef>190821S662234027</documentRef>
<documentType xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<documentMimeType xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<library>IDSCAN</library>
<libraryPath>2019/08/21</libraryPath>
<fileName>ImageWhite__5219450603368724974.jpg</fileName>
<directURL>http://something...</directURL>
<author xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<authoringGroup xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<title xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<subject xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<description>ABCDFA</description>
<properties>
<name>audit.last_updated_date</name>
<value>1566367303176</value>
</properties>
<properties>
<name>audit.creation_date</name>
<value>1566334800000</value>
</properties>
<properties>
<name>CIC</name>
<value>23321</value>
</properties>
<properties>
<name>file.size</name>
<value>56122</value>
</properties>
<properties>
<name>file.lastmodified</name>
<value>1566367244000</value>
</properties>
</documentMetadata>
</searchFileResponse>
</S:Body>
</S:Envelope>

您可以使用XmlSlurper:

def S_Envelope = new XmlSlurper(false, false).parse(responseXML)
S_Envelope."S:Body".searchFileResponse.documentMetadata.'**'.find { documentMetadata ->
if (documentMetadata.fileName.text().startsWith('ImageWhite__'))
println documentMetadata.directURL
}

如果响应XML在文件中,请使用.parse(FileName)进行解析,如果它在字符串中,则使用.parseText(response)

最新更新