Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...