Forum Discussion

nats's avatar
nats
New Contributor
16 years ago

Inserting values or datas into request by using grovvy scripts

HI
I am working on groovy scripting
I written script to access data from external file like xls,or csv successfully.

But I am facing problem to inserting same data or values in request  XML in soapui tool to run test.

Can you pls provide me code to pick data from xls and insert it to request dynamically for mor than one iteration for the same request.(Its just like parametrization of data's  running script for n number of iterations).

Please provide me ASAP I stuck up in between for this issue.

If this approach works fine its very useful to Soapui tool automation using groovy


Here is the groovy script to get values from xls and need how to pass it to soupui request.


//Fetches excel data if any value is present in Iteration Column.Breaks immediately when finds a null value//

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



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

// DECLARING VARIABLES FOR GLOBAL PROPERTIES
//def projectdir       =    context.expand('${#Global#PATH}');
   Workbook workbook    =    Workbook.getWorkbook(new File("C:/Documents and Settings/abc/Desktop/Orange/POC/Excel/Input_Data.xls"));
   Sheet sheet          =    workbook.getSheet("Sheet2");
   rowcount             =    sheet.getRows();
    log.info("rowcount = $rowcount");
   colcount             =    sheet.getColumns();
    log.info("colcount = $colcount");
 
//Getting rowcount and incrementing it
     
   for(r=1;r   {
     Cell a1= sheet.getCell(0,r);
     String s1 =a1.getContents();
      // log.info ("string value in cell s1 : $s1");
     if (s1 != "")
     {     
//Fetch header fields only if Value is present in Increment column
        for(c=1;c        {
           Cell a2= sheet.getCell(c,0);
           String s2 =a2.getContents();
         //     log.info ("string value in cell s2 : $s2");
              if (s2 != "")
              {
//Fetch the values of the current cell only if header is present   
              Cell a3= sheet.getCell(c,r);
     String s3 =a3.getContents();
log.info ("string value in cell s3 : $s3");
    }
    else break;
         }
         
     }
           else break;
   } 
     
     


My request is
which i need to pass values




   

   
     
         
           
               

                  new
                  ?
                  ?
                  ?
                  ?
                  ?
               

               
                 
                     
                     9900118820
                     
                     ?
                     
                     ?
                 

                  ?
               

           

         

     

   
  • nats's avatar
    nats
    New Contributor
    Hi Can any one help me out for the above query its very urgent for me
  • I found this to work:

    Using the request xmlholder,
      def holder = groovyUtils.getXmlHolder( "#Request" )

    I get the xml nodes in question
      def phoneNode = holder.getDomNode('//pat:PersonSearchCriteria[1]/pat:PhoneNumber[1]')
      def acNode = holder.getDomNode( '//pat:PersonSearchCriteria[1]/pat:AreaCode[1]' )
     
    I then set the values for the 'Value' of each node
      phoneNode.getFirstChild().setNodeValue(srchPhone)
      acNode.getFirstChild().setNodeValue(srchAC)
     
    I then get the actual test case Step of where to store the request
      def dsStep = testRunner.testCase.getTestStepByName('')
     
    And store my modified xml in that step's Request property.
      dsStep.setPropertyValue('Request', holder.getXml())

    I had to experiment a bit to get the getFirstChild() portion.

    I also determined that a Response property appears to be read only.

    Hope this helps.