Forum Discussion

del_exaclee's avatar
del_exaclee
New Contributor
6 years ago

ReadyAPI 2.5.0 - Parse jdbc response in Groovy script (until max row number value)

I am using a JDBC request in ReadyAPI 2.5.0 to retrieve data from our database. What I need to do is loop through all of the rows that are returned and pick out specific values for use in my SOAP request later on.

 

How do I use a Groovy script to take a JDBC response and loop through until all rows have been read? Please note the JDBC response will return a dynamic set of results each time so its important to loop until all rows have been processed.

 

I have included a sample JDBC response below for which we would need to extract values i.e. UNIQUEID and ROUTEID using a Groovy script and pass that into my SOAP request as a test case property.


<Results>
   <ResultSet fetchSize="128">
      <Row rowNumber="1">
         <UNIQUEID>80382049</UNIQUEID>
         <SOURCESYSTEM>HitsSC</SOURCESYSTEM>
         <ROUTEID>39812</ROUTEID>
         <SHIFTDATE>2018-12-07 00:00:00.0</SHIFTDATE>
      </Row>
      <Row rowNumber="2">
         <UNIQUEID>80382096</UNIQUEID>
         <SOURCESYSTEM>NTExchange</SOURCESYSTEM>
         <ROUTEID>39812</ROUTEID>
         <SHIFTDATE>2018-12-07 00:00:00.0</SHIFTDATE>
      </Row>
      <Row rowNumber="3">
         <UNIQUEID>80382097</UNIQUEID>
         <SOURCESYSTEM>NTExchange</SOURCESYSTEM>
         <ROUTEID>39812</ROUTEID>
         <SHIFTDATE>2018-12-07 00:00:00.0</SHIFTDATE>
      </Row>
      <Row rowNumber="4">
         <UNIQUEID>80382098</UNIQUEID>
         <SOURCESYSTEM>NTExchange</SOURCESYSTEM>
         <ROUTEID>39812</ROUTEID>
         <SHIFTDATE>2018-12-07 00:00:00.0</SHIFTDATE>
      </Row>
   </ResultSet>
</Results>

Regards,

Ajay

5 Replies

  • Radford's avatar
    Radford
    Super Contributor

    I like to use the XmlSlurper to parse XML data, here is a standalone example with your data:

     

    import groovy.util.XmlSlurper
    
    def xmlData = '''\
    <Results>
       <ResultSet fetchSize="128">
          <Row rowNumber="1">
             <UNIQUEID>80382049</UNIQUEID>
             <SOURCESYSTEM>HitsSC</SOURCESYSTEM>
             <ROUTEID>39812</ROUTEID>
             <SHIFTDATE>2018-12-07 00:00:00.0</SHIFTDATE>
          </Row>
          <Row rowNumber="2">
             <UNIQUEID>80382096</UNIQUEID>
             <SOURCESYSTEM>NTExchange</SOURCESYSTEM>
             <ROUTEID>39812</ROUTEID>
             <SHIFTDATE>2018-12-07 00:00:00.0</SHIFTDATE>
          </Row>
          <Row rowNumber="3">
             <UNIQUEID>80382097</UNIQUEID>
             <SOURCESYSTEM>NTExchange</SOURCESYSTEM>
             <ROUTEID>39812</ROUTEID>
             <SHIFTDATE>2018-12-07 00:00:00.0</SHIFTDATE>
          </Row>
          <Row rowNumber="4">
             <UNIQUEID>80382098</UNIQUEID>
             <SOURCESYSTEM>NTExchange</SOURCESYSTEM>
             <ROUTEID>39812</ROUTEID>
             <SHIFTDATE>2018-12-07 00:00:00.0</SHIFTDATE>
          </Row>
       </ResultSet>
    </Results>'''
    
    def Results = new XmlSlurper().parseText(xmlData)
    
    Results.ResultSet.Row.each(){row ->
    	log.info('UNIQUEID = ' + row.UNIQUEID + '; ROUTEID = ' + row.ROUTEID)
    }
    • del_exaclee's avatar
      del_exaclee
      New Contributor

      Thanks for this Radford. A couple of things:

       

      1) I need the code to dynamically take its input from a JDBC response as this code will need to run 100 times for our test requirements so I need to parse the JDBC response as XML dynamically. 

      2) Once I have extracted that data from the JDBC response, can I populate this data into a single test case property so that I can inject it into a SOAP request?

      Regards,

      Ajay

      • Radford's avatar
        Radford
        Super Contributor

        To get the results from the JDBC step I would just use the point and click Get Data functionality. Just right click in the groovy script, select the Get Data item, and select your desired data.

         

        As for setting properties via Groovy script. See the following link for examples:

         

        https://support.smartbear.com/readyapi/docs/testing/scripts/samples/variable.html

         

        But in short, you will see that the test suites, cases and steps all have a setPropertyValue method, so from a Groovy script test step you'll write something like:

         

        context.getTestCase().getTestStepByName('StepName').setPropertyValue( 'PropName', 'Value' )