Java Wrapper method for waiting for element to be available for Apache Cordova Webview driven App

ⅰ亾dé卋堺 提交于 2019-12-02 07:55:29

问题


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;
}

Thanks


回答1:


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)
    

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



来源:https://stackoverflow.com/questions/49749269/java-wrapper-method-for-waiting-for-element-to-be-available-for-apache-cordova-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!