Master Mobile UI Automation with Airtest: Full E‑Commerce Flow Example
This article introduces the open‑source Airtest framework for mobile UI automation, outlines its cross‑platform features, and provides a complete Python script that automates an end‑to‑end e‑commerce purchase, payment, return, and refund process with test data cleanup.
Overview
Airtest is an open‑source UI automation testing framework designed for mobile applications and games. It offers a simple API and visual tools that let developers and QA engineers quickly write and run automated test scripts across multiple platforms.
Key Features
Cross‑platform support for Android, iOS, and Windows.
Image‑recognition based element locating, eliminating the need for UI control IDs.
Script recording and playback to generate test code automatically.
Supports Python and Lua, allowing testers to use familiar languages.
Cloud device service enables execution on remote devices without physical hardware.
Sample E‑commerce Test Script
The following Python script demonstrates a comprehensive e‑commerce purchase flow, including login, product browsing, adding to cart, checkout, payment, return, refund, and test data cleanup. Each step uses Airtest’s touch, text, and assert functions together with image templates.
from airtest.core.api import *
from airtest.report.report import simple_report
# Connect to device
device = connect_device("android://")
def test_login():
start_app("com.example.shop")
touch(Template("username_input.png"))
text("myusername")
touch(Template("password_input.png"))
text("mypassword")
touch(Template("login_button.png"))
assert exists(Template("welcome_message.png"))
def test_browsing_products():
touch(Template("products_tab.png"))
touch(Template("product_1.png"))
assert exists(Template("product_details.png"))
def test_add_to_cart():
touch(Template("add_to_cart_button.png"))
assert exists(Template("add_to_cart_success.png"))
def test_checkout():
touch(Template("cart_tab.png"))
assert exists(Template("cart_items.png"))
touch(Template("checkout_button.png"))
touch(Template("address_input.png"))
text("123 Main St")
touch(Template("city_input.png"))
text("City")
touch(Template("zipcode_input.png"))
text("12345")
touch(Template("confirm_order_button.png"))
assert exists(Template("order_success.png"))
def test_payment():
touch(Template("payment_tab.png"))
touch(Template("payment_password_input.png"))
text("mypaymentpassword")
touch(Template("confirm_payment_button.png"))
assert exists(Template("payment_success.png"))
def test_return():
touch(Template("returns_tab.png"))
touch(Template("product_to_return.png"))
touch(Template("return_reason_input.png"))
text("Defective item")
touch(Template("confirm_return_button.png"))
assert exists(Template("return_success.png"))
def test_refund():
touch(Template("refunds_tab.png"))
touch(Template("order_to_refund.png"))
touch(Template("refund_amount_input.png"))
text("10.00")
touch(Template("confirm_refund_button.png"))
assert exists(Template("refund_success.png"))
def test_cleanup():
# Remove test orders or reset environment state
pass
def test_logout():
touch(Template("logout_button.png"))
assert exists(Template("login_button.png"))
if __name__ == '__main__':
test_login()
test_browsing_products()
test_add_to_cart()
test_checkout()
test_payment()
test_return()
test_refund()
test_cleanup()
test_logout()
simple_report("report.html")Conclusion
Running this script on an Android device (or adapting the driver for iOS/Windows) will simulate a user’s complete purchase journey, verify each critical UI interaction, and clean up test data afterward, demonstrating how Airtest can be used for robust mobile UI automation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
