Forum Discussion

koen_able's avatar
koen_able
Occasional Contributor
5 years ago

Get total DataSource rows incorrect

I used the following short command to get the total number of rows from the DataSource after reading this community question (https://community.smartbear.com/t5/SoapUI-Pro/how-to-count-the-number-of-rows-fetched-in-data-source/m-p/178520/highlight/true#M40637):

 

DSrowCount = testRunner.testCase.testSteps["DataSource"].rowCount
log.info DSrowCount

But this doesn't seem to work, because it returns 1 when there are actually 4 rows. It seems to count only the current row in the DataSource loop. Is there a way to always get the exact number of rows from my Excel file?

Am I doing something wrong? 

 

Maybe nmrao or tech321

 

 

 

  • jsheph01's avatar
    jsheph01
    5 years ago

    I started looking into this a little more and figured out a few things. It looks like value of row count does not read directly from the data source. It only looks at how many rows have been read from the datasource for the current iteration. If you were to set the datasource option "Rows Per Iteration" to 2 then your script to get row count will always be 2 (unless there is only one row left).

     

    I was not able to find a method that would get the total row count so I wrote a script to count them all up. I recommend you run this in a script step before your data source.

     

    def tStep = testRunner.testCase.getTestStepByName("DataSource")
    def data = tStep.getDataSource()
    
    //run the datasource test step
    data.prepare(testRunner,context,data.getPreparedProperties() )
    tStep.run(testRunner,context)
    
    //loop through the file until the end is reached
    def cnt = 1
    while(data.isExhausted() == false) {
    	 data.next(testRunner,context,data.getPreparedProperties() )
    	cnt++
    }
    return cnt

    There might be a better way to do this and I would be courious to see if anyone else knows of a simplier way to get the total row count.

  • Were you running that script as part of a test case when the row count returned one, or did you run the script by itself? If you are running that script outside of a test case you need to preload all the data into the data source. To do that run the datasource by itself and set the max number of rows to 0.

    The command you have is correct, I verified it by running it against a data source and it returned the correct count of all the rows.

    • koen_able's avatar
      koen_able
      Occasional Contributor

      Hi,

       

      Thanks for your reply. It does indeed work when I run the script seperately from running the whole testcase. But I would like to run it as part of a testcase with datasource loop. I have the following sript:

      def dsRowCount = testRunner.testCase.testSteps["DataSource"].rowCount
      
      		int dsRowCountAsInt = dsRowCount as Integer
      		def currentRowCount = testRunner.testCase.getTestStepByName("TestData").getPropertyValue("currentRowDataSource") 
      		int currentRowCountAsInt = currentRowCount as Integer
      		log.info "Current row is: " + currentRowCountAsInt
      		log.info "Total number of rows is: " + dsRowCount
      
      		if(currentRowCountAsInt < dsRowCountAsInt){
      
      			//Add 1 to current row count
      			currentRowCountAsInt = currentRowCountAsInt + 1
      			currentRowCountAsString = Integer.toString(currentRowCountAsInt)
      			log.info "new currentRowCountAsString count = " + currentRowCountAsString
      
      			//Set this new count as currentRowCount
      			testRunner.testCase.getTestStepByName("TestData").setPropertyValue("currentRowDataSource",currentRowCountAsString)
      			
      			assert statusApplicationAsInt == 900 : "Test passed: status 900 reached. Application of 'banksparen' succeeded for row " + currentRowCountAsInt +  "."
      		}

       This is the result: 

      Fri Nov 08 10:03:40 CET 2019: INFO: Current row is: 1
      Fri Nov 08 10:03:40 CET 2019: INFO: Total number of rows is: 1
      Fri Nov 08 10:03:40 CET 2019: INFO: 'isQcautoRunBefore' is set back to 'false' after a complete succesful run

      I expect 

      Total number of rows is: 4

      because there are 4 rows in the Excel file. 

      The following is what happens when running the script seperately (after preloading the Excel datasource):

      Fri Nov 08 10:15:04 CET 2019: INFO: Current row is: 1
      Fri Nov 08 10:15:04 CET 2019: INFO: Total number of rows is: 4
      Fri Nov 08 10:15:04 CET 2019: INFO: new currentRowCountAsString count = 2
      • jsheph01's avatar
        jsheph01
        Contributor

        I started looking into this a little more and figured out a few things. It looks like value of row count does not read directly from the data source. It only looks at how many rows have been read from the datasource for the current iteration. If you were to set the datasource option "Rows Per Iteration" to 2 then your script to get row count will always be 2 (unless there is only one row left).

         

        I was not able to find a method that would get the total row count so I wrote a script to count them all up. I recommend you run this in a script step before your data source.

         

        def tStep = testRunner.testCase.getTestStepByName("DataSource")
        def data = tStep.getDataSource()
        
        //run the datasource test step
        data.prepare(testRunner,context,data.getPreparedProperties() )
        tStep.run(testRunner,context)
        
        //loop through the file until the end is reached
        def cnt = 1
        while(data.isExhausted() == false) {
        	 data.next(testRunner,context,data.getPreparedProperties() )
        	cnt++
        }
        return cnt

        There might be a better way to do this and I would be courious to see if anyone else knows of a simplier way to get the total row count.