Clicking a Random Test Object from List of Test Objects

I am using Katalon to automate a test questionnaire but have no coding experience to support my project. Since I am working on a questionnaire, I would like Katalon to click on a random answer out of 4 possible answers for each of my questions. I’ve looked through the thread and found some code but cannot get it to work for the life of me. Any help would be greatly appreciated!

List randomObjects = Arrays.asList(findTestObject(‘TESTOBJECT1’),
findTestObject(‘TESTOBJECT2’),
findTestObject(‘TESTOBJECT3’),
findTestObject(‘TESTOBJECT4’)) Random rand = new Random()String randomPath = randomObjects.get(rand.nextInt(randomObjects.size()))WebUI.click(findTestObject(randomPath))

I would say you are so close. It looks like only on the last line, you should not use “findTestObject” as you have that phrase in your element array. Instead, just use randomPath. Try that and see if you got it.

And if you should post code again, here is a manner to properly display/format the code:

WebUI.openBrowser(’’)

WebUI.navigateToUrl(‘https://www.saucedemo.com/’)

WebUI.setText(findTestObject(‘Object Repository/Page_Swag Labs/input_standard_userlocked_out_userproblem_u_db77ac’), ‘standard_user’)

WebUI.setEncryptedText(findTestObject(‘Object Repository/Page_Swag Labs/input_standard_userlocked_out_userproblem_u_3423e9’),
‘qcu24s4901FyWDTwXGr6XA==’)

WebUI.click(findTestObject(‘Object Repository/Page_Swag Labs/input_standard_userlocked_out_userproblem_u_0dff71’))

List randomObjects = Arrays.asList(findTestObject(‘Object Repository/Page_Swag Labs/button_Add to cart’),
findTestObject(‘Object Repository/Page_Swag Labs/button_Add to cart_1’),
findTestObject(‘Object Repository/Page_Swag Labs/button_Add to cart_1_2’),
findTestObject(‘Object Repository/Page_Swag Labs/button_Add to cart_1_2_3’),
findTestObject(‘Object Repository/Page_Swag Labs/button_Add to cart_1_2_3_4’),
findTestObject(‘Object Repository/Page_Swag Labs/button_Add to cart_1_2_3_4_5’))

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

WebUI.click(findTestObject(randomPath))

WebUI.click(findTestObject(‘Object Repository/Page_Swag Labs/span_6’))

WebUI.click(findTestObject(‘Object Repository/Page_Swag Labs/button_Open Menu’))

WebUI.click(findTestObject(‘Object Repository/Page_Swag Labs/a_Logout’))

WebUI.closeBrowser()

Click on two random test objects from the list of test objects

I found this thread on the forum / Clicking a Random Test Object from List of Test Objects/. After editing the code, I did not reach a successful solution. But I would need help from someone to achieve - Click on two random test objects from the list of test objects.

You did not say what concerns you are having, but a small item that I see is in your statement:

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

needs to become like below (parentheses after size):

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

so, what exception(s) are you getting?

As a reminder, put triple slanted quotes (found above the TAB key) before and after your code.

Another small item is when you move off a page, you should apply a wait statement, such as:

WebUI.click(findTestObject('Object Repository/Page_Swag Labs/input_standard_userlocked_out_userproblem_u_0dff71'))

WebUI.waitForPageLoad(10)

"click on two random items"
for (int i = 0; i < 2; i++) {
    randomPath = randomObjects.get(rand.nextInt(randomObjects.size()))
    WebUI.waitForElementVisible(randomPath)
    WebUI.click(randomPath)
}

Thank you for your reply and advice.

WebUI.click(findTestObject(‘Object Repository/Page_Swag Labs/input_standard_userlocked_out_userproblem_u_0dff71’))

WebUI.waitForPageLoad(10)

List randomObjects = Arrays.asList(findTestObject(‘Object Repository/Page_Swag Labs/button_Add to cart’),
findTestObject(‘Object Repository/Page_Swag Labs/button_Add to cart_1’),
findTestObject(‘Object Repository/Page_Swag Labs/button_Add to cart_1_2’),
findTestObject(‘Object Repository/Page_Swag Labs/button_Add to cart_1_2_3’),
findTestObject(‘Object Repository/Page_Swag Labs/button_Add to cart_1_2_3_4’),
findTestObject(‘Object Repository/Page_Swag Labs/button_Add to cart_1_2_3_4_5’))

Random rand = new Random()

String randomPath = ‘’

“and click on two random items”
for (int i = 0; i < 2; i++) {
randomPath = randomObjects.get(rand.nextInt(randomObjects.size()))
WebUI.waitForElementVisible(randomPath)
WebUI.click(randomPath)
}

WebUI.click(findTestObject(‘Object Repository/Page_Swag Labs/span_6’))

I modified the code, but it showed me this error.`
=============== ROOT CAUSE =====================

For trouble shooting, please visit: https://docs.katalon.com/katalon-studio/docs/troubleshooting.html

11-18-2021 09:51:12 AM Test Cases/New Test Case_Sauce_1 - Random - Copy

Elapsed time: 27,745s

Test Cases/New Test Case_Sauce_1 - Random - Copy FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.waitForElementVisible() is applicable for argument types: (java.lang.String) values: [TestObject - ‘Object Repository/Page_Swag Labs/button_Add to cart_1_2_3_4’]
Possible solutions: waitForElementVisible(com.kms.katalon.core.testobject.TestObject, int), waitForElementVisible(com.kms.katalon.core.testobject.TestObject, int, com.kms.katalon.core.model.FailureHandling), waitForElementNotVisible(com.kms.katalon.core.testobject.TestObject, int)
at New Test Case_Sauce_1 - Random - Copy.run(New Test Case_Sauce_1 - Random - Copy:47)
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:442)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:433)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:412)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:404)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:281)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:138)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:129)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1637225457864.run(TempTestCase1637225457864.groovy:25)

The concern is KS does not consider randomPath to be a TestObject, the data type needed for the statement, waitForElementVisible(). KS considers it to be a String. The concern will also apply to the statement, click(). My bad. Change this line of code:

String randomPath = ''

to become:

TestObject randomPath;

"and click on two random items"
for (int i = 0; i < 2; i++) {
    randomPath = randomObjects.get(rand.nextInt(randomObjects.size()))
    WebUI.waitForElementVisible(randomPath)
    WebUI.click(randomPath)
}

WebUI.click(findTestObject('Page_Swag Labs/span_6'))

To be on the safe side, add this to the top of your code:

import com.kms.katalon.core.testobject.TestObject

You may note that the selection of a random item worked, as the choice was:
values: [TestObject - 'Object Repository/Page_Swag Labs/button_Add to cart_1_2_3_4']

An alternative is to store the String and then create the TestObject at the random point

List<String> randomObjects = Arrays.asList('Page_Swag Labs/button_Add to cart',
'Page_Swag Labs/button_Add to cart_1',
'Page_Swag Labs/button_Add to cart_1_2',
'Page_Swag Labs/button_Add to cart_1_2_3',
'Page_Swag Labs/button_Add to cart_1_2_3_4',
'Page_Swag Labs/button_Add to cart_1_2_3_4_5')

Random rand = new Random()

String randomPath = ''

"click on two random items"
for (int i = 0; i < 2; i++) {
    randomPath = randomObjects.get(rand.nextInt(randomObjects.size()))
    WebUI.waitForElementVisible(findTestObject(randomPath))
    WebUI.click(findTestObject(randomPath))
}

WebUI.click(findTestObject('Page_Swag Labs/span_6'))

Thank you, but I’m probably making a mistake somewhere. I tried to change the code in both cases, but I don’t have a result.

wrong:

    WebUI.waitForElementVisible(findTestObject(randomPath))

right:

    WebUI.waitForElementVisible(findTestObject(randomPath), 10)

You need to specify a value for timeout of int type

Kazurayam, thank you for your help, the test works reliably. But when I changed the parameters to select 3 random objects - it worked once the second and third times it didn’t work anymore.


You can help me.

You may be encountering a previously used value. Insert a print statement of the values that are randomly generated in your loop to see if this is true.

"click on three random items"
for (int i = 0; i < 3; i++) {
    randValue = rand.nextInt(randomObjects.size())
    WebUI.comment("random value of ${randValue}")
    println("random value of ${randValue}")
    randomPath = randomObjects.get(randValue)
    WebUI.waitForElementVisible(findTestObject(randomPath), 10)
    WebUI.click(findTestObject(randomPath))
}

WebUI.click(findTestObject('Page_Swag Labs/span_6'))

I entered a print order with values ​​that are randomly generated.

when selecting 2 random objects it passed

The error message indicates there is a concern with the pathway (xpath or whatever) that you have for the “…button_Add to cart_1_2_3” reference. The error message indicates that at the time the element is chosen, the pathway you have to that element does not exist. Do you have multiple pathways selected (or has the element already been selected)? You will have to review your pathway (xpath or whatever) for this element to ensure they are valid (when chosen)?

And what items were chosen when you did the loop with three? Was one item chosen twice?