Switching between views - web and native

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) {
if (contextName.contains("NATIVE"))
driver.context(contextName);
}
}

Generally, switching between a WebView and native view happens across the app on different pages, so it will make more sense to have this method created in BasePage. The advantages of this approach are as follows:

  • Easy access to call from any page
  • Avoid duplication of the implementation

We can use the preceding code for reference and may tweak it, if need be. The next tip is taking a screenshot of the app while under execution.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.156.93