Can i use xpath to locate element directly in scripts instead of objects

Hi,

I have located the elemnt using xpath, but it will be changing its position. so i think it can be resolved with directly inputting xpath in the scripts

I ve tried the below script
for (int p=1; p<10;p++)
{
WebElement element1 = WebUiCommonHelper.findWebElement(By.xpath("//div[@id=‘surveylist’]/div/div[p]/div/div//div[@class=‘survey-card-title-text activator truncate tooltipped’]"),30)
if (element1.getText() == “smoke test”)
{
WebUI.executeJavaScript(‘arguments[0].click()’, Arrays.asList(element1))

WebUI.delay(5)

WebUI.click(findTestObject(By.xpath("//div[@id='surveylist']/div/div[p]/div/div/div[3]/div[3]/div/div[12]/a/i")))
break
}

}

But on executing the , im getting error like

groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.webui.common.WebUiCommonHelper.findWebElement() is applicable for argument types: (org.openqa.selenium.By$ByXPath, java.lang.Integer) values: [By.xpath: //div[@id=‘surveylist’]/div/div[p]/div/div//div[@class=‘survey-card-title-text activator truncate tooltipped’], …]
Possible solutions: findWebElement(com.kms.katalon.core.testobject.TestObject, int), findWebElements(com.kms.katalon.core.testobject.TestObject, int)

Kindly help me out to solve this

This is what you can to do easily use XPath in findTestObject.

Create a new custom keyword called TestObjectHelper (or whatever)

public class TestObjectHelper {
	public static TestObject getTestObjectWithXpath(String xpath) {
		return new TestObject().addProperty('xpath', ConditionType.EQUALS, xpath)
	}
}

Usage:

WebUI.click(TestObjectHelper.getTestObjectWithXpath('//whatever'))

2 Likes

Thanks Marek. I did like you said

But again am facing the issue as below

groovy.lang.MissingMethodException: No signature of method: static com.kms.katalon.core.testobject.ObjectRepository.findTestObject() is applicable for argument types: (com.kms.katalon.core.testobject.TestObject) values: [TestObject - ‘’]
Possible solutions: findTestObject(java.lang.String), findTestObject(java.lang.String, java.util.Map)

Oh, sorry, my fault. Even findTestObject method is unnecessary.

WebUI.click(TestObjectHelper.getTestObjectWithXpath('//whatever'))

Thank you. It worked now.

this was exactly what I was looking for. I use this Testhelper class too.

But when call WebUI.click… I get this error. did I miss something? (just renamed the method in the class)

click(TestObjectHelper.findTestObjectWithXpath(strTestXpath))
2020-11-24 13:55:00.014 ERROR c.k.k.core.main.WSVerificationExecutor   - ? Verification FAILED.
Reason:
groovy.lang.MissingPropertyException: No such property: TestObjectHelper for class: WSVerification1606222480518
	at WSVerification1606222480518.run(WSVerification1606222480518:63)
	at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
	at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
	at com.kms.katalon.core.main.WSVerificationExecutor.runScript(WSVerificationExecutor.java:146)
	at com.kms.katalon.core.main.WSVerificationExecutor.doExecute(WSVerificationExecutor.java:140)
	at com.kms.katalon.core.main.WSVerificationExecutor.processExecutionPhase(WSVerificationExecutor.java:123)
	at com.kms.katalon.core.main.WSVerificationExecutor.accessMainPhase(WSVerificationExecutor.java:115)
	at com.kms.katalon.core.main.WSVerificationExecutor.execute(WSVerificationExecutor.java:103)
	at com.kms.katalon.core.main.TestCaseMain.runWSVerificationScript(TestCaseMain.java:127)
	at com.kms.katalon.core.main.TestCaseMain$runWSVerificationScript$0.call(Unknown Source)
	at TempTestCase1606222477921.run(TempTestCase1606222477921.groovy:25)