Random value in xpath for WebUI.click, WebUI.scrollToElement

Hello,

I kindly ask for your help.

I have a list of records.
I need to click random one.
Test passes without variable.

Should I add something to import java.util.concurrent.ThreadLocalRandom?

Thank you in advance!

xpath (works with number, but does not work with variable index):
//div[contains(@fxlayoutalign, "start start ")][1]/div[1]/div[${index}]/div[1]/div[1]

code:

import java.util.concurrent.ThreadLocalRandom

def index 

def x = ThreadLocalRandom.current().nextInt(1, 124)

WebUI.scrollToElement(findTestObject('Smoke Test Objects/Fleet/ConstructionProjectLevel/Clickable Machine Icon'), [(index) : x], 3)

WebUI.click(findTestObject('Smoke Test Objects/Fleet/ConstructionProjectLevel/Clickable Machine Icon', [(index) : x]))



index
image


FAILED.
Reason:
groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords.scrollToElement() is applicable for argument types: (com.kms.katalon.core.testobject.TestObject, java.util.LinkedHashMap, java.lang.Integer) values: [TestObject - 'Object Repository/Smoke Test Objects/Fleet/ConstructionProjectLevel/Clickable Machine Icon', ...]

wrong:

WebUI.scrollToElement(findTestObject('...'), [(index) : x], 3)

right:

WebUI.scrollToElement(findTestObject('...', [(index) : x]), 3)

Mind the ) character.

1 Like

The problem is solved.
There was wrong variable description

def index

def x = ThreadLocalRandom.current().nextInt(1, 124)

WebUI.scrollToElement(findTestObject('Smoke Test Objects/Fleet/ConstructionProjectLevel/the_index_machine', [('index') : x]), 
    3)

WebUI.verifyElementVisible(findTestObject('Smoke Test Objects/Fleet/ConstructionProjectLevel/the_index_machine', [('index') : x]))