阅读Web服务响应并在Groovy脚本中写入Excel(使用SOAP)

时间:2017-04-11 19:34:49

标签: excel web-services soap groovy

我编写了以下代码,用于从Excel动态读取值,从SOAP UI调用Web服务以及在Response标记之间写入值到Excel。

import jxl.*; 
import jxl.read.biff.BiffException; 
import jxl.write.*; 

def reqOperationName = "TestRequest"; 
def inputDataFileName = "Automate/SampleData.xls";
def inputDataSheetName = "sendSampleData";
log.info("Service Testing Started")     
Workbook workbook = Workbook.getWorkbook(new File(inputDataFileName)); 
WritableWorkbook copy = Workbook.createWorkbook(new File(inputDataFileName),workbook); 
    WritableSheet sheetl = copy.getSheet(inputDataSheetName); 

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context); 
def reqholder = groovyUtils.getXmlHolder(reqOperationName+"#Request");
    try{ 
        rowcount = sheetl.getRows(); 
        colcount = sheetl.getColumns(); 
        for(Row in 1..rowcount-1){
            for(Col in 2..colcount-1){
                String reqTagName = sheetl.getCell(Col,0).getContents() 
                def TagCount = reqholder["count(//*:"+reqTagName+")"] 

                    if(TagCount!=0){
                            String reqTagValue = sheetl.getCell(Col,Row).getContents() 
                            reqholder.setNodeValue("//*:"+reqTagName, reqTagValue) 
                            reqholder.updateProperty() 
                        } 
            } 
                        // To Run Test Request 
                        testRunner.runTestStepByName(reqOperationName) 
        //Read Response XML
                def resholder = groovyUtils.getXmlHolder(reqOperationName+"#Response")

                resTagValue1 = resholder.getNodeValues("//*:message")

                //Write Response value to Excel Sheet
                Label resValue1 = new Label(8,Row,java.lang.String resTagValue1);
                        sheetl.addCell(resValue1);
        }// Row loop Ends Here 
    }catch (Exception e) {log.info(e) }
        finally{
            copy.write(); 
            copy.close(); 
            workbook.close(); 
        } 
log.info("Service Testing Finished") 

来自我的网络服务的回复如下: -

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
  <Response xmlns="http://eh.dummy.com">
     <sets xmlns="">
        <sets_InnerSet xmlns="http://eh.dummy.com">
           <visibleSetsInnerSet>
              <set_cd>QS1</set_cd>
           </visibleSetsInnerSet>
           <visibleSetsInnerSet>
              <set_cd>QS8</set_cd>
           </visibleSetsInnerSet>
           <visibleSetsInnerSet>
              <set_cd>QS9</set_cd>
           </visibleSetsInnerSet>
        </sets_InnerSet>
     </sets>
     <responseCode xmlns="">success</responseCode>
  </Response>
</soap:Body>
</soap:Envelope>

您能告诉我如何从set_CD标签读取值(QS1 / QS8 / QS9)并将单个单元格中的Excel文件写入QS1,QS8,QS9。

1 个答案:

答案 0 :(得分:0)

您可以使用以下groovy代码段以所需格式获取所需的值。

//Pass the xml string to parseText method below i.e., your response
def parsedXml = new XmlSlurper().parseText(xml)
//Get the desired value in required format
def result = parsedXml.'**'.findAll {it.name() == 'set_cd'}.collect{it.text().trim()}.join(',')
log.info "Desired output : $result"

//You can use result variable to write or use anywhere you needed

您可以在线快速尝试 Demo

希望您能够继续并将上述重试的result写到您需要的地方。