Mobile Development 18 min read

Why UI Automation Matters for Mobile Apps and Using Appium with Cucumber

This article explains why UI automation testing is crucial for complex mobile apps, introduces Appium as a cross‑platform open‑source solution, demonstrates organizing test cases with Cucumber and Page Object patterns, details element locating strategies, custom steps, workflow architecture, and discusses current limitations and improvement plans.

Huajiao Technology
Huajiao Technology
Huajiao Technology
Why UI Automation Matters for Mobile Apps and Using Appium with Cucumber

Need for UI Automation Testing

Mobile apps are tightly coupled systems; unit tests cannot guarantee end‑to‑end functionality. Manual UI testing is slow and repetitive, so a CI‑driven automation solution is required to reduce manual effort and increase test efficiency.

Why Appium

Open‑source tool that drives native, hybrid, and web applications on Android, iOS, and Windows.

Hybrid apps embed web pages; Appium can switch between native and web contexts seamlessly.

Appium uses each platform’s native testing framework, avoiding third‑party code injection or app re‑packaging.

Supported automation back‑ends:

Android 4.2+ – UiAutomator / UiAutomator2 (default)

Android 2.3+ – Instrumentation (provided by Selendroid)

iOS 9.3+ – XCUITest

iOS ≤ 9.3 – UIAutomation

Actively maintained on GitHub; community adds features such as log collection, screen recording, OpenCV‑based image recognition, and support for recent OS versions (iOS 13, Android 10).

Custom plugins can extend element search. Example AI‑powered icon‑recognition plugin URL: https://github.com/testdotai/appium-classifier-plugin. Developers can also write plugins that use image recognition or OCR.

Organizing Test Cases with Cucumber

Cucumber provides BDD‑style, near‑natural‑language step definitions, improving readability over raw code. Java 8 and Cucumber support UTF‑8, allowing Chinese step names.

@当("^点击 \"([^\"]*)\"$")
public void findElementAndClick(String type) throws Throwable {
    driver.findElement(By.id(type)).click();
}

Test cases follow the Page Object pattern: each UI screen is represented by a Page class that contains element locators and reusable actions. Example resource‑id for the Huajiao app home page search button: com.huajiao:id/main_home_top_search After defining basic click, swipe, and input operations, a test case can be written in a natural‑language style:

# "$首页.搜索" means using the "搜索" element on the Home page
当 点击 $首页.搜索
# "$搜索.搜索()" calls the search method with a keyword parameter
$搜索.搜索(43011080)
当 断言元素出现 $搜索.搜索结果

Complex Custom Operations

Cucumber executes steps sequentially and cannot express branching or loops. Complex logic must be implemented in custom step definitions. The following Java step handles logout for both logged‑in and logged‑out states:

@当("^退出登录$")
public void logout() throws Throwable {
    // Click "Home‑My"
    onView(By.id("com.huajiao:id/bottom_tab_user")).perform(click());
    try {
        // Logged in: element is visible
        onView(By.id("com.huajiao:id/bottom_tab_user")).check(matches(isDisplayed()));
        // Perform logout
        logOut();
    } catch (NoSuchElementException e) {
        // Not logged in: close login popup via back key
        onActions().pressKeyCode(AndroidKey.BACK, "1").perform();
    }
}

Finding UI Elements with Appium

Basic locators By.id – locate by resource‑id. MobileBy.AndroidUIAutomator(String code) – use UiAutomator2 selector code, e.g.,

String code = "new UiSelector().textContains(\"" + text + "\");"

.

XPath XPath can locate elements when id or class name fails, but it is fragile to layout changes and slower on iOS. Prefer resource‑id based strategies.

Although precise, XPath paths may break with UI changes and perform poorly on iOS; therefore prioritize resource-id selectors.

Sibling example:

xpath://*[@text='TEXT']/../android.widget.TextView[@resource-id='ID']

Child with attribute:

xpath://*[@resource-id='ID' and @selected='true']/*[@attr='value']

Image‑based recognition Appium supports image matching via MobileBy.image(base64ImageString) . Only the first matching element is returned.

Install OpenCV for Node.js: npm install -g opencv4nodejs Configure Appium settings, for example:

// Set image match threshold (default 0.4)
driver.setSetting(Setting.IMAGE_MATCH_THRESHOLD, 0.5);
// Disable stale‑image check to speed up repeated operations
driver.setSetting(Setting.CHECK_IMAGE_ELEMENT_STALENESS, false);

StaleElementReferenceException : Occurs when an element is found but later removed from the UI hierarchy (e.g., after a page transition or when a popup obscures the element). The cached element reference becomes invalid, causing the exception.

Scenario: element ID exists on both pages A and B; after navigating to B, Appium may still use A’s cached reference.

Network latency between the find and click HTTP requests can also produce stale references if transient UI changes happen.

Overall Workflow

htest client fetches the latest APK from the server, installs it if newer, and triggers BVT test execution.

The test platform selects Cucumber‑described BVT cases, resolves Page objects, and translates steps into element locators ( id: for resource‑id, text: for text search). The translated steps are sent to the client via HTTP or socket.

During execution, unexpected pop‑ups (e.g., first‑charge dialogs) are detected and dismissed before retrying element lookup.

The client initializes an Appium driver, connects to the device, and performs the scripted actions.

If a test fails, it is retried; persistent failures trigger log collection, screenshot, screen‑recording, and result reporting back to the platform.

Framework Modules

Test Platform : Web UI for storing and editing Cucumber cases, managing Page objects, and translating steps into executable commands.

htest Server : Java Netty middleware that forwards socket messages between the platform and client. Example server bootstrap code uses BaseServer, NioEventLoopGroup, and ServerBootstrap to bind IP and port.

HttpServer / JarServer / WebsocketServer : Different transport implementations sharing similar startup logic; each uses a specific handler class ( ServerHttpHandler, ServerHandler, ServerChannelHandler) to process HTTP, protobuf, or websocket messages.

htest Client : Java client that defines Cucumber steps, updates the APK, initializes Appium, and runs test cases. Executed with java -jar htest-client.jar and a YAML configuration that specifies device parameters, APK version policy, and result reporting.

Appium : Node.js service that drives the device using UIAutomator2 (Android) or XCUITest (iOS) for basic actions.

Issues and Improvements

Current client supports only a single USB‑connected device. Parallel execution across multiple devices can be achieved by enabling ADB over TCP/IP.

Failure‑capture video is limited to 3 minutes using ADB recording; switching to scrcpy would improve compatibility and allow longer recordings.

Standard ID/text locators work well for native controls, but custom controls lacking resource‑ids require XPath, which is slower and sometimes ambiguous. Image matching accuracy varies, especially for Flutter UI. A custom Appium plugin that combines image recognition and OCR can provide more reliable element location.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

UI automationmobile testingimage recognitionAppiumPage ObjectCucumber
Huajiao Technology
Written by

Huajiao Technology

The Huajiao Technology channel shares the latest Huajiao app tech on an irregular basis, offering a learning and exchange platform for tech enthusiasts.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.