猿问

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

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

例如

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

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

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


月关宝盒
浏览 124回答 3
3回答

慕码人2483693

没有将 WebElements 转换为 TestObjects 的直接方法。根据这个论坛问题,您可以创建一个函数来获取 web 元素的 xpathprotected String getXPathFromElement(RemoteWebElement element) {&nbsp; &nbsp; String elementDescription = element.toString();&nbsp; &nbsp; return elementDescription.substring(elementDescription.lastIndexOf("-> xpath: ") + 10, elementDescription.lastIndexOf("]"));}然后使用给定的 xpath 创建一个新的测试对象:protected TestObject fromElement(RemoteWebElement element) {&nbsp; &nbsp; TestObject testObject = new TestObject();&nbsp; &nbsp; testObject.addProperty("xpath", ConditionType.EQUALS, getXPathFromElement(element));&nbsp; &nbsp; return testObject;}笔记:对于其他方式(测试对象 - > WebElement),使用WebUiCommonHelper.findWebElement(test-object, timeout)

叮当猫咪

为了从任何 WebElement 创建测试对象,我开发了如下功能public static String WebElementXPath(WebElement element) {&nbsp; &nbsp; if (element.tagName.toUpperCase() == 'HTML')&nbsp; &nbsp; return '/html';&nbsp; &nbsp; if (element.tagName.toUpperCase() == 'BODY')&nbsp; &nbsp; &nbsp; return '/html/body';&nbsp; &nbsp; // calculate position among siblings&nbsp; &nbsp; int position = 0;&nbsp; &nbsp; // Gets all siblings of that element.&nbsp; &nbsp; List<WebElement> siblings = WebUI.executeJavaScript("return arguments[0].parentNode.childNodes", [element])&nbsp; &nbsp; WebElement innerSibs&nbsp; &nbsp; //List<WebElement> siblings = element.parentNode.childNodes;&nbsp; &nbsp; WebElement sibling&nbsp; &nbsp; def type,response&nbsp; &nbsp; for(int i=0;i<siblings.size();i++){&nbsp; &nbsp; &nbsp; &nbsp; type = WebUI.executeJavaScript("return arguments[0].nodeType", [siblings[i]])&nbsp; &nbsp; &nbsp; &nbsp; if (type == null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(type!=1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; sibling = siblings[i];&nbsp; &nbsp; &nbsp; &nbsp; // Check Siblink with our element if match then recursively call for its parent element.&nbsp; &nbsp; &nbsp; &nbsp; if (sibling == element) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; innerSibs = WebUI.executeJavaScript("return arguments[0].parentElement", Arrays.asList(sibling))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(innerSibs==null){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ""&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response = functions.WebElementXPath(innerSibs)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return response+'/'+element.tagName+'['+(position+1)+']';&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // if it is a siblink & element-node then only increments position.&nbsp; &nbsp; &nbsp; &nbsp; type = WebUI.executeJavaScript("return arguments[0].nodeType", [sibling])&nbsp; &nbsp; &nbsp; &nbsp; if (type == 1 && sibling.tagName == element.tagName)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; position++;&nbsp; &nbsp; }}然后我按照 Mate Mrše 的建议创建了如下函数来获取测试对象public static TestObject getTestObjectFromWebElement(WebElement element) {&nbsp; &nbsp; TestObject object = new TestObject()&nbsp; &nbsp; object.addProperty("xpath", ConditionType.CONTAINS, functions.WebElementXPath(element))&nbsp; &nbsp; return object}注意:我们在关键字文件夹中创建了“框架”文件夹,然后我们创建了“函数”关键字

开满天机

WebUI.convertWebElementToTestObject()
随时随地看视频慕课网APP

相关分类

Java
我要回答