1

I have a question in regards in wanting to know how to select a random row within a CSV using Groovy.

Currently I have two groovy scripts (well actually three but the third one is not important for this question). One groovy script is called 'ReadData' where it reads the csv file and sets it to row one, and then there is another step called 'SetProperties' where like it states sets the property value to what ever the value is obtained form the CSV.

Now the scripts below works in finding the second row in the CSV file under the first column of the spreadsheet and setting it as a property value. My question is instead of selecting the second row, how can I select a random row from the spreadsheet (we don't select from the first row ever of the spreadsheet as that's our headers).

i did try replacing within SetProperties:

testRunner.testCase.setPropertyValue( "id", singleLineArray[0])) 

with

testRunner.testCase.setPropertyValue('departureAirportId', String.valueOf((int)Math.random()*singleLineArray[0]))

but no luck as it errored.

BruceyBandit
  • 3,978
  • 19
  • 72
  • 144

1 Answers1

1

Possible solution

I've never used Groovy so take this with a grain of salt, but it should bring you closer to what you want. This answer helped me :

Random random = new Random(); // initialize this somewhere once in your code
int randomRowId = 1 + random.nextInt(totalRecords); // Random integer between 1 and totalRecords (both inclusive).
testRunner.testCase.setPropertyValue('departureAirportId', DataTable[randomRowId][0]);

Your code

testRunner.testCase.setPropertyValue('departureAirportId', String.valueOf((int)Math.random()*singleLineArray[0]))

The second parameter is :

String.valueOf( (int) Math.random() * singleLineArray[0] )

You still extract the first element of your singleLineArray, and try to multiply it with a random float between 0 and 1. Here's the reason why it didn't work.

Community
  • 1
  • 1
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
  • Yeah i'm not too familiar with goovy scripting as well. only been using it this week. Still can;t seem to get it working. States outofbounds exception for the array 'testRunner.testCase.setPropertyValue('departureAirportId', singleLineArray[randomRowId])' – BruceyBandit Jan 08 '17 at 16:36
  • out of bounds? Weird. What's the value of `totalRecords`, the size of `singleLineArray` and the value of `randomRowId`? – Eric Duminil Jan 08 '17 at 16:57
  • Can't seem to log the data. Total Records should be 6 (6 rows in csv file), singleLineArray should be 1 and RandomRowId changed. The error i get is java.lang.ArrayIndexOutOfBoundsException: 5, but this value changes between 1-5 – BruceyBandit Jan 08 '17 at 17:15
  • Sorry, I was confused. It should be `DataTable`, not `singleLineArray`. I thought `singleLineArray` was an Array of lines. – Eric Duminil Jan 08 '17 at 17:19
  • line 18 is `testRunner.testCase.setPropertyValue('id', DataTable[randomRowId]);` – BruceyBandit Jan 08 '17 at 17:32
  • Updated again, you need `DataTable[randomRowId][0]` – Eric Duminil Jan 08 '17 at 17:33
  • Awesome, i thought I had to change toString but it is the [0]. thank you very much. I've been stuck on this since yesterday as I am still a groovy noob. There is one question I've been stuck on for 2 days if you fancy a go...http://stackoverflow.com/questions/41522279/how-to-cast-null-to-0-in-groovy-scripting – BruceyBandit Jan 08 '17 at 17:36