Random select value from Data file

I created a data file via excel sheet having 100 entries. I want to read a random value from the cell using random function. Please can someone help with the code.

Every time i run test case, random value to be input into the fields. See sample test data.

data.PNG

Hi Olagoke,

You can try this code:

String[] array = findTestData(“Data Files/YOUR TEST DATA”).getAllData()

int random = new Random().nextInt(array.length)

println array[random].toString()

Hope that helps. . . :slight_smile:

3 Likes

Also can try this:

Random rand = new Random()

DBData listUsers = findTestData(‘katalonFolderPath/yourDataFile’)
int rowCount = listUsers.getRowNumbers()
rowNum= (rand.nextInt(rowCount - 1) + 1)
String firstName = listUsers.getValue(1, rowNum)
String lastName = listUsers.getValue(2, rowNum)

This will define two string variables you can pass into fields. It will always pick the two next to each other and randomized based upon size of your life. you can also comment them if you want to see what they are with.
WebUI.comment(firstName)

Or put them as a single value such as:
String firstName = listUsers.getValue(1, rowNum)
String lastName = listUsers.getValue(2, rowNum)
String userName = firstName + ’ ’ + lastName

2 Likes

Thanks BL and Arnel. Both are great solutions worked well. I used them in the Keyword function.

2 Likes

You’re welcome. Glad it works. . . :slight_smile:

1 Like

The entire code is already posted

can anyone has a code for creating custom keyword while selecting random value from a an excel.

dear @Olagoke_Alli Kindly I am also stuck in that issue …kindly i need ur help

I think you will have to be more exacting on what your concern is with the code that @Arnel and @B_L have given above to do just that; select random value from an Excel sheet.