每天都有人偷我森林的能量,我写了一个蚂蚁森林自动化收取能量!看你们咋偷!

1,126 阅读5分钟

前言

     很多小伙伴应该和小编一样,没啥事了,就会收一收蚂蚁森林的能量,就好像回到了当年种菜,农场,牧场这些一样,但是总有刁民惦记着我得能量,当年就是偷我的菜,虽然我也没少偷!哈哈哈!好了接下来教你们怎么实现能量自动收取!

必备环境

Window10系统

Appium1.21.0

AndroidSDK r24.1.1

Python3.7.5

支付宝apk文件

查看支付宝apk包信息

使用android sdk aapt命令查看支付宝apk包信息,后面会用到,如下。

检测设备是否连接

一开始想用模拟器(如夜神模拟器)进行自动化,后来发现支付宝在模拟器里运行特别卡,最终决定使用真机了。这里使用android sdk的adb工具检测手机设备是否连接正常,如下。如果看不到连接信息或者显示unauthorized的,请开启手机的USB调试权限,也有可能开启开发者模式呦。

准备知识

pip install 默认安装路径修改

Appium工作原理

Appium使用Python运行appium测试的实例

安装自动化工具包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

安装包的时候加上参数--user 包就会自动安装到自定义路径下面

启动Appium服务端

开启新会话定位画面元素

{
  "deviceName": "Alipay-Test",
  "platformName": "Android",
  "platformVersion": "10",
  "appActivity": "com.eg.android.AlipayGphone.AlipayLogin",
  "appPackage": "com.eg.android.AlipayGphone",
  "noReset": true,
  "fullReset": false
}

编写python脚本

知道如何定位支付宝界面的元素后,开始编写python自动化运行脚本。大体分为如下几个步骤。

初始化客户端

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)

客户端初始化后会自动启动支付宝APP,注意noReset和fullReset参数的设置

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.

解锁手机

def unlocked(self):
    sz = self.getSize();
    x1 = int(sz[0] * 0.10)      #x坐标
    y1 = int(sz[1] * 0.95)      #起始y坐标
    y2 = int(sz[1] * 0.15)      #终点y坐标
    self.driver.swipe(x1, y1, x1, y2, 1000)
    sleep(1)
    try:
        self.driver.find_element_by_id('com.android.systemui:id/vivo_pin_keyboard')
        for k in [5,1,2,9,9,9]:
            self.driver.find_element_by_id('com.android.systemui:id/VivoPinkey%d' % k).click()
        print('手机解锁成功...')
    except NoSuchElementException:
        print('手机已解锁或解锁失败')

进入蚂蚁森林

def entry_ant_forest(self):
  try:
      sleep(2)
      # 点击蚂蚁森林icon
      self.driver.find_element_by_android_uiautomator('new UiSelector().text("蚂蚁森林")').click()
  except NoSuchElementException:
      # 异常回到首页重试
      self.driver.back()
      sleep(2)
      # 点击支付宝icon
      self.driver.find_element_by_android_uiautomator('new UiSelector().text("支付宝")').click()
      sleep(2)
      # 点击蚂蚁森林icon
      self.driver.find_element_by_android_uiautomator('new UiSelector().text("蚂蚁森林")').click()

按理说进入蚂蚁森林直接模拟点击“蚂蚁森林”icon就可以了,但是偶尔会抛出 NoSuchElementException 异常。也就是Appium在切换activity后导致元素无法定位,如果手机不锁屏不会发生这种情况(可以在开发者模式中指定),锁屏解锁后切换到支付宝的activity后偶尔会出现这种情况。没有找到太好的解决方法,发生异常时使手机界面返回到首页,然后点击支付宝重新进入,最后点击蚂蚁森林进入。

搜索能量

def search_energy(self):
    # 点击找能量
    self.driver.tap([(1000, 1520), (1080, 1580)], 1000)
    sleep(3)
    try:
        self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("好友能量都收完了")')
    except NoSuchElementException:
        try:
            self.driver.find_element_by_android_uiautomator('new UiSelector().textContains("返回我的森林")')
        except NoSuchElementException:
            pass
        else:
            print('全部好友能量收取完毕...')
            return
            
        # 收取好友能量
        self.collect_energy()
        # 继续找能量
        self.search_energy()
    else:
        print('全部好友能量收取完毕...')

点击“找能量”功能自动定位到有能量偷取的好友界面。如果界面中有“好友能量都收完了”或者“返回我的森林”相关字样,结束查找,否则开始收取好友能量。

收取好友能量

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('开始收取%s的能量...' % name)
    
    # 获取手机屏幕宽高
    sz = self.getSize();
    width = sz[0]
    height = sz[1]
    
    # 能量球可能出现的区域坐标
    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("关闭")').click()
                sleep(1)
            except NoSuchElementException:
                pass
            tap_x1 = int((int(j) / width) * width)
            tap_y1 = int((int(i) / height) * height)
            # 点击指定坐标
            self.driver.tap([(tap_x1, tap_y1), (tap_x1, tap_y1)], 1000)
            
    print('结束收取%s的能量...' % name)

首先获取当前页面的“ com.alipay.mobile.nebula:id/h5_tv_title ”元素,代表好友的名字;因为蚂蚁森林现在不能定位能量球元素了,所以要在能量球可能出现的方块区域按照一定的坐标步长模拟点击进行能量偷取。上面的方案中,规定的能量球可能出现的区域为[(110,460),(940,880)],这个坐标可以根据实际的机型进行修改,可以通过Appium坐标定位判断出矩形区域,如下。

​还要一个点需要注意的,点击的时候可能会出现装饰树和挂件的展示,如下图所示。这时候需要在界面中查找“ 关闭 ”元素,然后click事件关闭就行了。

⑥项目源码案例分享有

如果你用得到的话可以直接拿走,在我的QQ技术交流群里群号:948351247(纯技术交流和资源共享,广告勿入)以自助拿走

点击这里 领取

​​

​​​​​