preface
Many small friends should be like Xiaobian, no matter what, they will harvest the energy of the ant forest, just like back to the year of planting vegetables, farms, pastures and so on, but there are always people thinking about my energy, that is to steal my vegetables, although I did not steal less! Ha ha ha! Ok, here’s how you can achieve automatic energy charging!
Necessary environment
Window10 system
Appium1.21.0
AndroidSDK r24.1.1
Python3.7.5
Apk file of Alipay
Check the apK package information of Alipay
Use the Android SDK aapt command to check the apK package information of Alipay, which will be used later, as follows.
Check whether the device is connected
At first, I wanted to use a simulator (such as nighgod simulator) for automation, but later I found that Alipay was running special cards in the simulator, and finally I decided to use the real machine. The adb tool of the Android SDK is used to check whether the mobile device is connected properly, as shown below. If no connection information is displayed or an unauthorized information is displayed, enable USB debugging rights for the phone.
Prepare knowledge
PIP install Change the default installation path
How Appium works
Appium Example of running the Appium test in Python
PIP install appium-python-client –use
An extension library for adding Selenium 3.0 draft and Mobile JSON Wire Protocol Specification draft functionality to the Python language bindings, for use with the mobile testing framework Appium .r
pip install pytest --user
Copy the code
Add a parameter when installing the package — the user package will be automatically installed to the custom path
Start the Appium server
Opens a new session to locate screen elements
{
"deviceName": "Alipay-Test",
"platformName": "Android",
"platformVersion": "10",
"appActivity": "com.eg.android.AlipayGphone.AlipayLogin",
"appPackage": "com.eg.android.AlipayGphone",
"noReset": true,
"fullReset": false
}
Copy the code
Writing Python scripts
Once you know how to locate elements of the Alipay interface, start writing python scripts to automate the run. Roughly divided into the following steps.
Initialize the client
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '10'
desired_caps['deviceName'] = 'Alipay'
desired_caps['appActivity'] = 'com.eg.android.AlipayGphone.AlipayLogin'
desired_caps['appPackage'] = 'com.eg.android.AlipayGphone'
desired_caps['noReset'] = True
desired_caps['fullReset'] = False
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
Copy the code
After the client is initialized, alipay APP will be automatically started. Pay attention to the Settings of noReset and fullReset parameters
If noReset is set to TRUE, the app data will NOT be cleared before this session starts.
If fullReset is set to true, the app will get uninstalled and all data will be cleared.
Copy the code
Unlock the phone
def unlocked(self): sz = self.getSize(); Swipe (sz[1] * 0.15) # self.driver. Swipe (sz[1] * 0.15) # self.driver. Swipe (sz[1] * 0.15) # self.driver. Swipe (sz[1] * 0.15) # self.driver. y2, 1000) sleep(1) try: Self. Driver. Find_element_by_id (' com. Android. Systemui: id/vivo_pin_keyboard ') for k,1,2,9,9,9 [5] in: Self. Driver. Find_element_by_id (' com. Android. Systemui: id/VivoPinkey % d '% k). Click () print (' mobile phone unlocked... ') except NoSuchElementException: print(' phone unlocked or failed to unlock ')Copy the code
Into the ant Forest
def entry_ant_forest(self): try: Find_element_by_android_uiautomator ('new UiSelector().text(" ant Forest ")').click() except NoSuchElementException: Self.driver.back () sleep(2) self.driver.find_element_by_Android_UIautomator ('new UiSelector().text(" pay ")').click() sleep(2) # click on self.driver.find_element_by_android_uiautomator('new UiSelector().text(" ant forest ")').click()Copy the code
You can just click the “Ant Forest” icon to enter the ant Forest simulation, but occasionally NoSuchElementException is thrown. This will not happen if the phone does not lock the screen (which can be specified in developer mode). This will occasionally happen when you switch to alipay’s activity after unlocking the lock screen. Did not find a good solution, when an exception occurs, make the mobile phone interface return to the home page, and then click Alipay to re-enter, and finally click ant Forest to enter.
Search for energy
Self.driver. Tap ([(1000, 1520), (1080, 1580)], 1000) sleep(3) try: Self.driver.find_element_by_android_uiautomator ('new UiSelector().textcontains (" Friend energy is finished ")') except NoSuchElementException: try: Sell.driver.find_element_by_android_uiautomator ('new UiSelector().textcontains (" Return to my forest ")') except NoSuchElementException: Pass else: print(' All friend energy collected... Self.search_energy () else: print(self.search_energy() else: print(self.search_energy() else: print(self.search_energy()) ')Copy the code
Click the “Find energy” function to automatically locate the friend interface with stolen energy. If there is a “friends are running out of energy” or “go back to my forest” in the interface, end the search, otherwise start charging friends energy.
Charging friends
def collect_energy(self): name = '' try: name = self.driver.find_element_by_id('com.alipay.mobile.nebula:id/h5_tv_title').text except NoSuchElementException: Pass print(' start charging %s... Sz = self.getsize (); Start_x = 110 end_x = 940 start_y = 460 end_y = 880 for I in range(start_y, end_y, 80): for j in range(start_x,end_x, 80): try: Self.driver.find_element_by_android_uiautomator ('new UiSelector().textcontains (" off ")').click() sleep(1) except NoSuchElementException: Pass tap_x1 = int((int(j)/width) * width) tap_y1 = int((int(I)/height) * height) # Tap_y1), (tap_x1 tap_y1)], 1000) print (' end the energy charge % s... ' % name)Copy the code
First gets the current page “com. Alipay. Mobile. The nebula: id/h5_tv_title” elements, on behalf of his best friend’s name; Since ant Forest is no longer able to locate the energy sphere elements, it is necessary to simulate clicks in the area where the energy sphere is likely to appear with a certain coordinate step size to steal energy. In the above scheme, the region where the energy sphere may appear is [(110,460), (940,880)]. This coordinate can be modified according to the actual model, and the rectangular region can be determined by Appium coordinate positioning, as follows.
Another point to note is that when you click it, you may see the display of decorative trees and pendants, as shown in the picture below. Look for the “close” element in the interface, and click to close the event.
⑥ Project source code case sharing
If you use to get words can directly take away, in my QQ technology communication group group number: 948351247 (pure technical communication and resource sharing, advertising does not enter) to self-help take away
Click here to claim