How can I convert a Web Element into test Object?

I have to right click on a particular cell on a HTML table. I have gathered a list of Web Elements (of “td” tag) and can fetch the data and left click on the object. But when trying to use WebUI.rightClick(<>) it says we need to pass a Test Object and not a Web Element.

Kindly let me know how to right click on that particular cell.

Thanks.

Figured out a way.

So what is the way??

2 Likes

You can do something like this

public TestObject createTestObject(String locator){

    TestObject updatedTestObject = new TestObject("Grid")  

    updatedTestObject.addProperty("xpath", ConditionType.EQUALS, locator)  

    return updatedTestObject  

}
1 Like

Thank you very much!

There must be some questions missing, because I don’t see “the way”! :’(

2 Likes

Mate Mrse said:

There must be some questions missing, because I don’t see “the way”! :’(

Uh, yeah… what’s this thread about? The question is clear, the Best answer is clear, yet they don’t seem to be connected ???

Signed, confused.

To the OP, both Click and Right Click take a TestObject… so what’s the problem? If you ended up with a Web Element… explain how, please.

1 Like

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:

https://github.com/detroit-labs/katalon-mobile-util#converting-from-selenium-webelements-to-katalon-testobjects

To read about how to include an external library into your Katalon project, see here.

5 Likes

Thanks Chris. This was extremely helpful.I spent alot of time on this.There is also a lesson to be learned here. When in doubt, use the toString() method to see what text comes out. Once the WebElement is converted to a Sting it’s simple stuff.

1 Like

Katalon also now includes WebUI.convertWebElementToTestObject() function since 6.2 update. I assume a lot of this discussion has helped with implementing that. I haven’t utilized it much yet to see how it works.

6 Likes