Data Source loop creation using groovy script

groovysoapui

I am trying to create a regression test suite for my project in SOAP UI 4.5(no a pro version). I have created groovy script to read request data from excel sheet. The problem which i am facing is that i am not able run test cases in loop. The test case which comes after execution of groovy script is taking the last value only. I want the testcases to run for each iteration(the functionality in SOAP UI pro is implemented using datasource loop). Please suggest some solution. I am new to groovy scripting. Here is the groovy script which i have created up till now :

import org.apache.poi.hssf.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;

InputStream inp = new FileInputStream("workbook.xls");
POIFSFileSystem fs = new POIFSFileSystem(inp);
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
for (HSSFRow row : sheet) {

    def rownum = row.getRowNum();
    log.info rownum;
    def value1 = row.getCell(0).getStringCellValue();
    def value2 = row.getCell(1).getStringCellValue();
    log.info value1;
    log.info value2;
    context.setProperty("companyid",value1)
companyid = context.expand('${companyid}')
context.setProperty("operation",value2)
operation = context.expand('${operation}')
    }

Best Answer

Execution in soapUI is sequential and what is happening in your case is that the groovy code is executed and then soapUI executes the next step as at that time your code has gone through all the rows in the excel and the data in the properties is from the last row in excel that is why the next step is executed with data from the last row of excel.

As you are controlling the flow your tests execution using groovy, i.e., this groovy script is your driver. In this case you also need to include code to call all the steps that are part of the regression script, try the below code from soapUI forum

import com.eviware.soapui.model.testsuite.TestStepResult.TestStepStatus

def testStep = .. create your teststep.. 

def result = testStep.run( testRunner, context )
if( result.status == TestStepStatus.OK )
{
...
}
else
{ 
...
}

Another thing you need to remember is once soapUI has finished executing this groovy script and hence your regression it will move to the next step in the same test case as the groovy step which you want to avoid as they have already been executed in your groovy step. To do this i can suggest two things.

  1. Disable all the steps other than the groovy step. soapUI will skip over the disabled steps. [Link to disable test step] (http://www.soapui.org/forum/viewtopic.php?t=126)
  2. Second and probably my fav, move all the steps you are going to call in your groovy code to a seperate test case/test suite.

Hope this helps!