用于等待元素可用于 Apache Cordova Webview 驱动的应用程序的 Java Wrapper 方法 [英] Java Wrapper method for waiting for element to be available for Apache Cordova Webview driven App

查看:12
本文介绍了用于等待元素可用于 Apache Cordova Webview 驱动的应用程序的 Java Wrapper 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于基于多个 Webview 的移动应用程序(使用 Cordova、PhoneGap、XCode 构建的 iOS 应用程序),我创建了以下方法来检查元素是否存在.请建议以下代码段是否有意义?因为基于传统显式等待的传统包装函数不能可靠地工作.

For Multiple Webview based Mobile App (iOS app built using Cordova, PhoneGap, XCode), I have created below method to check if element is present. Kindly suggest if below snippet makes sense? as traditional wrapper functions based on traditional Explicit waits are not working reliably.

    public boolean waitForElemToBeAvailable(final By by, final int timeout, int retries) {
    WebDriverWait wait = new WebDriverWait(appiumDriver, timeout);
    boolean success = false;
    final long waitSlice = timeout/retries;

    if(retries>0){
        List<WebElement> elements = appiumDriver.findElements(by);
        if(elements.size()>0){
            success = true;
            return success;
        }else {
            appiumDriver.manage().timeouts().implicitlyWait(waitSlice, TimeUnit.SECONDS);
            retries--;
        }
    }
    return success;
}

谢谢

推荐答案

根据您共享的代码块,我没有看到任何通过 检查 if element is present 的附加值隐式等待.该实现看起来是一种纯粹的开销.相反,如果您查看 ExpectedCondition 来自 org.openqa.selenium.support.ui 包的接口,该接口模拟了可能被评估为不符合条件的条件null 或 false 还包含 ExpectedConditions 可以由 WebDriverWait 类和 方法 提供了更精细的方法来确认特定条件是否已达到.这使我们可以更灵活地选择 WebElement 的所需行为.一些广泛使用的方法是:

As per the code block you have shared I don't see any value addition to check if element is present through implicitlyWait. The implementation looks as a pure overhead. Instead if you look into the Java Docs of ExpectedCondition Interface from the org.openqa.selenium.support.ui package which models a condition that might be expected to evaluate to something that is not null nor false also contains the ExpectedConditions Class that can be called in a loop by the WebDriverWait Class and the methods provides more granular approach to confirm if a particular condition have achieved or not. This gives us much more flexibility in choosing the desired behavior of a WebElement. Some of the widely used methods are :

  • 元素的存在:

  • Presence of an element :

presenceOfElementLocated(By locator)

  • 元素的可见性:

  • Visibility of an element :

    visibilityOfElementLocated(By locator)
    

  • 元素的交互性:

  • Interactibility of an element :

    elementToBeClickable(By locator)
    

  • 注意:根据 documentation 不要混合隐式和显式等待.这样做可能会导致不可预测的等待时间.

    Note : As per the documentation Do not mix implicit and explicit waits. Doing so can cause unpredictable wait times.

    这篇关于用于等待元素可用于 Apache Cordova Webview 驱动的应用程序的 Java Wrapper 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆