0
votes

I am trying a data driven approach in soap ui using groovy script and I wanted to export each of the dynamic request and response from MessageExchange to an Excel sheet. I am using the input data Excel file "dataFile.xls" for a simple "Login" operation. It consists of 2 fields Username and Password with 4 records. My script is doing a data driven approach and executing all the records from the excel file. But I wanted to export all the four dynamic request and response into an Excel file "Output.xls". My soap pack is having 2 groovy test request(one for data driver and the other for looping), one property test step to hold the input element properties and temporary variables, one soap test request "login". Below is the code snippet I am using in the "data driver" groovy test step.

// 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") 
}

I am getting the fault "groovy.lang.MissingMethodException: No signature of method: com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext.getRequestContent() is applicable for argument types: () values: [] error at line: 50"

Could some one please help me in this. The main objective is to get all the dynamic request and response from the MessageExchange and export all the data into the excel sheet.

2

2 Answers

0
votes

Your program throws the exception there:

def testStepContext = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext(temp)
def reqq = testStepContext.getRequestContent()
log.info(reqq)

The exception is pretty clear MissingMethodException so the problem is that com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext doesn't have getRequestContent() method.

Anyway in your code you only use reqq on log.info(reqq)... then without analyzing the whole code the easy solution to solve the problem is simply remove this lines due they are useless.

0
votes

You better make use of project level event "TestRunListener.afterStep" to accomplish your task.

In this event you'll have access to testRunner, context & testStepResult Objects. The following code snippet can help you.

For Soap Services:

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

For Rest Services:

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