我想获取TestObject的文本,我使用WebUI.getText()。我的代码适用于我的某个页面,但无法用于其他页面。我无法弄清楚它失败的原因,一切都是一样的,它不应该失败。这就是我在做的事情:
@Keyword
public boolean verifyIPAddr(Socket socket){
//create test object for the ip header
TestObject ipHeader = new TestObject().addProperty("id", ConditionType.EQUALS, 'ipaddr-in-header')
WebUI.waitForElementPresent(ipHeader, 20, FailureHandling.OPTIONAL)
//get text (IP) from ipHeader
String ipHeaderStr = WebUI.getText(ipHeader)
KeywordUtil.logInfo("ipHeaderStr: " + ipHeaderStr.toString())
//split the ipHeaderStr so that "IP: " portion can be removed and only "0.0.0.0" portion is left
String[] ipHeaderStrArr = ipHeaderStr.split(' ')
//store the ip in a variable
String guiIPAddress = ipHeaderStrArr[1]
//get the socket side ip
String cassetteIP = socket.getInetAddress().getHostAddress()
KeywordUtil.logInfo(" address:" + cassetteIP)
//validate that both are the same
if(cassetteIP.equals(guiIPAddress)){
KeywordUtil.logger.logPassed(guiIPAddress + " IP from GUI matches: " + cassetteIP + " from socket")
return true;
}
else{
KeywordUtil.logger.logFailed(guiIPAddress + " IP from GUI does not match: " + cassetteIP + " IP from socket")
return false
}
}
] 2
我100%它与WebUI.getText()有关,但它让我感到困惑,因为它适用于一个页面,但对另一个页面失败。
以下是不起作用的页面的HTML:
更新:
我只是注意到失败的那个,有时会失败,有时会失败,我仍然想知道如何保证行为保持稳定。
您可以尝试以下几种方法:
WebUI.delay(30)
WebUI.waitForElementPresent(ipHeader, 60, FailureHandling.OPTIONAL)
原因是,Katalon(Selenium)代码通常依赖于其影响范围之外的元素,如加载时间,网络流量,计算机硬件,竞争竞争条件等。
因此,即使使用相同的代码,有时等待时间也会不同,这就是使用灵活等待(如waitForElement*()
方法)更好的原因。