How to select random data from a CSV?

As the title says I’ve been searching how to randomly pick a data from the CSV or Test Data.

What I’m trying to do is fill up First Name, Last Name and Middle Name with random names from my CSV when creating a user. I already have the CSV with firstName, lastName and middleName with 1000 names under them.

So far I am able to run it with this code

WebUI.setText(findTestObject('Employee/Add Employee/input_firstName'), findTestData('TestData').getValue(firstName, 22))WebUI.setText(findTestObject('Employee/Add Employee/input_lastName'), findTestData('TestData').getValue(lastName, 88))WebUI.setText(findTestObject('Employee/Add Employee/input_middleName'), findTestData('TestData').getValue(middleName, 1))

But I want to change those numbers to random so everytime I run it, it will choose random names from the list. I’ve already tried using RandomStringUtils.randomNumeric(3). I’m not very familiar with coding so I’m not sure what to do. Thanks in advance

Hi Hailey,

You can try to do this:

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

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

WebUI.setText(findTestObject('Employee/Add Employee/input_firstName'), findTestData('TestData').getValue(firstName, random + 1))WebUI.setText(findTestObject('Employee/Add Employee/input_lastName'), findTestData('TestData').getValue(lastName, random + 1))WebUI.setText(findTestObject('Employee/Add Employee/input_middleName'), findTestData('TestData').getValue(middletName, random + 1))
Hope that helps. . . :)
3 Likes