Reputation: 1961
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
Upvotes: 1
Views: 355
Reputation: 193208
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.
Upvotes: 1