I am working on automating a iOS app using Appium-Java.
In appium documentation it was mentioned that- "autoWebView" APPIUM capability should be used for Cordova based app
In our case, app keeps on stacking multiple WebViews, as the user goes on navigating the screens. And in that case normally I have 1 Native View & 3-4 WebViews. So if I set autoWebView, will it gurantee that- it will always point to correct webview (i.e. the webview, which contains HTML for current page)
For our unique requirement (Cordova based Hybrid app creating Multiple webviews), I have created a reusable function to point to intended WebView:
public boolean switchToMeaningfulWEBVIEWUsingPageTitle(final String title) {
Set<String> contextNames = appiumDriver.getContextHandles();
List<MobileElement> elements = null;
String defaultContext = appiumDriver.getContext();
for (String contextName : contextNames) {
System.err.println("\n ** DEBUG: contextName = "+contextName);
appiumDriver.context(contextName);
appiumDriver.manage().timeouts().implicitlyWait(6, TimeUnit.SECONDS);
if (contextName.contains("WEBVIEW")) {
String metaTitle = appiumDriver.getTitle();
if (metaTitle.equalsIgnoreCase(title)) {
if(appiumDriver.getPageSource().trim().contains("<body></body></html>")) {
continue;
}else{
return true;
}
}
}
}
appiumDriver.context(defaultContext);
return false;
}
Basically when we've to some operation on WebView of the latest page, we toggle to concerned webView from Stack of webviews. Html Title is used as reference, as we try to keep it unique but something else could also be used for unique reference. Thanks