Forum Discussion

guarav's avatar
guarav
Occasional Contributor
15 years ago

groovy script code for data driven testing

Hi,

i m using soapUI 2.5.1.i have to test a soap request on mutiple set of data.
How do i write groovy script to get data from excel file and input it to soap request.

Thanks
Guarav

3 Replies

  • logesh's avatar
    logesh
    Occasional Contributor
    Hi Guarav,

    This is peice of code I use to write data from Excel into Request.

    Step 1 : Download jxl or JExcel API and copy them into C:\Program Files\eviware\soapUI-2.5.1\bin\ext folder.
    Step 2 :Re-start the application.
    Step 3: Paste the below script into the Groovy Script - test step

    Code:

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

    // VARIABLE TO HOLD THE DATA SHEET NAME
    sDataSheetName = "SearchCustomer.xls"

    // TO CREATE VARIABLES TO HOLD THE RESQUEST VALUES
    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
    def request  = groovyUtils.getXmlHolder("SearchCustomer#Request")

    // TO GET THE TEST CASE NAME
    def testCase = testRunner.getTestCase()
    Name= testCase.getName()

    // DECLARING VARIABLES FOR GLOBAL PROPERTIES
    def projectdir = context.expand('${#Global#PATH}');
    Workbook workbook = Workbook.getWorkbook(new File(projectdir + sDataSheetName))
    Sheet sheet = workbook.getSheet("Input");
    rowcount = sheet.getRows();
    colcount = sheet.getColumns();

    // CHECK WHETHER THE TESTCASE NAMES MATCH
    for (tc_row in 2..rowcount)
    {
    Cell a1 = sheet.getCell(0,tc_row)
    String  s1 = a1.getContents()
    if ( Name == s1 )
    {
    current_row = tc_row
    break
    }
    }

    // TO CHECK FOR THE HEADER TAGS
    for (tc_column in 1..colcount-1)
    {
    Cell a2= sheet.getCell(tc_column,1)
    String s2 = a2.getContents()
    if (s2 != "" )
    {
    Cell a3= sheet.getCell(tc_column,current_row)
    String s3 = a3.getContents()
    //UISupport.showInfoMessage("${s3}")
    tag = s3
    //SETS INPUT VALUES IN CORRESPONDING NODE
    request.setNodeValue("//" + s2, "${tag}")
    request.updateProperty()

    }
    }

       
    //**************************************** END OF TEST STEP *************************************//
     
    Refer the attached Search.xls sheet I have used. Use similar Excel.

    :-)
       
  • guarav's avatar
    guarav
    Occasional Contributor
    HI,

    Above script is not working from line:

    Workbook workbook      =    Workbook.getWorkbook(new File(projectdir + sDataSheetName))

    Thanks & Regards
    Gaurav
  • You can also save your Excel file as CSV and use a DataSource to read it from SoapUI Pro...