使用groovy脚本将excel表中的值替换为soap请求时出现问题

时间:2017-12-06 09:29:48

标签: excel xpath soap groovy scripting

我尝试从excel工作表传递到soap请求中的特定字段的值在测试用例级别不起作用。我能够阅读excel表,并且正在按预期执行,但是这些值不会在excel表的soap请求中替换。 我已粘贴下面的代码,我正在尝试使用groovy脚本实现数据驱动。

import com.eviware.soapui.support.XmlHolder
import java.io.File;
import java.io.IOException;
import jxl.*;
import jxl.read.biff.BiffException;
import jxl.write.*;
log.info("Testing Started")
def reqOperationName = "getInsuranceDetails_1_FTC_005";
def inputDataFileName = "D:/SOAP UI Pro/MPI.xls"
def inputDataSheetName = "MPI"
Workbook workbook = Workbook.getWorkbook(new File(inputDataFileName));
WritableWorkbook copy = Workbook.createWorkbook(new File(inputDataFileName),workbook);
WritableSheet sheet1 = copy.getSheet(inputDataSheetName);
log.info("Testing1 Started")
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
String xmlResponse = '${' + reqOperationName + '#Request' +'}'
def reqholder = groovyUtils.getXmlHolder(context.expand(xmlResponse))
try{
    rowcount = sheet1.getRows();
colcount = sheet1.getColumns();
for(Row in 1..rowcount-1){

    String reqTagName = sheet1.getCell(0,0).getContents()
        log.info reqTagName
        def TagCount = reqholder["count(//*:"+reqTagName+")"]
        if(TagCount!=0){
            String reqTagValue = sheet1.getCell(0,Row).getContents()
            reqholder.setNodeValue("//*:"+reqTagName, reqTagValue)
            reqholder.updateProperty()                              
        }

    //test the request
    testRunner.runTestStepByName(reqOperationName)
log.info("Testing3 Started")
def resholder = groovyUtils.getXmlHolder(reqOperationName+"#Response")
resTagValue1= resholder.getNodeValues("//*:productID")
resTagValue2= resholder.getNodeValues("//*:accountNumber")
resTagValue3= resholder.getNodeValues("//*:insuranceCategory")
resTagValue4= resholder.getNodeValues("//*:imei")
resTagValue5= resholder.getNodeValues("//*:handsetMake")
resTagValue6= resholder.getNodeValues("//*:handsetModel")
resTagValue7= resholder.getNodeValues("//*:insurancePolicyName")
resTagValue8= resholder.getNodeValues("//*:insuranceStartTimestamp")        
//Write Response into excel sheet
Label resValue1= new Label(4,Row,resTagValue1);
sheet1.addCell(resValue1);   
Label resValue2= new Label(5,Row,resTagValue2);
sheet1.addCell(resValue2);
Label resValue3= new Label(6,Row,resTagValue3);
sheet1.addCell(resValue3);
Label resValue4= new Label(7,Row,resTagValue4);
sheet1.addCell(resValue4);
Label resValue5= new Label(8,Row,resTagValue5);
sheet1.addCell(resValue5);
Label resValue6= new Label(9,Row,resTagValue6);
sheet1.addCell(resValue6);
Label resValue7= new Label(10,Row,resTagValue7);
sheet1.addCell(resValue7);
Label resValue8= new Label(11,Row,resTagValue8);
sheet1.addCell(resValue8);      
    }   
}catch (Exception e) {log.info(e)}
finally{
    copy.write();
    copy.close();
    workbook.close();
}
log.info("Testing Over")

当我尝试执行上面的代码时,错误日志中没有任何错误。我在日志输出中唯一得到的是 Wed Dec 06 15:04:00 IST 2017:INFO:groovy.lang.GroovyRuntimeException:找不到匹配的构造函数:jxl.write.Label(java.lang。 Integer,java.lang.Integer,[Ljava.lang.String;)

对上述问题的任何帮助都将非常感激......

提前谢谢。

新编辑代码......

import com.eviware.soapui.support.XmlHolder
import java.io.File;
import java.io.IOException;
import jxl.*;
import jxl.read.biff.BiffException;
import jxl.write.*;
log.info("Testing Started")
def reqOperationName = "getInsuranceDetails_1_FTC_005";
def inputDataFileName = "D:/SOAP UI Pro/MPI.xls"
def inputDataSheetName = "MPI"
Workbook workbook = Workbook.getWorkbook(new File(inputDataFileName));
WritableWorkbook copy = Workbook.createWorkbook(new File(inputDataFileName),workbook);
WritableSheet sheet1 = copy.getSheet(inputDataSheetName);
log.info("Testing1 Started")
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
String xmlResponse = '${' + reqOperationName + '#Request' +'}'
def reqholder = groovyUtils.getXmlHolder(context.expand(xmlResponse))
try{
    rowcount = sheet1.getRows();
colcount = sheet1.getColumns();
for(Row in 1..rowcount-1){

    String reqTagName = sheet1.getCell(0,0).getContents()
        log.info reqTagName
        def TagCount = reqholder["count(//*:"+reqTagName+")"]
        if(TagCount!=0){
            String reqTagValue = sheet1.getCell(0,Row).getContents()
            reqholder.setNodeValue("//*:"+reqTagName, reqTagValue)
            reqholder.updateProperty()                              
        }

    //test the request
    testRunner.runTestStepByName(reqOperationName)
log.info("Testing3 Started")
def resholder = groovyUtils.getXmlHolder(reqOperationName+"#Response")
resTagValue1= resholder.getNodeValue("//*:productID")
resTagValue2= resholder.getNodeValue("//*:accountNumber")
resTagValue3= resholder.getNodeValue("//*:insuranceCategory")
resTagValue4= resholder.getNodeValue("//*:imei")
resTagValue5= resholder.getNodeValue("//*:handsetMake")
resTagValue6= resholder.getNodeValue("//*:handsetModel")
resTagValue7= resholder.getNodeValue("//*:insurancePolicyName")
resTagValue8= resholder.getNodeValue("//*:insuranceStartTimestamp")        
//Write Response into excel sheet
Label resValue1= new Label(2,Row,resTagValue1);
sheet1.addCell(resValue1);   
Label resValue2= new Label(3,Row,resTagValue2);
sheet1.addCell(resValue2);
Label resValue3= new Label(4,Row,resTagValue3);
sheet1.addCell(resValue3);
Label resValue4= new Label(5,Row,resTagValue4);
sheet1.addCell(resValue4);
Label resValue5= new Label(6,Row,resTagValue5);
sheet1.addCell(resValue5);
Label resValue6= new Label(7,Row,resTagValue6);
sheet1.addCell(resValue6);
Label resValue7= new Label(8,Row,resTagValue7);
sheet1.addCell(resValue7);
Label resValue8= new Label(9,Row,resTagValue8);
sheet1.addCell(resValue8);      
    }   
}catch (Exception e) {log.info(e)}
finally{
    copy.write();
    copy.close();
    workbook.close();
}
log.info("Testing Over")

1 个答案:

答案 0 :(得分:0)

尝试以下代码。它将解决您的问题:

pusha