WebUI.getText()返回空String

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

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

stack-traces[![][1]] 2

我100%它与WebUI.getText()有关,但它让我感到困惑,因为它适用于一个页面,但对另一个页面失败。

以下是工作页面的HTML:HTML

以下是不起作用的页面的HTML:

HTML2

更新:

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

katalon-studio
1个回答
1
投票

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

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

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

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

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