How to pick a random test object?

let’s say I have 2 buttons = 2 test objects, and when executing i want the test to decide which one of them will be selected. Now given the fact that i already have this 2 objects defined, how should the code look like?
E.g. i have 2 buttons > “male”, “female”, in a gender page. Whenever i run a test case, the script should pick one of them randomly and continue executing.

Hello,

put objects you want to use randomly into a list and use following code:

List<String> randomObjects = Arrays.asList('Path/To/TestObject1', 'Path/To/TestObject2', 'Path/To/TestObject3')Random rand = new Random()String randomPath = randomObjects.get(rand.nextInt(randomObjects.size()))WebUI.click(findTestObject(randomPath)) 

randomPath variable will contain one of predefined test objects paths.

3 Likes

Marek Melocik said:

Hello,

put objects you want to use randomly into a list and use following code:

List<String> randomObjects = Arrays.asList('Path/To/TestObject1', 'Path/To/TestObject2', 'Path/To/TestObject3')Random rand = new Random()String randomPath = randomObjects.get(rand.nextInt(randomObjects.size()))WebUI.click(findTestObject(randomPath)) 

**randomPath** variable will contain one of predefined test objects paths.

  

Can we use findTestObject instead of objects path in arrayList ?

List<TestObjects> randomObjects = Arrays.asList(findTestObject('Path/To/TestObject1'), findTestObject('Path/To/TestObject2'), findTestObject('Path/To/TestObject3'))

so whenever i rename any object it is automatically rename in testcase

Definitely! Nice solution. Just declare it as List<TestObject**>** :slight_smile:

Hi,

Thank you for your help
But I get some errors when running the test cases. Could you please help me resolve it?

Thanks,

"groovy.lang.MissingMethodException: No signature of method: java.util.Random.call() is applicable for argument types: (java.lang.Class) values: [class java.lang.String]

Possible solutions: wait(), any(), wait(long), each(groovy.lang.Closure), any(groovy.lang.Closure), ints()

at quickBetBetting_singleBet.run(quickBetBetting_singleBet:26)

at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)

at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)

at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:369)

at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:360)

at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:339)

at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:331)

at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:248)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:142)

at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:133)

at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)

at"

The above error message means that you do not have the proper data type(s) for the parameter(s) for the method you are using. As an example, perhaps a parameter is supposed to be an Integer and you are using a String.

You do not have any code showing, so I assume you are using something like below:

List<String> randomObjects = Arrays.asList('Path/To/TestObject1', 'Path/To/TestObject2', 'Path/To/TestObject3')
Random rand = new Random()
String randomPath = randomObjects.get(rand.nextInt(randomObjects.size()))
WebUI.click(findTestObject(randomPath)) 

The next part of the error message indicates which method you were using:

java.util.Random.call() is applicable for argument types: (java.lang.Class) values: [class java.lang.String]

That’s likely from the line:

String randomPath = randomObjects.get(rand.nextInt(randomObjects.size()))

IMHO, something you are doing in the parameter for the get function is making it a String, and not an Integer:
rand.nextInt(randomObjects.size())

Look at that for a start or give us more information on your concern and your code.