Printing the attribute value using println

I have an object and I would like to get the attribute xpath of the same using getattribute
I used the below code.But it shows the xpath is null for the object in console and prints nothing.
But the object has an xpath in object repository.Please help.The code used and error return in consolde is given below

Attribute ‘xpath’ of object ‘Object Repository/Page_Amazon.com Online Shopping for/input_field-keywords’ is: ‘null’

tagvalue = WebUI.getAttribute(findTestObject(‘Page_Amazon.com Online Shopping for/input_field-keywords’), ‘xpath’)

println(tagvalue)

/**please note tag value is added as variable in testcase

Hello. WebUI._getAttribute _returns HTML attribute of specified element (like id, class, style etc.).

If you want to get XPath of object in Object repository, use this code:

TestObject to = ObjectRepository.findTestObject("Misc/test1")List<TestObjectProperty> props = to.getProperties()for(prop in props) {	if(prop.getName() == 'xpath') {		println prop.getValue()	}}

If you use Selector method XPath, the code is easier:

TestObject to = ObjectRepository.findTestObject("Misc/test1")println to.getSelectorCollection().get(SelectorMethod.XPATH)

20180829-130727.jpg

20180829-131030.jpg

1 Like

option 1
my code is given below

TestObject to = ObjectRepository.findTestObject(‘Page_Amazon.com Online Shopping for/input_field-keywords’)

List props = to.getProperties()

for(prop in props) {

if(prop.getName() == ‘xpath’) {

println prop.getValue()

}

}

But failed with compilation error

Test Cases/New Test Case FAILED because (of) org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:

file:/C:/Users/875886/Katalon%20Studio/tryang1/Scripts/New%20Test%20Case/Script1535528887285.groovy: 34: unable to resolve class TestObjectProperty

@ line 34, column 6.

On importing the below package option 1 works fine:import com.kms.katalon.core.testobject.TestObjectProperty;

option 2
my code is given below

TestObject to = ObjectRepository.findTestObject(‘Page_Amazon.com Online Shopping for/input_field-keywords’)

println to.getSelectorCollection().get(SelectorMethod.xpath)

failed with run time error

Test Cases/New Test Case FAILED because (of) Variable ‘SelectorMethod’ is not defined for test case.

This is all you need:

import com.kms.katalon.core.testobject.ObjectRepositoryimport com.kms.katalon.core.testobject.SelectorMethodimport com.kms.katalon.core.testobject.TestObjectimport com.kms.katalon.core.testobject.TestObjectProperty

Or - press Ctrl-Shift-O and imports are added automatically.

1 Like

Thank u