Python+appium automation example: use Python 3+appium to automatically collect Payment Ant forest energy

1. Environmental Construction

 

1. Download and install appium.On the official website http://appium.io/ Download and install the appium installation package.

2. Install Android SDK.Can be directly in ( https://android-sdk.en.softonic.com ) in the download.You can also install Android Studio first, then install it in Android Studio.

3. Configure environment variables: Add a new variable name: ANDROID_HOME, corresponding to the Android SDK path.Examples: D:\Android\Sdk.Then find Path and add:%ANDROID_HOME%\platform-tools.

 

2. Brief introduction

 

1. Start appium.

(1) Click the start server directly.

 

 

(2) Start the session and click the first button.The current page can view the real-time logs, and view all logs by clicking the second button.Stop this service session and click the third button to stop the service.

 

 

The following image page appears after the session starts:

3. Before connecting to your mobile phone, you need to set it in Developer mode to allow USB debugging.

4. Configure desired capabilities related parameters on the page started by the appium session.

Desired capabilities are sent by Appium Client to Appium Server as a json-formatted key-value pair.

    platformName: Platform name, such as ios, android
platformVersion: corresponding platform version
deviceName: device name, mobile phone model
appPackage: The package name of the application
appActivity: The Activity of the application
automationName: Automatic

How do I find the appPackage and appActivity parameters?

 

(1) Find the location of the apk information tool (aapt tool for android SDK) first; (Android\Sdk\build-tools\29.0.3\aapt.exe)

(2) Download Alipay app and remember the storage location;

(3) Open the command line and enter the following information: aapt.exe dump badging D:\alipay_wap_main.apk (Alipay apk path)

Values for'appPackage'and'appActivity' can be found in the output information:

 

 

3. Development Steps

 

1. appium parameter configuration.

The following is my configuration:

{
  "platformName": "Android",
  "deviceName": "ALP-AL00",
  "appPackage": "com.eg.android.AlipayGphone",
  "appActivity": "com.eg.android.AlipayGphone.AlipayLogin",
  "noReset": "true",
  "fullReset": "false",
  "automationName": "UiAutomator1"
}


Once the configuration is complete, click on start session to run Alipay mobile app.

2. After entering the Alipay application, use the mouse to click on the Alipay interface on the left, and display the information of the click item on the right. When you need to enter the specified item, click on the top right Tap to enter.

Click in turn:

The order is: Enter Alipay --->More --->Ant Forest --->View Many Friends --->Click successively to enter Friends Ant Forest homepage.Record the source-id you want to apply each time you click.Required for code editing.

You can also have a small eye at the top of the page, click on it to record code for the operation, while recording, you can choose a code type, such as Java, Python, etc.

 

3. Python implementation steps

(1) Install appium package: pip install appium

(2) Detect the presence or absence of energy balls.Below, if there is an energy ball on the page, the number of classnames (android.widget.Button) on the page is greater than 5, and the text of the energy ball contains the word "energy".A non-collectible energy ball does not contain the word "energy".So you can tell if there are any energy balls to collect based on the number of classnames and whether the text contains the word "energy".

Why should the number of classname s be greater than 5?Because there are five basic buttons on the page, such as achievements, water, marbles, etc.

 

The code is as follows:

    items = driver.find_elements_by_class_name("android.widget.Button")
        if len(items)>5:
            for i in items:
                if 'energy' in i.text:
                    print('Collect{0}Of{1}'.format(name,i.text.replace('collect','')))
                    i.click()
            #driver.find_elements_by_class_name('android.widget.Image')[1].click()
            time.sleep(0.5)

 

(3) After collecting the energy of the first friend, you need to go to the home page of the second friend, and the way to do this is to slide the page up about the height of a friend box.


        start_x = 500  
        start_y = 1910
        distance = 195  # About the height of a box
        driver.swipe(start_x, start_y, start_x, start_y - distance)    # Slide up the height of a box   # driver.swipe(Indicates the start and end positions of the slide, respectively x/y Coordinates)

 

(4) Avoid missing friends whose energy is not yet up to date, return to the top to re-detect.

        name = driver.find_element_by_id('com.alipay.mobile.nebula:id/h5_tv_title').text
        if name=='Ant forest of somebody': #Fill in the last friend nickname
            driver.tap([(50, 130), (70, 150)], 100) #Return to the Weekly Charts home page( < Ant Forest )    driver.tap(x coordinate,y coordinate,t(ms))
            time.sleep(1)
            driver.tap([(50, 130), (70, 150)], 100) #Return to Ant Forest Homepage
            Swipe(driver)    # See more friends
            continue

(4) The complete code is as follows: Refer to the Wechat public number "Naked Sleeping Pig"*

from appium import webdriver
import timefrom appium.webdriver.common.touch_action import TouchAction

desired_caps = {
                "platformName": "Android",
                "deviceName": "MHA_AL00",
                "appPackage": "com.eg.android.AlipayGphone",
                "appActivity": "AlipayLogin",
                "noReset": "true",
                "fullReset": "false",
                "automationName": "UiAutomator1"
}

server = 'http://localhost:4723/wd/hub'
driver = webdriver.Remote(server, desired_caps)
time.sleep(2)
driver.find_element_by_id('com.alipay.android.phone.openplatform:id/more_app_icon').click() #Click More
time.sleep(2)
driver.find_element_by_id('com.alipay.android.phone.wallet.homemarket:id/app_group_item_icon').click() #Click on the ant forest
time.sleep(2)

def Swipe(driver):
    n=0
    while n<=5:
        start_x = 500
        start_y = 1500
        distance = 1000
        driver.swipe(start_x, start_y, start_x, start_y - distance)
        n=n+1
    driver.find_element_by_xpath("//*[@text='see more friends']").click() #Click to see more friends
    time.sleep(1)

def run(driver):
    Swipe(driver)
    while True:
        TouchAction(driver).press(x=150, y=700).release().perform() #First Ant FriendFrame coordinates
        time.sleep(0.5)

        name = driver.find_element_by_id('com.alipay.mobile.nebula:id/h5_tv_title').text
        if name=='': #Fill in the last friend nickname
            driver.tap([(50, 130), (70, 150)], 100) #Return to the Weekly Charts home page( < Ant Forest )    driver.tap(x coordinate,y coordinate,t(ms))
            time.sleep(1)
            driver.tap([(50, 130), (70, 150)], 100) #Return to Ant Forest Homepage
            Swipe(driver)
            continue
        print('Viewing{0}'.format(name))
        items = driver.find_elements_by_class_name("android.widget.Button")
        if len(items)>5:
            for i in items:
                if 'energy' in i.text:
                    print('Collect{0}Of{1}'.format(name,i.text.replace('collect','')))
                    i.click()

            time.sleep(0.5)
            driver.tap([(50, 130), (70, 150)], 100)
            time.sleep(0.1)      

        start_x = 500  
        start_y = 1910
        distance = 195  # Height of a box
        driver.swipe(start_x, start_y, start_x, start_y - distance)    # Slide up the height of a box   # driver.swipe(Indicates the start and end positions of the slide, respectively x/y Coordinates)

        time.sleep(0.2)

if __name__ == '__main__':
    run(driver)

Note: When collecting energy, each time you go to the first friend's ant home page, the code always does not go up the sliding part, but you can go to it when debug, and then sleep can enter normally after 2 seconds.So during debugging, if you encounter a problem that prevents you from proceeding to the next step, add sleep as appropriate.

Tags: Python Android SDK Session Mobile

Posted on Tue, 17 Mar 2020 19:51:43 -0700 by firecircle