使用Groovy Script,如何在数据驱动方法中访问存储在MessageExchange中的动态soap请求?

时间:2016-03-11 06:24:52

标签: groovy soapui

我正在使用groovy脚本在soap ui中尝试数据驱动方法,我想将每个动态请求和响应从MessageExchange导出到Excel工作表。我使用输入数据Excel文件“dataFile.xls”进行简单的“登录”操作。它由2个字段组成,用户名和密码,共4条记录。我的脚本正在执行数据驱动的方法并执行excel文件中的所有记录。但我想将所有四个动态请求和响应导出到Excel文件“Output.xls”中。我的soap pack有2个groovy测试请求(一个用于数据驱动程序,另一个用于循环),一个属性测试步骤用于保存输入元素属性和临时变量,一个soap测试请求“login”。下面是我在“数据驱动程序”groovy测试步骤中使用的代码片段。

// IMPORT THE LIBRARIES WE NEED

import com.eviware.soapui.support.XmlHolder 
import jxl.* 
import jxl.write.*
import com.eviware.soapui.model.iface.MessageExchange


// DECLARE THE VARIABLES

def myTestCase = context.testCase //myTestCase contains the test case 
log.info(myTestCase)
def counter,next,previous,size //Variables used to handle the loop and to move inside the file 
Workbook workbook1 = Workbook.getWorkbook(new File("d:\\Groovy videos\\dataFile.xls")) //file containing the data 
Sheet sheet1 = workbook1.getSheet(0) //save the first sheet in sheet1 
size = sheet1.getRows().toInteger() //get the number of rows, each row is a data set 
log.info(size)
propTestStep = myTestCase.getTestStepByName("Property - Looper") // get the Property TestStep object 
log.info(propTestStep)
req = myTestCase.getTestStepByName("login").getProperty("Request").getValue() 
//log.info(req)

propTestStep.setPropertyValue("Total", size.toString())
counter = propTestStep.getPropertyValue("Count").toString() //counter variable contains iteration number 
counter = counter.toInteger() // 
next = (counter > size-2? 0: counter+1) //set the next value 

// OBTAINING THE DATA YOU NEED

Cell u = sheet1.getCell(0,counter) // getCell(column,row) //obtains user 
Cell p = sheet1.getCell(1,counter) // obtains password 
workbook1.close() //close the file

////////////////////////////////////

usr = u.getContents() 
pass = p.getContents() 
propTestStep.setPropertyValue("user", usr) //the value is saved in the property 
propTestStep.setPropertyValue("pass", pass) //the value is saved in the property 
propTestStep.setPropertyValue("Count", next.toString()) //increase Count value 
next++ //increase next value 
propTestStep.setPropertyValue("Next", next.toString()) //set Next value on the properties step

WritableWorkbook workbook2 = Workbook.createWorkbook(new File("d:\\output.xls"))
WritableSheet sheet2 = workbook2.createSheet("Request", 0)
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context) 
def temp=testRunner.testCase.testSteps["login"]

def testStepContext = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext(temp)
def reqq = testStepContext.getRequestContent()
log.info(reqq)
//def temp=testRunner.testCase.testSteps.testRequest.getRequestContent()
//holder = groovyUtils.getXmlHolder(messageExchange.requestContent)
log.info("request : "+temp)
Label label1 = new Label(0, 6, temp);
sheet2.addCell(label1);
workbook2.write()
workbook2.close()

//Decide if the test has to be run again or not 
if (counter == size-1) 
{ 
    propTestStep.setPropertyValue("StopLoop", "T") 
    log.info "Setting the stoploop property now..." 
} 
else if (counter==0) 
{ 
    def runner = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner(testRunner.testCase, null) 
    propTestStep.setPropertyValue("StopLoop", "F")
} 
else 
{ 
    propTestStep.setPropertyValue("StopLoop", "F") 
}

我收到了错误 “ groovy.lang.MissingMethodException:没有方法签名:com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext.getRequestContent()适用于参数类型:()值:[]错误在行:50 < /强>“

有人可以帮助我。 主要目标是从MessageExchange获取所有动态请求和响应,并将所有数据导出到Excel工作表中。

2 个答案:

答案 0 :(得分:0)

你的程序在那里抛出异常:

int fd[2];

if (pipe(pipefd) == -1) {       
    perror("pipe");
    exit(EXIT_FAILURE);
}

child_pid = fork();

异常非常清楚def testStepContext = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext(temp) def reqq = testStepContext.getRequestContent() log.info(reqq) 所以问题是com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext没有MissingMethodException方法。

无论如何,在您的代码中,您只在getRequestContent()上使用reqq ...然后在不分析整个代码的情况下解决问题的简单解决方案就是删除这些行,因为它们没用。

答案 1 :(得分:0)

您最好使用项目级别事件&#34; TestRunListener.afterStep&#34;完成你的任务。

在这种情况下,您可以访问testRunner,context&amp; testStepResult对象。以下代码段可以为您提供帮助。

肥皂服务:

if (context.getCurrentStep() instanceof WsdlTestRequestStep) {
    WsdlTestStepResult soapResult= (WsdlTestStepResult) testStepResult;
    log.info "Request: "+soapResult.getRequestContent();
    log.info "Response: "+soapResult.getResponseContent();
}

休息服务:

if (context.getCurrentStep() instanceof RestTestRequestStep) {
    RestRequestStepResult restResult= (RestRequestStepResult) testStepResult;
    log.info "Request: "+restResult.getRequestContent();
    log.info "Response: "+restResult.getResponseContent();
}