Master Mobile Automation with Airtest: From App Launch to Endless Swipes
This tutorial walks you through using Airtest to automate mobile apps—showing how to launch apps via image recognition or package name, perform likes and comments, capture screenshots, enable real‑time coordinates, and create endless swipe loops with Python code.
Introduction
In the previous article we covered the basics of Airtest. This guide demonstrates practical operations such as launching apps, performing likes and comments, taking screenshots, enabling real‑time coordinate display, and automating continuous swipe actions.
Basic Operations
Use Airtest's cross‑platform API to interact with an APK. First, click Touch and select the app area; Airtest generates the corresponding script automatically.
After selecting the area, the script editor shows the generated code.
1. Open Target App and Wait for Loading
Because app launch times vary, you must wait until the app is fully loaded before proceeding. The example uses the short‑video app Weishi.
1) Open via Image Recognition
This method works only when the app icon is visible on the current screen.
2) Open via Package Name
First retrieve all package names, then launch the desired one.
dev = device() # Get the current Android device object
print(dev.list_app()) # List installed apps
start_app('com.tencent.weishi') # Launch Weishi by package name2. Like, Comment, and Close
Using the Douyin short‑video app, capture the app’s image, wait for it to appear, then perform like and comment actions before exiting.
After entering a comment, close the comment panel and return to the home screen. Remember to add a short wait() before clicking an image to avoid errors.
3. Capture Screenshots and Swipe
Use snapshot() to take a screenshot (specify an absolute path).
snapshot(filename=r'C:\Users\Administrator\Desktop\test.jpg', msg='截图成功')Enable real‑time coordinate display to obtain exact positions for swipe actions.
After enabling coordinates, use swipe() to perform screen swipes.
4. Continuous Swiping Loop
Implement an infinite loop with random delays to keep swiping short videos.
import random
wait(Template(r"tpl1594203055954.png", record_pos=(0.356, 0.562), resolution=(1080, 2340)))
sleep(1)
touch(Template(r"tpl1594203066034.png", record_pos=(0.354, 0.564), resolution=(1080, 2340)))
sleep(2)
exists(Template(r"tpl1594203382253.png", record_pos=(0.415, 0.139), resolution=(1080, 2340)))
sleep(2)
snapshot(filename=r'C:\Users\Administrator\Desktop\test.jpg', msg='截图成功')
sleep(2)
assert_exists(Template(r"tpl1594203920118.png", record_pos=(0.411, 0.33), resolution=(1080, 2340)), "请填写测试点")
while True: # endless swipe loop
aa = random.randint(1, 5)
swipe((700, 1950), (700, 300))
sleep(aa)You can also embed additional actions such as likes or comments inside the loop.
Conclusion
By mastering Airtest, you can effortlessly automate short‑video app interactions—liking, commenting, swiping, and more—providing a powerful tool for mobile testing and growth hacking.
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.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
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.
