Forum Discussion

johnapclaw1's avatar
johnapclaw1
New Contributor
16 years ago

keyword-driven excel spreadsheet.

I am not sure if this is helpful but it is way to read in an excel spreadsheet and print the results to a file.
import com.eviware.soapui.model.testsuite.TestRunner
import com.eviware.soapui.impl.wsdl.testcase.*
import java.io.File;
import com.eviware.soapui.support.*;
import java.util.*;
import jxl.*;  // download and add jxl to the bin/ext
import java.lang.*;
def dataArray = new HashMap()
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
String foundKeyword = "";
// if you are adding the parameter mySpreadsheetName as a command line parameter
// spreadsheet = testRunner.testCase.testSuite.project.getPropertyValue("mySpreadsheetName");
spreadsheet = "c:\\accountUser.xls"
Workbook workbook = Workbook.getWorkbook(new File(spreadsheet))
Sheet sheet = workbook.getSheet("Sheet1");
rowcount = sheet.getRows();
colcount = sheet.getColumns();
myResultFileName="c:\\results.log"
def tclog = new File (myResultFileName)
for(table_row = 1; table_row < rowcount; table_row++)
{
// grab the column headers
Cell keywordCell= sheet.getCell(0, table_row)
      Cell parameterCell = sheet.getCell(1, table_row)
Cell inputExpResultsCell = sheet.getCell(2, table_row)
String keyword = keywordCell.getContents()
String parameter = parameterCell.getContents()
String inputExpResult = inputExpResultsCell.getContents()

// test for a keyword
if ((parameter!= " ")&&(parameter !=""))
{

// convert the keyword to uppercase - just in case the test case is in lowercase
switch (keyword.toUpperCase())
{
case "LOGIN":
log.info("keyword = " + keyword)
table_row = createArray( workbook, sheet,table_row, rowcount, dataArray)
def request = testRunner.testCase.getTestStepByName( "login" );
def requestProperty = request.getProperty( "request" );
targetStep.setPropertyValue( "param0", dataArray["user_id"])
targetStep.setPropertyValue( "param1", dataArray["password"])
requestMessage = testRunner.runTestStepByName( testStepName)
printResults(requestMessages, keyword)
break;
case "CREATE_USER":
log.info("keyword = " + keyword)
table_row = createArray( workbook, sheet,table_row, rowcount, dataArray)
def request = testRunner.testCase.getTestStepByName( "createUser" );
targetStep.setPropertyValue( "param0", dataArray["user_id"])
targetStep.setPropertyValue( "param1", dataArray["password"])
requestMessage = testRunner.runTestStepByName( testStepName)
printResults(requestMessages, keyword)
break;

default:
if (keyword != "")
{
log.info("Could not find a case for keyword " + keyword)
}
}
}

}


def createArray(workbook, sheet, table_Row, table_RowCount, dataArray)//(in table, table_RowCount, dataarray[], inout table_Row)
{
done = 0;
dataArray.clear()

String tmpkeyword, lastkeyword, done, tmpDaysSupply, tmpEditDaysSupply;
lastkeyword = null;
for(table_Row; ((table_Row < table_RowCount) && (! done)); table_Row++)
{
keyword = sheet.getCell(0, table_Row).getContents();
parameter = sheet.getCell(1, table_Row).getContents()
inputExpResult = sheet.getCell(2, table_Row).getContents()
tmpkeyword = keyword;
if (lastkeyword == null)
{
lastkeyword = tmpkeyword;
}
if ((tmpkeyword == "") || (tmpkeyword == lastkeyword))
{
dataArray[parameter] = inputExpResult;
lastkeyword = "done";
}
else
{
done = 1;

}
}

return table_Row = table_Row - 2;

}

def printResults(requestMessages, keyword)

myTestType = testRunner.testCase.testSuite.project.getPropertyValue("myTestType");
if (myTestType != "load")
{
def results = requestMessages.getMessages()
def tclog = new File (myResultFileName)
for( i in results )
{
tclog << keyword +" "+i+" \r\n"
}
}
}
The spreadsheet look like
Keyword Parameter Input_Expected_Results
LOGIN username   auser
password apassword
CREATE_USER username anewuser
password newpassword
done done

2 Replies

  • Hi!

    interesting! Where do you set the value of testStepName, which is used in

    requestMessage = testRunner.runTestStepByName( testStepName)

    ?

    regards!

    /Ole
    eviware.com
  • Hi,

    Sorry for the typos in my first post.   

    requestMessage = testRunner.runTestStepByName( testStepName) should be
    requestMessage = testRunner.runTestStepByName("login");
    requestMessage = testRunner.runTestStepByName("create_user");


    Also, the reason we adopted a keyword based spreadsheet is because it allows our subject matter experts (who aren't automation engineers) to write and maintain their test cases.