[Android] Realize the automatic ticket grabbing program of Damai.com app based on Airtest

0x01 Origin

In May 2023, when the epidemic ended, everyone was a little crazy about going out to play, and singers also got together to hold concerts. But there are so many concerts, tickets are not easy to grab at all, and the difficulty of grabbing tickets for Mayday is no less than buying high-speed rail tickets for May Day. So I want to try to find some scripts to help grab tickets. I used to use selenium and request to do some small crawlers to get automated work, so I changed it on the basis of MakiNaruto/Automatic_ticket_purchase to realize the ticket grabbing function. But Damai.com is really cunning . After changing the crawler, I found that almost all popular concerts are only allowed to be purchased in the app, so I need to use the app to automate the interface.

0x02 Airtest automated testing

The first thing that comes to mind is to implement this function by operating on the UI. Currently, the more popular frameworks are NetEase’s poco and appium. After a comparison, I found that poco is relatively simple and easy to use, and it is also based on python syntax, so I chose this framework.

Build the operating environment

  • Airtest IDE: poco runs Airtest IDE based on Netease, just download and unpack from the official website and install it
  • adb debugging: Turn on the usb debugging function of the developer option in the settings of the Android phone. For Xiaomi and Huawei phones, it should also allow installation of apps via USB.

After configuration, click connect to connect to the phone
connect device

Operation recording

I think the most convenient place for Airtest IDE is to automatically analyze the controls of the apk window, record operations, and generate code. As shown in the figure below, click the button in the upper right corner of the poco auxiliary window to start recording.

image

At this point, click the corresponding control in the device window to automatically generate code in the code window on the left.

image

Generated code example

# 点击操作
poco("cn.damai:id/project_poster_mask_iv").click()
poco("cn.damai:id/rich_text_tv").click()
poco("cn.damai:id/tv_left_main_text").click()
poco("cn.damai:id/title_back_btn").click()

# 滑动操作
poco("cn.damai:id/rich_text_tv").swipe([-0.0254, -0.3666])

In addition, inspector mode can also be supported. Click the part of the red button in the figure below to open the inspector, similar to the inspector of the chrome browser console. Then move the mouse in the device window, and right-click on the target position, you can also see the name of the control.

Inspector mode:
image

In addition, you can also click on the tree structure node of the window in the poco auxiliary window to analyze the specific control name and type

Other control operations

  1. Wait for the controls to appear . wait(timeout=1), if the control appears within the timeout time, return the control, otherwise return None

For example, the following code waits for sku_contanier to appear, if it does not appear, it proves that this page is not the page for selecting a ticket:

    if not poco("cn.damai:id/sku_contanier").wait(timeout=1):
        logger.debug("未在票档页")
        return False
  1. Get child controls . offspring()A control used to get all descendant nodes of a control. .child()Get subordinate nodes.

like:

# 遍历控件,返回第一个的item_text节点
poco("cn.damai:id/project_detail_perform_flowlayout").offspring("cn.damai:id/item_text")

# 遍历子节点,并单击子节点的checkbox
    for viewer_widget in  poco("cn.damai:id/recycler_view").offspring("cn.damai:id/recycler_main").child():
        viewer_widget.offspring("cn.damai:id/checkbox").click()
  1. Get properties . get_text()It is used to get the text content, and attr()other properties can be obtained.
#获取tv_tag的文字
tag_str=tv_tag.get_text() 

# 判断复选框是否已被选中
viewer_widget.offspring("cn.damai:id/checkbox").attr("checked") 
  1. click .click()
viewer_widget.offspring("cn.damai:id/checkbox").click()

code development

Based on the above-mentioned API provided by poco, automated testing tools can be combined like building blocks to realize automated ticket grabbing.
The complete code is posted at: https://gist.github.com/m2kar/4f4c1cabe047ac77d5ca0a3b35fad4e1

However, this method needs to interact with the App UI through adb, and the calling framework is heavy, so it runs very slowly. The actual measurement takes 17s, which is much slower than humans, so it is not very practical in actual ticket grabbing.

Therefore, the author made an in-depth analysis of the interface implementation of the Damai.com app, and wrote it in the article " [Android Reverse] A Maiwang APK ticket grabbing interface encryption parameter analysis and ticket grabbing tool development " article.

reference

For more poco operations, please refer to the official documentation: https://airtest.doc.io.netease.com/tutorial/3_Poco_introduction/

Welcome to star my GitHub, blog posts will start on GitHub by default.

Original link of this article: https://github.com/m2kar/m2kar.github.io/issues/20

Guess you like

Origin blog.csdn.net/still_night/article/details/130767783
Recommended