XPath with randomized variables


#1

Hello,
I’ve read various explanations of how variables are passed into xpath, but I’m still not getting it.

Object’s Xpath:
//*[@id='hello']/div/table/tbody[${index}]/tr/td[4]/span/a

My Script:
def index;
def x = ThreadLocalRandom.current().nextInt(1, 10 + 1)
WebUI.click(findTestObject('PageObject/a_Open', [(index) : x]))

parameter-xpath-a1

What am I doing wrong here? Simply trying to randomize the ‘tbody’ so that a random link is clicked in td[4].

Thanks,


#2

You’re close! Try:

WebUI.click(findTestObject(‘PageObject/a_Open’, [‘index’ : x]))

(i.e. put ‘index’ in single quotes)


#3

Doesn’t seem to have worked.
I’m sure there’s something else I’m doing wrong.

Here’s the details:-

Test object:
a_Open

Selection Method:
XPath

Selector Editor:
//*[@id='hello']/div/table/tbody[${index}]/tr/td[4]/span/a

Test Script (excerpt):
def x = ThreadLocalRandom.current().nextInt(1, 10 + 1)
WebUI.click(findTestObject('PageObject/a_Open', ['index' : x]))

Test Object Variables:
parameter-xpath-a2

Is there possibly something else that I could be doing wrong?
If I replace XPath’s ‘[$index]’ with [1] or [6] or other integers, then it works fine. But the point is that I want to randomize ‘tbody’: not hard code values.

Thanks,


#4

try to use selection mode attributes with xpath = ‘//*[@id=‘hello’]/div/table/tbody[${index}]/tr/td[4]/span/a’

i don’t know if it’s still true, but xpath selection method cannot be parameterized. (at least it was true few version back, i stopped to use selection method other then attributes back then)


#5

@Andrej_Podhajsky @Brandon_Hein
The combination of both your answers has helped me resolve this.

Katalon has such an active community.
Thank you!