WebUI.getText() returns 空字符串

WebUI.getText() returns empty String

我想获取我的 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:

以下是无效页面的HTML:

更新:

我刚刚注意到失败的那个,有时会失败,有时会通过,我仍然想知道如何保证行为保持稳定。

您可以尝试以下几种方法:

  1. 您可以在获取元素文本之前添加延迟:
WebUI.delay(30)
  1. 您可以延长等待时间
WebUI.waitForElementPresent(ipHeader, 60, FailureHandling.OPTIONAL)

原因是,Katalon (Selenium) 代码通常取决于其影响范围之外的元素,例如加载时间、网络流量、计算机硬件、竞争竞争条件等。

因此,即使使用相同的代码,有时等待时间也会不同,这就是为什么最好使用灵活等待(如 waitForElement*() 方法)。