App types, contexts and gestures
Apps are native, hybrid with a WebView, or mobile web, and for the driver native and WebView are different contexts with different element trees. A session starts in NATIVE_APP, so for WebView content you switch with getContextHandles() and context(...) and then switch back. Gestures use W3C Actions or mobile: commands; the legacy TouchAction is not used after Appium 2.
Appium · Apps and gestures
The "Pay" button is on the screen, yet Appium cannot see it. Where does the "empty" screen come from?
A hybrid app looks like one whole, but for the driver there are two different element trees: native and web. A button in a WebView is unavailable to native locators, and a native panel is unavailable to CSS. This lesson covers what kinds of apps exist, how to switch between them, and how to perform gestures when a simple click is not enough.
App types.
| Type | What it is | How to set it in the session |
|---|---|---|
| Native | Written for the platform (Kotlin, Swift) | appium:app or appium:bundleId / appium:appPackage |
| Hybrid | A native shell with a WebView | as native, plus a context switch |
| Mobile web | A site in the device browser | browserName: Chrome or Safari instead of appium:app |
How it works and why: contexts. A session starts in the NATIVE_APP context. A context is a set of rules by which the driver finds elements and runs commands. To work with WebView content you have to switch: driver.getContextHandles() returns NATIVE_APP and one WEBVIEW_… context per active WebView, and driver.context(name) changes the context. In a WebView the driver works as a web driver (CSS and XPath locators), in native as a mobile one. A command that does not belong to the context returns an error.
Contexts
Set<String> contexts = driver.getContextHandles(); // [NATIVE_APP, WEBVIEW_com.example]
String web = contexts.stream()
.filter(c -> c.startsWith("WEBVIEW"))
.findFirst()
.orElseThrow(() -> new IllegalStateException("WebView not found"));
driver.context(web);
driver.findElement(By.cssSelector("#checkout")).click(); // web locator
driver.context("NATIVE_APP"); // back
- Android: the WebView must be debuggable, and the Chromedriver version used by the driver must match the WebView version (the path to your own executable is set by the
appium:chromedriverExecutablecapability). - iOS: for a WKWebView the developer must set
isInspectabletotrue, and on a real device Web Inspector must be enabled in the Safari settings. If a context does not appear, check whether it is visible in Safari remote debugging. appium:autoWebview(UiAutomator2 and XCUITest) starts the session directly in the WebView, if the app consists almost entirely of it.- Do not pick a context by index in the list: the order is not guaranteed. Search by the
WEBVIEWprefix and by the package name.
Gestures. The old TouchAction worked on a legacy protocol that Appium 2 removed, so it is not used. There are two ways. The first is W3C Actions: PointerInput + Sequence and driver.perform(...). They are universal but need coordinates:
PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
Sequence swipe = new Sequence(finger, 1)
.addAction(finger.createPointerMove(Duration.ZERO,
PointerInput.Origin.viewport(), 900, 1600)) // start
.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()))
.addAction(finger.createPointerMove(Duration.ofMillis(600),
PointerInput.Origin.viewport(), 900, 400)) // end
.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
driver.perform(List.of(swipe));
The second way is the ready driver commands mobile: …, called through driver.executeScript. They accept an element or an area, a direction and a percentage rather than pixel coordinates, so they depend less on the screen size:
| Action | UiAutomator2 | XCUITest |
|---|---|---|
| Tap | mobile: clickGesture | mobile: tap |
| Swipe | mobile: swipeGesture | mobile: swipe |
| Scroll | mobile: scrollGesture | mobile: scroll |
| Long press | mobile: longClickGesture | mobile: touchAndHold |
| Deep link | mobile: deepLink | mobile: deepLink (iOS 16.4+) |
// Android (UiAutomator2): scroll until the element appears
WebElement list = driver.findElement(AppiumBy.id("com.example:id/list"));
boolean more = true;
while (more && driver.findElements(AppiumBy.accessibilityId("item_42")).isEmpty()) {
more = (Boolean) driver.executeScript("mobile: scrollGesture", Map.of(
"elementId", ((RemoteWebElement) list).getId(),
"direction", "down",
"percent", 0.75)); // false: nowhere left to scroll
}
// iOS (XCUITest): the counterpart
driver.executeScript("mobile: swipe", Map.of("direction", "up"));
mobile: scrollGesture returns a boolean: true if more scrolling is possible and false if the end is reached. The loop "scroll while the element is missing and there is room" does not hang at the end of a list.
System things. Permissions are solved without hunting for a dialog in the test: appium:autoGrantPermissions (UiAutomator2) and appium:autoAcceptAlerts (XCUITest). A deep link opens the needed screen without step-by-step navigation, and sending the app to the background and back checks whether it kept its state:
// Deep link: open a screen without navigation (Android)
driver.executeScript("mobile: deepLink", Map.of(
"url", "myapp://orders/42",
"package", "com.example.app"));
// send the app to the background for 5 seconds and back (Android)
driver.executeScript("mobile: backgroundApp", Map.of("seconds", 5));
Limits and alternatives.
- Gestures by coordinates break on a different screen size: take
mobile:commands with an element and a percentage, and keep W3C Actions for non-standard paths. - The list of
mobile:arguments depends on the driver and version: check the driver documentation, not another project's example. - Automatic acceptance of system dialogs hides their behaviour. A test that checks the permission request itself must not turn it off.
- Scrolling to an element through
UiScrollableon Android is shorter than a loop, but tied to UiSelector, which the documentation warns about. - For mixed screens it is handy to have a helper "run in the WebView" that switches there and back in
finallyby itself.
Typical mistakes.
- Looking for a WebView element with a native locator (and vice versa) without switching context.
- Not returning to
NATIVE_APP: the next steps look for elements in the wrong place. - Taking a context by position in the list or waiting for it without retries: a WebView does not appear at once.
- A swipe by hard-coded pixels that works on one phone only.
- Using
TouchActionfrom old examples.
Check yourself
Why does a native locator not find a button inside a WebView?
Answer
In a hybrid app the WebView has its own context. While the driver is in NATIVE_APP it looks for native elements. You must switch through driver.context("WEBVIEW_…"), and then CSS and XPath work.
Why are W3C Actions or mobile: commands used instead of TouchAction?
Answer
TouchAction worked on a legacy protocol that Appium 2 removed. W3C Actions are standard, and the driver mobile: commands accept an element, a direction and a percentage, so they depend less on screen size.
What does mobile: scrollGesture return, and why is it useful?
Answer
A boolean: true if more scrolling is possible, false at the end. Thanks to that a loop scrolling to an element does not hang at the end of a list.
browserName instead of appium:app). A session starts in NATIVE_APP; for a WebView I take getContextHandles and context(...), then return. I do gestures through W3C Actions or mobile: commands and no longer use TouchAction. Middle/Senior: I pick a context by prefix, not by index, and wrap it in a helper that returns in finally. For gestures I prefer driver commands with an element and a percentage, because they survive a different screen diagonal. I handle permissions and system dialogs with capabilities, but a test of the permission request itself does not turn them off. I use a deep link so as not to walk through navigation in every test.