While testing an app, we often find the need to switch between the Web and native views. A typical example is the Facebook sign-in page in many apps or an intermediate payment page. In those situations, we need to change the application context to WEBVIEW
or NATIVE
. Use the following code snippet to switch to WebView:
public static void changeDriverContextToWeb(AppiumDriver driver) { Set<String> allContext = driver.getContextHandles(); for (String context : allContext) { if (context.contains("WEBVIEW")) driver.context(context); } }
It tries to get a list of all the context handles, checks whether there is any context that contains WebView, and then the driver switches to that context. The following code snippet switches to native on a similar logic:
public static void changeDriverContextToNative(AppiumDriver driver) { Set<String> contextNames = driver.getContextHandles(); for (String contextName : contextNames...