Automating Android Hybrid Apps

You are here:
< All Topics

It is possible to test hybrid apps in TestGrid the same way you can with Selenium for web apps. We just need to let the platform know whether we want to automate the native aspects of the app or the web views. Once the test is in a web view context the command set is available in the full Selenium WebDriver API.

Entering the web view context

Here are the steps required to talk to a web view in your Android test:

  1. Navigate to a portion of your app where a web view is active.
  2. Retrieve the currently available contexts.
Set<String> contexts = driver.getContextHandles();
for (String context : contexts) {

The above line of code you can use in the custom script to get the list of available context.

  •  This returns a list of contexts we can access, like ‘NATIVE_APP’ or ‘WEBVIEW_1’.

Step 3: Set the context for Webview.

Set<String> contexts = driver.getContextHandles();
for (String context : contexts) {
            if (!context.equals("NATIVE_APP")) {
                driver.context((String) contexts.toArray()[1]);
  • This puts your Appium session into a mode where all commands are interpreted as being intended for automating the web view, rather than the native portion of the app. For example, if you run findElement, it will operate on the DOM of the web view, rather than return native elements. Of course, certain WebDriver methods only make sense in one context or another, so in the wrong context you will receive an error message.

Step 4: Perform the required steps on web view using Selenium java API.

For example:

WebElement usernameEditText = driver.findElement(By.cssSelector("#proxyUsername"));
      • This will find the TextField with CSS Selector “#proxyUsername” and enter the “613528” value in the TextField.

Step 5: To stop automating in the web view context and go back to automating the native portion of the app, simply set the context again with the native context id (generally ‘NATIVE_APP’) to leave the web context and once again access the native commands.

    • This will set the context to Native.
Table of Contents