如何在katalon studio中将WebElement转换为TestObject?

时间:2019-02-03 15:30:11

标签: java selenium-webdriver groovy automated-tests katalon-studio

我有WebElement,必须使用groovy脚本将其转换为katalon中的Testobject。

例如

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

现在我想将WEs [0]转换为Katalon接受的TestObject。

如果您对此有想法,请告诉我。

谢谢

3 个答案:

答案 0 :(得分:2)

没有将WebElements转换为TestObjects的直接方法。根据{{​​3}},您可以创建一个函数来获取Web元素的xpath

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

,然后使用给定的xpath创建一个新的测试对象:

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


注意:

对于其他方法(测试对象-> WebElement),请使用

WebUiCommonHelper.findWebElement(test-object, timeout)

答案 1 :(得分:0)

要从任何WebElement创建测试对象,我已经开发了如下功能

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++;

    }
}

然后我按照MateMrše的建议创建了如下函数来获取测试对象

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

注意:“框架”文件夹是我们在“关键字”文件夹中创建的,然后我们创建了“功能”关键字

enter image description here

我希望这会对其他开发人员有所帮助。

答案 2 :(得分:0)

WebUI.convertWebElementToTestObject()

相关问题