The number of the context changes with every new build of the app for iOS webviews.
Eg. There is a button which opens a webview and I write:
driver.context(“WEBVIEW_4”).switchTo();
I get a new build from the app and the context changes to WEBVIEW_5
Is it an Appium related issue at all?
My app always has two web contexts and only one of them is valid. Moreover with every launch of app, webviews have different names.
In your case, you just should switch to context using regex. That’s what I have (Ruby): https://gist.github.com/kirillzh/9eae2fd8bc8b0a2fc710.
Basically, you wait for any WEBVIEW context using regex and since you have one one context, just pick it from array.
Thanks. It is a good solution. But in my case I have multiple webviews in the app and it is not easy to manage these name changes while I am trying to navigate between them.
Assuming that you have something on each/desired web-view say unique ID or Text.
Switch to every web-view and get the page source of that web view, match that if it’s the one you want. If it is break the loop else keep switching.
Hope it works
Yeah, finally this is what I did. Iterating through the WEB contexts to find the right one:
public boolean changeContextsUntilFindID(String id) {
try {
Set<String> contextNames = driver.getContextHandles();
int listSize = contextNames.size();
int i = 0;
String contextName;
//iterate through contexts to find if the element with the given id is present somewhere
log("----------------------------------------------");
log("Trying to find the displayed context...");
while(true) {
contextName = (String) contextNames.toArray()[i];
if (contextName.equals("NATIVE_APP")) {
i+=1;
continue;
}
log("Switch to: " + contextName);
driver.context(contextName).switchTo();
try {
driver.findElement(By.id(id)).isDisplayed();
logPass("Found: " + contextName);
log("----------------------------------------------");
return true;
} catch (NoSuchElementException e) {
if (i == listSize-1) {
return false;
} else {
i+=1;
}
}
}
} catch (Throwable e) {
logFail("Switching to the web part is failed");
}
return false;
}