4

I have WebElement which I have to convert into Testobject in katalon using groovy script.

For Example

List<WebElement> WEs = WebUI.executeJavaScript("return document.querySelector('#email').parentElement", [])

Now I want to convert WEs[0] to TestObject which Katalon accepts.

Please let me know if you have an idea about this.

Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
Rohit Ramani
  • 776
  • 7
  • 15

3 Answers3

5

There is no direct way to convert WebElements to TestObjects. According to this forum question, you could create a function to get the xpath of the web element

protected String getXPathFromElement(RemoteWebElement element) {
    String elementDescription = element.toString();
    return elementDescription.substring(elementDescription.lastIndexOf("-> xpath: ") + 10, elementDescription.lastIndexOf("]"));
}

and then create a new test object with the given xpath:

protected TestObject fromElement(RemoteWebElement element) {
    TestObject testObject = new TestObject();
    testObject.addProperty("xpath", ConditionType.EQUALS, getXPathFromElement(element));
    return testObject;
}


NOTE:

For other way around (Test Object -> WebElement), use

WebUiCommonHelper.findWebElement(test-object, timeout)
Mate Mrše
  • 7,997
  • 10
  • 40
  • 77
  • I think element.toString() does not return xpath each time, I have tried and didn't get xpath. I am using following code to get the web element `WebElement element = WebUI.executeJavaScript("return document.querySelector('.text-center img')", []) String elementDescription = element.toString(); println(elementDescription.substring(elementDescription.lastIndexOf("-> xpath: ") + 10, elementDescription.lastIndexOf("]")))` Please let me know if I am doing wrong. – Rohit Ramani Feb 06 '19 at 05:03
  • The `element` in your example is selected by a css locator. Do you need to use WebUI.executeJavaScript() in order to select the element? It is simpler using css or xpath. – Mate Mrše Feb 06 '19 at 07:43
  • Yes sometimes, I need to get elements using WebUI.executeJavaScript() and then I need to convert those elements to TestObject. – Rohit Ramani Feb 06 '19 at 08:12
2

WebUI.convertWebElementToTestObject()

  • Please provide more details to your answer. Show examples if at all possible. One line answers with no explanation are hard to understand. – cyn Jul 13 '20 at 17:36
1

To create Test Object from any WebElement, I have developed the function as below

public static String WebElementXPath(WebElement element) {
    if (element.tagName.toUpperCase() == 'HTML')    return '/html';
    if (element.tagName.toUpperCase() == 'BODY')      return '/html/body';


    // calculate position among siblings
    int position = 0;
    // Gets all siblings of that element.
    List<WebElement> siblings = WebUI.executeJavaScript("return arguments[0].parentNode.childNodes", [element])
    WebElement innerSibs
    //List<WebElement> siblings = element.parentNode.childNodes;

    WebElement sibling
    def type,response
    for(int i=0;i<siblings.size();i++){
        type = WebUI.executeJavaScript("return arguments[0].nodeType", [siblings[i]])
        if (type == null){
            continue;
        }
        if(type!=1){
            continue;
        }
        sibling = siblings[i];
        // Check Siblink with our element if match then recursively call for its parent element.
        if (sibling == element) {
            innerSibs = WebUI.executeJavaScript("return arguments[0].parentElement", Arrays.asList(sibling))
            if(innerSibs==null){
                return ""
            }
            response = functions.WebElementXPath(innerSibs)
            return response+'/'+element.tagName+'['+(position+1)+']';

        }

        // if it is a siblink & element-node then only increments position.
        type = WebUI.executeJavaScript("return arguments[0].nodeType", [sibling])
        if (type == 1 && sibling.tagName == element.tagName)            position++;

    }
}

And then I have created function to get test object as below as suggested by Mate Mrše

public static TestObject getTestObjectFromWebElement(WebElement element) {
    TestObject object = new TestObject()
    object.addProperty("xpath", ConditionType.CONTAINS, functions.WebElementXPath(element))
    return object
}

Note : "Framework" folder had been created by us as inside the Keyword folder and then We had created the "functions" keyword

enter image description here

I hope this might help other developers.

Rohit Ramani
  • 776
  • 7
  • 15