Forum Discussion

Pankajyadav's avatar
Pankajyadav
New Contributor
11 years ago

Data Driven testing Groovy approach

Step-1
create a test case level property "suite_count"

step-2
create a groovy step read request "ReadRequest" to read parameterized request.

use below code:-
import groovy.xml.XmlUtil
def xmlParser = new XmlParser()
def request = xmlParser.parse("D:/soapui-practice/Request.xml")// configure path of your parameterized(property expension) xml.
context.Request = XmlUtil.serialize(request); //pass this ${Request} as a request body of your soap request
log.info XmlUtil.serialize(request);

step-3
Parse your loaded xml against data set ".xls" file,create first row of headers(used for property expension) and rest rows for testdata values
use below code to parse:-

import java.io.File;
import com.eviware.soapui.support.*;
import java.util.*;
import jxl.*;
import java.lang.*;

Workbook workbook = Workbook.getWorkbook(new File("D:/soapui-practice/InputDatasheet.xls"));
Sheet sheet = workbook.getSheet("Sheet1");
rowcount = sheet.getRows(); //getting total number of rows
colcount = sheet.getColumns(); //getting total number of columns
log.info "Rows :"+rowcount+"--Columns:"+ colcount;

if(testRunner.testCase.hasProperty( "suite_count" ) && testRunner.testCase.getPropertyValue( "suite_count" ) !="0"){
suite_count = testRunner.testCase.getPropertyValue( "suite_count" )

}else{

testRunner.testCase.setPropertyValue( "suite_count" , "1" )
suite_count = testRunner.testCase.getPropertyValue( "suite_count" )

}
suite_count_integer=suite_count.toInteger();
rowcount_integer=rowcount.toInteger();

if(suite_count_integer < (rowcount_integer))
{

int count=1;
for (tc_col=0; tc_col<colcount; tc_col++)

{
Cell cell = sheet.getCell(tc_col,suite_count_integer);


Cell headers = sheet.getCell(tc_col,0);
header = headers.getContents();
value = cell.getContents();
context[header] = value;
log.info header+"=="+value;
count++;
}

}
else if(suite_count_integer>=rowcount_integer){
log.info "Inside else if condition";
testRunner.gotoStepByName( "Execution Complete");


}

Step-4
//After request step loop the data set

use below code;-

suite_count = testRunner.testCase.getPropertyValue( "suite_count" )

log.info('The counter is : ' + suite_count );
curr_counter = suite_count.toInteger()
newcounter = (curr_counter+1);
testRunner.testCase.setPropertyValue( 'suite_count', newcounter.toString());
log.info('The new counter is : ' + newcounter );
testRunner.gotoStepByName( "ParseXML");

step-5
//create an "Execution Complete" step to finish the execution
log.info "+ve testing completed";
testRunner.testCase.setPropertyValue( 'suite_count', 1.toString());

3 Replies

  • Hi Pankaj,

    Thanks for the idea. I tried yours and facing issue on the below line. Could you please help me.

    Workbook workbook = Workbook.getWorkbook(new File("D:/soapui-practice/InputDatasheet.xls"));

    The error looks like this....

    org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script3.groovy: 8:
    unable to resolve class Workbook @ line 8, column 10. Workbook workbook = Workbook.getWorkbook(new File("D:/soapui-practice/InputDatasheet.xls")); ^
    org.codehaus.groovy.syntax.SyntaxException: unable to resolve class Workbook @ line 8, column 10.
    at org.codehaus.groovy.ast.ClassCodeVisitorSupport.addError(ClassCodeVisitorSupport.java:148)
    at org.codehaus.groovy.control.ResolveVisitor.resolveOrFail(ResolveVisitor.java:229)
    at org.codehaus.groovy.control.ResolveVisitor.resolveOrFail(ResolveVisitor.java:239)
    at org.codehaus.groovy.control.ResolveVisitor.transformVariableExpression(ResolveVisitor.java:973)
    ...
    ...
    ...