首页 > 解决方案 > 使用 groovy 脚本比较两个肥皂响应

问题描述

我正在使用 SoapUI 免费版。我需要比较肥皂响应。我期望在 Excel 文件中得到响应。我想将预期响应与 SoapUI 的实际响应进行比较,并且在比较时我需要使用 groovy 脚本忽略一些字段。我可以将 Excel 中的数据读入一个字符串,并将 SoapUI 的响应读入另一个字符串。我需要一些帮助来比较这两个值。我没有得到任何适当的参考来比较。我没有任何代码进行比较。请在这件事上给予我帮助

import static java.nio.charset.StandardCharsets.*;
import java.lang.*;
import com.eviware.soapui.model.testsuite.Assertable.AssertionStatus
import com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep
import com.eviware.soapui.support.XmlHolder
import jxl.*
import jxl.write.*
import org.skyscreamer.jsonassert.JSONAssert
import org.skyscreamer.jsonassert.JSONCompareMode
import groovy.json.JsonSlurper
/*XMLUnit.setIgnoreWhitespace(true)
XMLUnit.setIgnoreComments(true)
XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true)
XMLUnit.setNormalizeWhitespace(true)*/
def count=0,i=1,j=1,k=1
def inputFileName,outputFileName
def inputFile = testRunner.testCase.testSuite.getPropertyValue("inputFileName")
def outputFile = testRunner.testCase.testSuite.getPropertyValue("outputFileName")
def testStep = testRunner.testCase.testSteps["Properties"]

Workbook inputWB = Workbook.getWorkbook(new File(inputFile)) 
Sheet inputSheet = inputWB.getSheet(0)
no_of_rows= inputSheet.getRows().toInteger() 

Workbook existingInputWB=Workbook.getWorkbook(new File(inputFile))
WritableWorkbook outputWB=Workbook.createWorkbook(new File(outputFile),existingInputWB)

testCaseName=testRunner.testCase.name
outputWB.createSheet(testCaseName,2)
WritableSheet sheet_writable = outputWB.getSheet(testCaseName)
jxl.write.Label testStepCell =  new jxl.write.Label(0,0 ,"Test Step")
jxl.write.Label reqUrlCell =  new jxl.write.Label(2,0 ,"Request URL")
jxl.write.Label reqPayloadCell =  new jxl.write.Label(3,0 ,"Request Payload")
jxl.write.Label responseCell =  new jxl.write.Label(4,0 ,"Response")
jxl.write.Label statusCell =  new jxl.write.Label(1,0 ,"Status")
jxl.write.Label reasonCell =  new jxl.write.Label(5,0 ,"Reason")
sheet_writable.addCell(testStepCell)
sheet_writable.addCell(reqUrlCell)
sheet_writable.addCell(reqPayloadCell)
sheet_writable.addCell(responseCell)
sheet_writable.addCell(statusCell)
sheet_writable.addCell(reasonCell)
(0..context.testCase.testStepCount-1).each{
    def step = context.testCase.testStepList[it]
    if ( step instanceof WsdlTestRequestStep) {
        jxl.write.Label stepName =  new jxl.write.Label(0,i ,step.name)
        sheet_writable.addCell(stepName)
        def tr=testRunner.testCase.getTestStepByName(step.name)
          def String endPointUrl= tr.getHttpRequest().getResponse().getURL()
        jxl.write.Label reqUrl =  new jxl.write.Label(2,i ,endPointUrl)
        sheet_writable.addCell(reqUrl)
        def payload = context.expand(step.getPropertyValue('Request'))
        jxl.write.Label reqPayload =  new jxl.write.Label(3,i ,payload)
        sheet_writable.addCell(reqPayload)
         def response = context.expand(step.getPropertyValue('Response'))
        jxl.write.Label reqResponse =  new jxl.write.Label(4,i ,response)
        sheet_writable.addCell(reqResponse)
        count=count+1
        i=i+1
    }
}

(0..context.testCase.testStepCount-1).each{
    def step1 = context.testCase.testStepList[it]
    if ( step1 instanceof WsdlTestRequestStep) {
        for(j=1;j<no_of_rows;j++){
            Cell f=inputSheet.getCell(0,j)
             step_name=f.getContents()
             Cell f1=inputSheet.getCell(3,j)
             def String step_response=f1.getContents()
             step_response=step_response.replaceAll(" ","")
             def String response = context.expand(step1.getPropertyValue('Response')) 
             log.info response
             response=response.replaceAll(" ","") 
             /*if(step1.name==step_name&step1.name!=null){

             }*/

        }
    }
}
outputWB.write()
outputWB.close()
existingInputWB.close()

要比较的数据:我想在比较时忽略 Auth

<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header/>
<env:Body>
<ns3:ZeppEsCheckOnlCustIfResponse xmlns:ns3="urn:sap-com:document:sap:soap:functions:mc-style">
<Auth>36962796d68753535353f2a79637f536865636b6f6e6c636573747f213537353839303631393336353c745543545</Auth>
<number>224354</number>
<Ok>X</Ok>
<Failure/>
</ns3:abcd>
</env:Body>
</env:Envelope> 

标签: excelsoapui

解决方案


有三种主要方法可以做到这一点。

1) 如果你想比较 XML 和 XML,你可以使用带有 Groovy 的 XMLUnit 库。我发现这很脆弱。

2)您还可以使用 Groovy 的 XMLSlurper 解析响应 XML,并使用 GPath 导航到特定元素并获取它们的值:

def responseXML = context.expand('${mySoapRequestStep#Response}')
def Envelope = new XmlSlurper().parseText(responseXML)
log.info Envelope.Body.someNode.nodeIWantValueFor

3) 第三个选项是在您的 SOAP 请求步骤之后创建一个属性传输步骤,并使用 XPath 将值存储在属性中。您在该步骤中使用的 XPath 表达式采用以下格式:

//*[local-name()='someNode']/*[local-name()='nodeIWantValueFor']/text()

在 Property Transfer 步骤中,您可以指定值的保存位置,例如在名为 nodeIWantValueFor 的 testCaseProperty 中。


推荐阅读