XPATH with variables

Dears,
i have issue with dynamic xpath since in my application most componant i’ve called them like the below xpath quiery:
(.//*[text()=“User name”]/following::input[@type=“text”])[1]
it’s working well with me when testing the object but my issue with arabic version, how i can pass parameter to the xpath to use the arabic and english version without re-recording the whole script in arabic version

arabic xpath: (.//*[text()=“اسم المستخدم’”]/following::input[@type=“text”])[1]

thanks in advance

[text()="اسم المستخدم'"]

I just noted, here you have a single quotation character (’).

It seems to be a typo, isn’t it?

sorry it’s by mistake what I’ve looking for to have variable in the xpath to be able mapping the objects to the correct language

Why not have a look at this: Parameterize a Test Object
https://docs.katalon.com/display/KD/Parameterized+a+Test+Object

You will find an example of a Groovy-String as XPath with a ‘index’ variable.

"//*[@id="main"]/div/table/tbody/tr[2]/td[${index}]/input"

And you can give value to the index variable with 2nd Map argument to findTestObject method call.

WebUI.check(findTestObject('chk_Title', [('index') : it]))

See https://api-docs.katalon.com/studio/v4.7.0/api/com/kms/katalon/core/testobject/ObjectRepository.html#findTestObject(java.lang.String,%20Map<Object,%20Object>) for detail

Dear @@4280-kazurayam
i have tried the provided solution without success if you tied it please provide me with your stepes since what i want to have that index variable to declare it in my code if it’s 1 refer to English and 2 refer to Arabic that’s it.

I have made a running example. Please have a look at:

Let me brief it:
My Application-Under-Test for demo gives you a target HTML like this:

<div id="english"><label>User name</label><input type="text" value="foo"></div>
<div id="arabic"><label>اسم المستخدم</label><input type="text" value="bar"></div>

My Test Object looks like this:

This has a xpath selector like this:

//label[text()='${index}']/following-sibling::input

And my Test Case has following lines of code:

// <div id="arabic"><label>اسم المستخدم</label><input type="text" value="bar"></div>
//                                                                         ^^^
def input_ar = WebUI.getAttribute(
    findTestObject('Page_/input_next_to_label_parameterized', ['index':'اسم المستخدم']),
    'value')
WebUI.verifyEqual(input_ar, 'bar')

I found that the technique described at https://docs.katalon.com/display/KD/Parameterized+a+Test+Object does works, but a bit difficult to understand.

MyTestObject_update.png

Hi , having similar problem:
I tried parameterizing the object’s xpath (selectIndustryName) as follows for above AUT:

//*[@${index}"]/span/div/div

then I wrote below code:
WebUI.click(findTestObject(‘Page_AddClient/selectIndustryName’, [(‘index’) : 1]))

but it failed with error:
Test Cases/AddClientFlow1 FAILED because (of) Unable to click on object ‘Object Repository/Page_AddClient/selectIndustryName’ (Root cause: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/Page_AddClient/selectIndustryName’ located by 'By.xpath: //*[@1"]/span/div/div’ not found)

Can you help?

correction: xpath is //*[@id=“ui-select-choices-row-24-${index}”]/span/div/div

tamanna said:

correction: xpath is //*[@id=“ui-select-choices-row-24-${index}”]/span/div/div

i meant this is the correct path:

xpath.jpg

there is another problem, 24 in xpath changes on each run, can i use regular expression in xpath like ‘'to substitute 24 like below: ( i dont know the correct syntax, just a raw code]
//
[@id="ui-select-choices-row-[’*’]-${index}"]/span/div/div

tamanna said:

there is another problem, 24 in xpath changes on each run, can i use regular expression in xpath like ‘'to substitute 24 like below: ( i dont know the correct syntax, just a raw code]
//
[@id="ui-select-choices-row-[’*’]-${index}"]/span/div/div


Raw code for using regular expression in xpath. Can someone please suggest?

xpathRegEx.jpg

XPath does not include Regex. You can not mix them arbitrarily. However XPath itself is expressive enough so that you can abstract ‘24’ portion.

Could you please answer to my comment to http://forum.katalon.com/discussion/comment/14592#Comment_14592

Can you please help me with building the xpath with abstracting 18 and replacing 1 with a
variable?

Screenshot_1.jpg

I have answered to your question at

Hi,

I tried so many ways. But a xpath with variable doesn’t seem to work. Is there a typo in the format?:

//input[@value=“clickandcollect-${currency}”]

It’s a radio button:

image

@Pascal_Donauer

Your Test Object seems to be OK.

But not enough. Your Test Case script must have a line which interpolates the value CHF into the place holder ${currency} in the selector. That line would look something like this:

TestObject tObj = findTestObject("test object name", [ "currency": "CHF"])

Is your test case script OK?

Well, thank you for your last reply.

I solved it another way: the xpath should only contain the part before the currency:
//input[contains(@id,“shippingMethod-clickandcollect-”)]