There isn’t a way to directly convert from RemoteWebElements (also WebElements or MobileElements) to TestObjects, but we can do it based on some assumptions:
- If the RemoteWebElement was created using xpath
- If Selenium/Appium’s RemoteWebElement.toString() function returns the xpath in the string
First, create a function to get the xpath from the RemoteWebElement:
protected String getXPathFromElement(RemoteWebElement element) {
String elementDescription = element.toString();
return elementDescription.substring(elementDescription.lastIndexOf("-> xpath: ") + 10, elementDescription.lastIndexOf("]"));
}
Then use the xpath to create a new TestObject with an xpath attribute:
protected TestObject fromElement(RemoteWebElement element) {
TestObject testObject = new TestObject();
testObject.addProperty("xpath", ConditionType.EQUALS, getXPathFromElement(element));
return testObject;
}
As an alternative to writing all of that code in your tests, you can use an external library which includes a TestObjectConverter:
To read about how to include an external library into your Katalon project, see here.