Forum Discussion

RobinOng's avatar
RobinOng
Occasional Contributor
9 years ago
Solved

Looping a Datasource in Groovy script (Always starts at last row)

Hi,

 

I have been trying to build a datasource loop in Groovy Script. For testing purposes I can't use the DataSourceLoop step.

Below is the case.

 

I have a Datasource (JDBC) with 4 rows in it and I have build this loop:

 

TSDS1 = testRunner.testCase.getTestStepByName('DataSource')

for (row in 0..TSDS1.rowCount -1)
{
    if (row > 1)
    {
        testRunner.testCase.getTestStepByName('DataSource').Next
    }
    log.info TSDS1.getPropertyValue('Field')
}

 

The problem I have is that CurrentRow of the DataSource is always on the last row.

 

Is there a way to set te currentRow to the first row in Groovy Script?

 

Kind Regards

  • Hi all,

     

    I posted too soon. I figured it out. You can call repositionAtStartRow to reset the source to the first row.

     

    TSDS1.repositionAtStartRow( testRunner, context)

3 Replies

  • RobinOng's avatar
    RobinOng
    Occasional Contributor

    Hi all,

     

    I posted too soon. I figured it out. You can call repositionAtStartRow to reset the source to the first row.

     

    TSDS1.repositionAtStartRow( testRunner, context)

    • punjprakash's avatar
      punjprakash
      New Contributor

      I am not able to go to next row, methods next( testRunner,context) is not working

       

      context.testCase.getTestStepByName("PNRDataSource").setStartRow("0")
      context.testCase.getTestStepByName("PNRDataSource").repositionAtStartRow( testRunner, context)
      currentRow = context.testCase.getTestStepByName("PNRDataSource").getCurrentRow()
      log.info "currentRow First is --" + currentRow
      def rowCount = context.testCase.getTestStepByName("PNRDataSource").rowCount
      log.info "rowCount-- " + rowCount
      for (def i = 0; i < rowCount ; i++) {
      //castToString= Integer.toString(i)
      //log.info "castToString---" + castToString
      //context.testCase.getTestStepByName("PNRDataSource").setStartRow(castToString)
      currentRow = context.testCase.getTestStepByName("PNRDataSource").getCurrentRow()
      log.info "currentRowSecond is --" + currentRow
      execute = context.testCase.getTestStepByName("PNRDataSource").getPropertyValue("Execute")
      log.info "execute is --" + execute
      log.info context.testCase.getTestStepByName("PNRDataSource").next(testRunner,context)
      currentRow = context.testCase.getTestStepByName("PNRDataSource").getCurrentRow()
      log.info "currentRowThird is --" + currentRow
      }

       

      Fri Jul 28 14:32:49 CDT 2017:INFO:currentRow First is --0
      Fri Jul 28 14:32:49 CDT 2017:INFO:rowCount-- 6
      Fri Jul 28 14:32:49 CDT 2017:INFO:currentRowSecond is --0
      Fri Jul 28 14:32:49 CDT 2017:INFO:execute is --N
      Fri Jul 28 14:32:49 CDT 2017:INFO:false
      Fri Jul 28 14:32:49 CDT 2017:INFO:currentRowThird is --0
      Fri Jul 28 14:32:49 CDT 2017:INFO:currentRowSecond is --0
      Fri Jul 28 14:32:49 CDT 2017:INFO:execute is --N
      Fri Jul 28 14:32:49 CDT 2017:INFO:false
      Fri Jul 28 14:32:49 CDT 2017:INFO:currentRowThird is --0
      Fri Jul 28 14:32:49 CDT 2017:INFO:currentRowSecond is --0
      Fri Jul 28 14:32:49 CDT 2017:INFO:execute is --N
      Fri Jul 28 14:32:49 CDT 2017:INFO:false
      Fri Jul 28 14:32:49 CDT 2017:INFO:currentRowThird is --0
      Fri Jul 28 14:32:49 CDT 2017:INFO:currentRowSecond is --0
      Fri Jul 28 14:32:49 CDT 2017:INFO:execute is --N
      Fri Jul 28 14:32:49 CDT 2017:INFO:false
      Fri Jul 28 14:32:49 CDT 2017:INFO:currentRowThird is --0
      Fri Jul 28 14:32:49 CDT 2017:INFO:currentRowSecond is --0
      Fri Jul 28 14:32:49 CDT 2017:INFO:execute is --N
      Fri Jul 28 14:32:49 CDT 2017:INFO:false
      Fri Jul 28 14:32:49 CDT 2017:INFO:currentRowThird is --0
      Fri Jul 28 14:32:49 CDT 2017:INFO:currentRowSecond is --0
      Fri Jul 28 14:32:49 CDT 2017:INFO:execute is --N
      Fri Jul 28 14:32:49 CDT 2017:INFO:false
      Fri Jul 28 14:32:49 CDT 2017:INFO:currentRowThird is --0

      • groovyguy's avatar
        groovyguy
        Champion Level 1

        punjprakash: You are replying to a thread that was already solved over two years ago. You may want to consider starting a new thread for your own issues next time.

         

        That being said, the " next( testRunner,context)" method works for me, but only when actively running a test case (or test suite) as a test. It does not work if I run a stand-alone test. Maybe that'll help.