2015-09-15 75 views
4

我的任務是從soapui中的xml中獲取值以創建序列中的下一個測試。在soapui groovy腳本中從xml中獲取文本值

我使用Groovy腳本

我的目標是讓裏面的標記文字。它很容易識別,因爲它是base64編碼的字符串。

def project = testRunner.testCase.testSuite.project ; 
def tcase = project.testSuites["ChangeRetentionByCopyDemoSuite"].testCases["Milestone2"] ; 
def tstep = tcase.getTestStepByName("getDocuments - GetContentURLRequest"); 

def responseTestSuite1 = tstep.getPropertyValue("response"); 

log.info(responseTestSuite1.toString()); 

def gutils = new com.eviware.soapui.support.GroovyUtils(context); 
def holder = gutils.getXmlHolder("$responseTestSuite1"); 

def byteResponse = holder.getNodeValue("/S:Envelope/S:Body/g:getDocumentsResponse/text()"); 

log.info(byteResponse);

的XML性反應在getDocuments - GetContentURLRequest

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> 
    <S:Header> 
     <Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> 
     <Timestamp xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"> 
      <Created>2015-09-15T13:57:50.844Z</Created> 
      <Expires>2015-09-16T13:57:50.844Z</Expires> 
     </Timestamp> 
     </Security> 
    </S:Header> 
    <S:Body> 
     <getDocumentsResponse xmlns="http://asg.com/2009/03/schemas/tciadapters/getdocument"> 
     <result> 
      <operationStatus> 
       <severity>INFO</severity> 
       <reasonCode>A300</reasonCode> 
       <messageText>All requests completed without errors.</messageText> 
      </operationStatus> 
      <documents> 
       <DocumentData> 
        <docId> 
        <NameValuePair> 
         <keyName>cmis:objectId</keyName> 
         <keyValue>idd_48716F01-F5F7-4702-AC80-4EC70C949121</keyValue> 
        </NameValuePair> 
        </docId> 
        <status xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> 
        <content> 
        <ContentSegment> 
         <data>aHR0cHM6Ly91c3J5ZTh0Y2k0bTAyLmFzZy5jb206NzQ0My9jbWlzYWRhcHRlci9nZXRjb250ZW50P3VzZXJpZD1kZXY2ZGV2aWQmcGFzc3dvcmQ9ZGV2ZWxvcCUyMzEmY21pc19pbnN0YW5jZT1GaWxlTmV0UDgrLStBdG9tUHViJnJlcG9zaXRvcnk9ZmlsZW5ldG9iamVjdHN0b3JlMSZjbWlzOm9iamVjdElkPWlkZF80ODcxNkYwMS1GNUY3LTQ3MDItQUM4MC00RUM3MEM5NDkxMjE=</data> 
        </ContentSegment> 
        </content> 
        <metadata/> 
        <properties> 
        <subrange xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> 
        <format xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> 
        <supportedFormats/> 
        </properties> 
        <retention/> 
       </DocumentData> 
      </documents> 
     </result> 
     </getDocumentsResponse> 
    </S:Body> 
</S:Envelope> 

不幸的是我得到的異常運行的代碼

回答

4

最後一行時,如果要提取標籤內的文本 「MessageText中」那麼您可以指定父節點和子節點,即將最後一行修改爲

def byteResponse = holder.getNodeValue("//*:operationStatus//*:messageText") 

// since there is only one tag named "messageText" in the entire response, you could use this also 

def byteResponse = holder.getNodeValue("//*:messageText") 
+0

非常感謝..我們一直在使用(「// * [local-name()='messageText']」)。你建議的方式更短,更好 –

相關問題