Stuggling with passing variable to object's xpath

Hello,
Just starting to learn Katalon Studio: I feel it is a great tool and I am getting really enthousiastic!

However, I’m struggling with dynamic xpath expressions. I want to adress a row in a table using a variable.

I have an object in the object repository with the following xpath:
_ id(“booked_form”)/table[@class=“login”]/tbody[1]/tr[2]/td[1]/table[1]/tbody[1]/tr[5]/td[1]
_and like to replace the 5 with a variable name.
Any hints how do I do that?

Kind regards,

Gerlof

Capture.PNG

@“Gerlof Hoekstra”: You might use manage test object by parameterizing its property.
This guidance introduces to use it: https://docs.katalon.com/display/KD/Manage+Test+Object#ManageTestObject-ParameterizingTestObject

Thanks for your suggestion. I did read that guide but cannot get it to work. Maybe I do not understand. In the test case, I created parameter RowNr:

Then I think I have to link this someway to the object in the object repository, but I think here I do not understand it quite well, now I have a failure during playback. What am I doing wrong?

Capture1.PNG

Capture2.PNG

Can you tell me what error message ? I suppose you should use either Integer or Number for that variable instead of String.

This is the error message:

Test Cases/CalculateTotalBooked FAILED because (of) Unable to click on object ‘Object Repository/Page_AdactIn.com - Select Hotel (1)/td’ (Root cause: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/Page_AdactIn.com - Select Hotel (1)/td’ located by ‘By.xpath: //td[count(. | id(“booked_form”)/table[@class=“login”]/tbody[1]/tr[2]/td[1]/table[1]/tbody[1]/tr[RowNr]/td[1]) = count(id(“booked_form”)/table[@class=“login”]/tbody[1]/tr[2]/td[1]/table[1]/tbody[1]/tr[RowNr]/td[1])][count(. | //[@RowNr = ‘5’]) = count(//[@RowNr = ‘5’])]’ not found)

I tried to use Number, but what do I have to fill in for Param in below table?
Still confused about how this is supposed to work.

Capture4.PNG

I’ve been able to use a variable in the xpath with the following method.

I create an object with a path of:
//table[@id=‘sales_dash_table’]/tbody/tr[${Variable}]/td

Then, within the code itself, I use the following:
seasonalityPlanValue= WebUI.getText(findTestObject(‘Page_/table_object_name’, [(‘Variable’) : index]))

The index is part of a loop, so it will be 1, 2, 3, etc as it iterates through.

2 Likes

YES, Peter, you made my day!
tr[${RowNr}] in the object’s xpath. does the trick.

Thank you!
Gerlof

2 Likes

That also helped me, thank you. Turned out I was just missing the $ in my variable name in the object :slight_smile:

Hi , I am also trying to pass a variable in xpath but no success , The xpath is //*[normalize-space(text())=${Qty}]

It works if i replace the variable Qty with value //*[normalize-space(text())=6]

Please advice how can i pass the variable

1 Like

I am having the same issue as Jaspreet, please can someone advise?

Does your test case script pass a parameter value appropriately?

TestObject tObj = findTestObject(String, ['Qty':6])

See

@kazurayam ,No my test case doesn’t pass that parameter. what is the importance of that parameter (The one you mentioned in your script)

I am able to pass the global variable in the xpath as below
//*[contains(text(),’${GlobalVariable.Specification}’)]

But I donot need to use the global variable instead I want to use the local variable from the test.

Invoking a method findTestObject is important. The method implements modifying a given String //*[normalize-space(text())=${Qty}] to a valid XPath expression //*[normalize-space(text())=6].

Without calling the method findTestObject, a placeholder ${Qty} will never be interpolated to 6.

@kazurayam, Thanks . I did went through the https://docs.katalon.com/katalon-studio/docs/parameterize-web-objects.html#example to understand it better .

Challenge that I am facing now is that If I use the global variable it working but I try to use a local variable it fails.

The Xpath is
///text()[normalize-space(.)=‘${Specifications}’]/parent::

In script view I am trying to get the text from this object as below :-

WebUI.getText(findTestObject(‘Products/Edit Specification/Added Specification Name - Copy’,
[(‘Spec’) : Specifications]))

The screenshot from manual view is as below:-

Specifications is a local variable declared as below in the test script
mydate = new Date()
formattedDate = mydate.format(‘MM/dd/yyyy HH mm ss’)
Specifications = “$UpdatedSpecification$formattedDate”

My script fails saying
Unable to find the element located by ‘By.xpath: ///text()[normalize-space(.)=‘${Specifications}’]/parent::’. Please recheck the objects properties to make sure the desired element is located.
2020-08-07 11:31:40.472 INFO c.k.k.c.webui.common.WebUiCommonHelper - [SMART_XPATH] Auto-trying out other XPath values in the test object.
2020-08-07 11:31:40.472 INFO c.k.k.c.webui.common.WebUiCommonHelper - [SMART_XPATH] No working XPath is found.

Please advice how can I use the local variables to pass the values to test objects.

// */text()[normalize-space(.)=’${Specifications}’]/parent::*

This string does not make any sence; not a valid XPath expression at all. No doubt you will get errors.

It is difficult for me to guess what you intended to express by this. Perhaps you need to study the XPath from scratch: See the following article.

If your script has a line

WebUI.getText(findTestObject(‘...’, [‘Spec’ : '...']))

where you have a parameter named Spec, then the locator in the TestObject should be like this:

//*[normalize-space(.)='${Spec}']

You should write the name of the parameter enclosed by ${ .... }.

@kazurayam thanks, I will take a look at this.
Actually I am stuck at Uploadfile functionality that need to be automated.

@kazurayam
I am trying to solve a problem of clicking sort arrow in multiple column headers.
Able to click in each column header by hardcoding variable value in script.
How to pick the column name or variable value from the method parameter in the script to click all the column headers based on columnName passed in method parameter?

Tried:
def sortAscendingOrDescending(String columnName){

	String expected = WebUI.getAttribute(findTestObject('CustomznObjects/BranchesListPage/ActivityHistoryTab/oCustomizationAH_ActivityHistoryTable_SortArrowAscend',['text':'Activity']),
	'class')

	WebUI.comment("Expected is" +expected)

}

Error message:
Caused by: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: ‘Object Repository/CustomznObjects/BranchesListPage/ActivityHistoryTab/oCustomizationAH_ActivityHistoryTable_SortArrowAscend’ located by ‘//th[contains(@id, ‘activitiesTable’) and contains(@title, ‘+columnName’)]/div[@class=“oj-table-column-header”]/div[@class=“oj-table-column-header-asc”]/a[contains(@class, ‘oj-table-column-header-asc’)]’ not found

Got this error message when tried to pass the local variable value as findTestObject(‘CustomznObjects/BranchesListPage/ActivityHistoryTab/oCustomizationAH_ActivityHistoryTable_SortArrowAscend’,[‘text’:’+columnName’]

Locator used:
//th[contains(@id, ‘activitiesTable’) and contains(@title, ‘${text}’)]/div[@class=“oj-table-column-header”]/div[@class=“oj-table-column-header-asc”]/a[contains(@class, ‘oj-table-column-header-asc’)]

Please help.

@arthi.vigneshwari Maybe you are trying too soon. Have you tried to insert a wait statement, such as
WebUI.waitForElementVisible(findTestObject(‘CustomznObjects/BranchesListPage/ActivityHistoryTab/oCustomizationAH_ActivityHistoryTable_SortArrowAscend’,[‘text’:‘Activity’])

before trying the getAttribute?

@grylion54 Thanks for your reply. Exception is not due to wait issue.It is due to xpath not found. Am not sure how to pass the method parameter in findTestObject parameter value.Please help with it.