addProperty('xpath' ...) with String as xpath?

I tried to parameterize my testobjects like it’s described here:
https://docs.katalon.com/display/KD/Manage+Test+Object#ManageTestObject-ParameterizingTestObject

It works fine for to get //*[@class = attribute] with the code below

TestObject to = findTestObject('bm/color/colorcode')
to.addProperty('class', ConditionType.EQUALS, attribute, true)

However it does not work with xpath for me so far…
The xpath I want to get: //*[@class = ‘P0751’]//h4[contains(text(),‘Alpine white.’)] by this:

TestObject to = findTestObject('bm/color/colorname')
String prop = "//*[@class = '"+detail+"']//h4[contains(text(),'"+attribute+"')]"
to.addProperty('xpath', ConditionType.EQUALS, prop, true)

The xpath is correct! If I print the String prop and use it as the xpath of colorname manually, it works perfectly.

My only guess is that the String is somehow not accepted as a xpath value because handing it over manually into the testobject works, but using the addProperty() method ends in this:

Finding web element with id: ‘Object Repository/bm/color/colorname’ located by 'By.xpath: ’ in ‘30’ second(s)

Where there is no xpath shown, like it didn’t accept it and that for kept it empty?

I appreciate any help! :slight_smile:

1 Like

It also does not work if I take out the variables of the prop String like this:
//*[@class = ‘P0751’]//h4[contains(text(),‘Alpine white.’)]
Even tho this is the exact same xpath that works when pasted in the testobject manually…

hi lukas,
define object like this:

xpath - //*[@class = '${PARENT_O}']//h4[contains(text(),'${TEXT_TO_FIND}')]

then in call to check if exist, use parametrised call:

WebUI.verifyElementPresent(findElement('OR/path/to/object',[('PARENT_O'):'PO751',('TEXT_TO_FIND'):'Alpine white.']),5)

i wrote it from memory, so errors may occure

Hi Andrej,
The object is now defined with xpath -

//*[@class = '${detail}']//h4[contains(text(),'${attribute}')]

and called by

String name = WebUI.getText(findTestObject('bm/color/colorname', [('detail'):'P0751',('attribute'):'Alpine white.']))

I don’t see an error here so far, but it seems to not use (‘detail’):'P0751 as a parameter for the xpath of the object, this is the log:

Finding web element with id: 'Object Repository/bm/color/colorname' located by 'By.xpath: //*[@class = '${detail}']//h4[contains(text(),'${attribute}')]' in '30' second(s)

are you using attribute section of object in OR?

image.png

Yes, I also just checked if caps or no caps changes things, it didnt help

kat2.PNG

Okay so… using

//*[@class = '${DETAIL2}']//h4[contains(text(),'${ATTRIBUTE}')]

gives

located by 'By.xpath: //*[@class = '${DETAIL2}']//h4[contains(text(),'${ATTRIBUTE}')]' in '30' second(s)

But using additional brackets like this [] at least makes it recognize the parameter, but for any reason it keeps them and puts them to the parameter itself? So this:

//*[@class = '[${DETAIL2}]']//h4[contains(text(),'[${ATTRIBUTE}]')]

results in:

located by 'By.xpath: //*[@class = '[P0751]']//h4[contains(text(),'[Alpine white.]')]' in '30' second(s)

So he uses [${DETAIL2}] as a parameter but keeps the brackets [] …

Also weird:

//*[@class = [${DETAIL2}]]//h4[contains(text(),'${ATTRIBUTE}')]

results in:

located by 'By.xpath: //*[@class = [P0751]]//h4[contains(text(),'Alpine white.')]' in '30' second(s)

But as soon as I change the first parameter to be like the second it doesnt work again?!
(Parameter 2, Alpine white. is how it should be)

so i did small exercise :
script

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpointimport static com.kms.katalon.core.testcase.TestCaseFactory.findTestCaseimport static com.kms.katalon.core.testdata.TestDataFactory.findTestDataimport static com.kms.katalon.core.testobject.ObjectRepository.findTestObjectimport com.kms.katalon.core.checkpoint.Checkpoint as Checkpointimport com.kms.katalon.core.checkpoint.CheckpointFactory as CheckpointFactoryimport com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as MobileBuiltInKeywordsimport com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobileimport com.kms.katalon.core.model.FailureHandling as FailureHandlingimport com.kms.katalon.core.testcase.TestCase as TestCaseimport com.kms.katalon.core.testcase.TestCaseFactory as TestCaseFactoryimport com.kms.katalon.core.testdata.TestData as TestDataimport com.kms.katalon.core.testdata.TestDataFactory as TestDataFactoryimport com.kms.katalon.core.testobject.ObjectRepository as ObjectRepositoryimport com.kms.katalon.core.testobject.TestObject as TestObjectimport com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSBuiltInKeywordsimport com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSimport com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUiBuiltInKeywordsimport com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUIimport internal.GlobalVariable as GlobalVariableWebUI.openBrowser('file://C:/Users/me/src/html/hello.html')WebUI.verifyElementPresent(findTestObject('Object Repository/__Sandbox/HelloFile/paramText',[('textP1'):'myText',('textP2'):'another text.']), 5)

html:

<html>  <head>  </head>  <body>    <div>Hello World!</div>  	<input id="0001" type="text" value="Ello"/>		<table>			<tr>				<td>					<div _ngcontent5>						<!---->						<div _ngcontent5>							<p _ngcontent5>Some text2</p>						</div>						<!---->					</div>				</td>			</tr>		</table>		<select id="0002" size="5">			<option value="0000">0000</option>			<option value="0001">0001</option>			<option value="0001">0001</option>			<option value="0002">0002</option>			<option value="0003">0003</option>			<option value="0004">0004</option>			<option value="0005">0005</option>			<option value="0006">0006</option>			<option value="0007">0007</option>			<option value="0008">0008</option>			<option value="0009">0009</option>			<option value="0010">0010</option>			<option value="0011">0011</option>			<option value="0011">0011</option>			<option value="0012">0012</option>			<option value="0013">0013</option>			<option value="0014">0014</option>			<option value="0015">0015</option>			<option value="0016">0016</option>			<option value="0017">0017</option>			<option value="0018">0018</option>			<option value="0019">0019</option>		</select>		<table>			<tr>				<td></td>			</tr>		</table>		<p>myText</p>		<div></div>		<h4>another text.</h4>  </body></html>

test object:

for some reason substitution is not working for you, but i cannot see why
and my logs:

09-22-2018 12:08:12 PM - [INFO]   - Navigating browser to: 'file://C:/Users/s2ppun/src/html/hello.html'09-22-2018 12:08:12 PM - [PASSED] - Browser is opened with url: 'file://C:/Users/s2ppun/src/html/hello.html'09-22-2018 12:08:12 PM - [END]    - End action : openBrowser09-22-2018 12:08:12 PM - [START]  - Start action : verifyElementPresent09-22-2018 12:08:12 PM - [INFO]   - Finding Test Object with id 'Object Repository/__Sandbox/HelloFile/paramText'09-22-2018 12:08:12 PM - [INFO]   - Checking object09-22-2018 12:08:12 PM - [INFO]   - Checking timeout09-22-2018 12:08:12 PM - [INFO]   - Finding web element with id: 'Object Repository/__Sandbox/HelloFile/paramText' located by 'By.xpath: //p[text()='myText']/..//h4[contains(text(),'another text.')]' in '5' second(s)09-22-2018 12:08:12 PM - [INFO]   - Found 1 web elements with id: 'Object Repository/__Sandbox/HelloFile/paramText' located by 'By.xpath: //p[text()='myText']/..//h4[contains(text(),'another text.')]' in '5' second(s)09-22-2018 12:08:12 PM - [PASSED] - Object 'Object Repository/__Sandbox/HelloFile/paramText' is present09-22-2018 12:08:12 PM - [END]    - End action : verifyElementPresent09-22-2018 12:08:12 PM - [PASSED] - Test Cases/__Sandbox/New Test Case TextParam Search09-22-2018 12:08:12 PM - [END]    - End Test Case : Test Cases/__Sandbox/New Test Case TextParam Search

2018-09-22_12-10-38.png

1 Like

also when i changed html to:

<p class="myClass0001">myText</p><div></div><h4>another text.</h4>

still is OK: (log)

09-22-2018 12:23:07 PM - [END]    - End action : openBrowser09-22-2018 12:23:07 PM - [START]  - Start action : verifyElementPresent09-22-2018 12:23:07 PM - [INFO]   - Finding Test Object with id 'Object Repository/__Sandbox/HelloFile/paramText'09-22-2018 12:23:07 PM - [INFO]   - Checking object09-22-2018 12:23:07 PM - [INFO]   - Checking timeout09-22-2018 12:23:07 PM - [INFO]   - Finding web element with id: 'Object Repository/__Sandbox/HelloFile/paramText' located by 'By.xpath: //p[@class='myClass0001']/..//h4[contains(text(),'another text.')]' in '5' second(s)09-22-2018 12:23:07 PM - [INFO]   - Found 1 web elements with id: 'Object Repository/__Sandbox/HelloFile/paramText' located by 'By.xpath: //p[@class='myClass0001']/..//h4[contains(text(),'another text.')]' in '5' second(s)09-22-2018 12:23:07 PM - [PASSED] - Object 'Object Repository/__Sandbox/HelloFile/paramText' is present09-22-2018 12:23:07 PM - [END]    - End action : verifyElementPresent09-22-2018 12:23:07 PM - [PASSED] - Test Cases/__Sandbox/New Test Case TextParam Search09-22-2018 12:23:07 PM - [END]    - End Test Case : Test Cases/__Sandbox/New Test Case TextParam Search

1 Like

btw. we should try to move this from tips&tricks

Thank you a lot for all the effort in trying to help!

Since there seems to be no real reason for it to be failing, just freshly reinstalled katalon and tried it again. But the exact same issue still occurs.

And this makes me really clueless…

lukas krombholz said:
//[@class = [${DETAIL2}]]//h4[contains(text(),‘${ATTRIBUTE}’)]
results in:
located by 'By.xpath: //
[@class = [P0751]]//h4[contains(text(),‘Alpine white.’)]’ in ‘30’ second(s)
But as soon as I change the first parameter to be like the second it doesnt work again?!
(Parameter 2, Alpine white. is how it should be)

btw. we should try to move this from tips&tricks

Yes sure, how do I move it?

try to execute my example … you should have everything to be able to run it…
regarding move, try to address forum admin

I copied your example and it works.
So Katalon itself isnt the problem, but I still have no idea why my case acts this weirdly

Any idea why also both below methods dont work?

TestObject to = findTestObject('bm/color/colorname')
to.addProperty('xpath', ConditionType.EQUALS, "testxpath", true)

or

to = WebUI.modifyObjectProperty(findTestObject('bm/color/colorname'), 'xpath', 'equals', "testxpath", true)

Both result in an empty xpath:

Finding web element with id: 'Object Repository/bm/color/colorname' located by 'By.xpath: ' in '30' second(s)

And it did work when I edited the ‘class’ property in another keyword, but xpath is a problem

in first is typo bmw_m
i’m not sure if that path should not be included ‘Object Repository’ - not sure about that

and regarding previous, i just wanted to ask if there are no leftovers for definitions of to ‘bm/color/colorname’

That was a typo in here, it’s fine in the project

There should be no leftovers if i understand you correctly

This is everything that is used related to color: ( // infront of old solution attempts)

color part in testcase:

case 'color':
WebUI.println(dat)
String action = findTestData("Testdat"+testcase+'bm').getValue("action", dat)
String detail = findTestData("Testdat"+testcase+'bm').getValue("detail", dat)
String detail2 = findTestData("Testdat"+testcase+'bm').getValue("detail2", dat)
String attribute = findTestData("Testdat"+testcase+'bm').getValue("attribute", dat)
if(action == 'select'){
CustomKeywords.'bm.colorselect.cs'(attribute, LoadTimeCorrector, step, run, device, timeStamp, product); step ++
}
if(action == 'check'){
CustomKeywords.'bm.colorcheck.cc'(detail, detail2, attribute, LoadTimeCorrector, step, run, device, timeStamp, product); step ++
}
WebUI.println(topic + action + attribute)
break

Keyword related to colorcheck:

if(detail == 'name'){
//TestObject to = findTestObject('bm/color/colorname')
//String prop = "//*[@class = '"+detail2+"']//h4[contains(text(),'"+attribute+"')]"
//to.addProperty('xpath', ConditionType.EQUALS, prop, true)
//String x = "'success'"

String name = WebUI.getText(findTestObject('bm/color/colorname', [('DETAIL2'):'P0751',('ATTRIBUTE'):'Alpine white.']))
WebUI.println(name)
WebUI.verifyElementVisible(findTestObject('bm/color/colorname'))

and the object colorname:

I seperated the color part from the rest of the test to be sure nothing is messing with it.
and for the xpath i tried different solutions so far

kat3.PNG

but this from keyword:

WebUI.verifyElementVisible(findTestObject('bm/color/colorname'))

must be failing here are no params…

Oh god… you are a savior, this indeed was the failure

Since there never was an output from this line (WebUI.println(name_)_) in the logs, I didnt even think of the following line to cause the error…
I used the println command on purpose there to see at which part the error occurs, but seems like something messed up there. I dont know why it was like this but that doesnt matter right now :slight_smile: I can at least make it work now and know what to look out for

Huge thanks for the help and your time!

you are welcome