如何WebElement转换的TestObject在katalon工作室?

问题描述 投票:2回答:2

我有WebElement,我有使用Groovy脚本转换成的TestObject在katalon。

例如

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

现在,我想转换WES [0]到的TestObject这Katalon接受。

请让我知道,如果你有这个想法。

谢谢

java selenium-webdriver groovy automated-tests katalon-studio
2个回答
3
投票

还有就是要WebElements转换为TestObjects没有直接的方法。据this forum question,您可以创建一个函数来获取网络元素的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
投票

从任何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++;

    }
}

然后,我创建功能得到如下测试对象由伴侣MRSE的建议

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

注:“框架”文件夹中已经建立由我们作为关键字的文件夹内,然后我们创造了“功能”关键字

enter image description here

我希望这可以帮助其他开发人员。

© www.soinside.com 2019 - 2024. All rights reserved.