Automating Android Hybrid Apps
Automating Android Hybrid Apps
Hybrid Android applications contain both native components and web views. On TestGrid, hybrid apps can be automated in a similar way to how web applications are tested using Selenium.
To automate a hybrid app, you must explicitly tell the automation framework whether you want to interact with:
- Native elements, or
- Web view (HTML/DOM) elements
Once the test switches to a web view context, the full Selenium WebDriver API becomes available.
Entering the Web View Context
Follow the steps below to interact with a web view in an Android hybrid application using Appium.
Step 1: Navigate to the Web View Screen
Launch the application and navigate to the part of the app where the web view is loaded and visible.
Step 2: Retrieve Available Contexts
Use the following code to fetch all available contexts (native and web view):
Set<String> contexts = driver.getContextHandles();
for (String context : contexts) {
System.out.println(context);
}
This code can be added to a custom script to print all available contexts.
Typical output includes:
NATIVE_APPWEBVIEW_<package_name>(for example,WEBVIEW_com.example.app)
Step 3: Switch to the Web View Context
Once the web view context is available, switch to it using the code below:
Set<String> contexts = driver.getContextHandles();
for (String context : contexts) {
if (!context.equals("NATIVE_APP")) {
driver.context(context);
break;
}
}
After switching, all WebDriver commands are executed against the web view DOM instead of native UI elements.
⚠️ Note: Some WebDriver methods are context-specific. If a command is executed in the wrong context (native vs web), an error will be thrown.
Step 4: Perform Actions on the Web View Using Selenium API
Once in the web view context, you can use standard Selenium locators and actions.
Example:
WebElement usernameEditText =
driver.findElement(By.cssSelector("#proxyUsername"));
usernameEditText.sendKeys("613528");
This locates the input field using the CSS selector #proxyUsername and enters the value 613528.
Step 5: Switch Back to the Native Context
After completing web view automation, switch back to the native context to continue interacting with native elements:
driver.context("NATIVE_APP");
This restores native automation commands.
Summary
- Hybrid apps require context switching between native and web views
- Use
getContextHandles()to identify available contexts - Switch to
WEBVIEWfor Selenium-based DOM automation - Switch back to
NATIVE_APPfor native UI interactions
This approach ensures seamless automation of both native and web components in Android hybrid applications on TestGrid.
