准备环境

  Window10系统

  Appium1.21.0

  AndroidSDK r24.1.1

  Python3.7.5

  支付宝apk文件

查看支付宝apk包信息

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

Android Asset Packaging Tool

Usage:
aapt l[ist] [-v] [-a] file.{zip,jar,apk}
List contents of Zip-compatible archive. aapt d[ump] [--values] [--include-meta-data] WHAT file.{apk} [asset [asset ...]]
strings Print the contents of the resource table string pool in the APK.
badging Print the label and icon for the app declared in APK.
permissions Print the permissions from the APK.
resources Print the resource table from the APK.
configurations Print the configurations in the APK.
xmltree Print the compiled xmls in the given assets.
xmlstrings Print the strings of the given compiled xml assets. D:\android-sdk-windows\build-tools\29.0.0> .\aapt.exe dump badging C:\Users\XXX\Downloads\alipay_wap_main.apk

输出结果如下:
package: name='com.eg.android.AlipayGphone'
versionCode='410'
versionName='10.2.26.9000'
compileSdkVersion='29'
compileSdkVersionCodename='10'
install-location:'auto'
sdkVersion:'18'
targetSdkVersion:'29'
launchable-activity: name='com.eg.android.AlipayGphone.AlipayLogin'

检测设备是否连接

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

准备知识

pip install 默认安装路径修改

Appium工作原理

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

安装自动化工具包

pip install Appium-Python-Client --user

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.

pip install pytest --user

The pytest framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries.

安装包的时候加上参数--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事件关闭就行了。

演示效果

如果效果感觉还不错的话,那就关注一下微信公众号(请扫下方二维码),回复“蚂蚁森林”获取完整代码吧。

Appium和Python实现蚂蚁森林自动化收取能量的更多相关文章

  1. 自动化测试 Appium之Python运行环境搭建 Part2

    Appium之Python运行环境搭建 Part2 by:授客 QQ:1033553122 实践环境 参见 Appium之Python运行环境搭建 Part1 环境部署 1.安装Android SDK ...

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

    Appium使用Python运行appium测试的实例 一.  Appium之介绍 https://testerhome.com/topics/8038 详情参考--https://testerhom ...

  3. 基于Appium、Python的自动化测试

    基于Appium.Python的自动化测试环境部署和实践   第一章 导言 1.1 编制目的 该文档为选用Appium作为移动设备原生(Native).混合(Hybrid).移动Web(Mobile ...

  4. 自动化测试 Appium之Python运行环境搭建 Part1

    Appium之Python运行环境搭建 Part1 by:授客 QQ:1033553122 实践环境 Win7 Python 3.4.0 JAVA JDK 1.8.0_121 node.js8.11. ...

  5. python入门以及接口自动化实践

    一.Python入门必备基础语法# 标识符:python中我们自己命名的都是标识符# 项目名 包名 模块名# 变量名 函数名 类名# 1:字母 下划线 数字组成 命名的时候不能以数字开头# 2:见名知 ...

  6. 用Python实现随机森林算法,深度学习

    用Python实现随机森林算法,深度学习 拥有高方差使得决策树(secision tress)在处理特定训练数据集时其结果显得相对脆弱.bagging(bootstrap aggregating 的缩 ...

  7. Appium(Python)测试混血App

    Hybrid App(混合模式移动应用)是指介于web-app.native-app这两者之间的app兼具Native App良好用户交互体验的优势和Web App跨平台开发的优势 HybridApp ...

  8. Appium+eclipse+python环境配置

    1.安装安卓开发环境(教程很多,不细写) 2.安装eclipse 下载eclipse,解压即可   3.安装python 下载地址:https://www.python.org/downloads/r ...

  9. Python+Django+ansible playbook自动化运维项目实战☝☝☝

    Python+Django+ansible playbook自动化运维项目实战☝☝☝  一.入门引导 DevOPSDevOps(英文Development和Operations的组合)是一组过程.方法 ...

随机推荐

  1. Redis的过期键删除策略

    文章首发于公众号:蘑菇睡不着,欢迎来看看 前言 Redis 中都是键值对的存储形式,键都是字符串类型的,而值有很多种类型,如 string.list.hash.set.sorted set等类型.当设 ...

  2. 白日梦的MySQL专题(第38篇文章)8分钟回顾MySQL的索引

    目录 公众号首发-推荐阅读原文-格式更好看 一.导读 二.聚簇索引 三.二级索引 四.联合索引 4.1.什么是联合索引 4.2.左前缀原则 4.3.联合索引的分组&排序 五.覆盖索引 六.倒排 ...

  3. spring boot使用@Async异步注解

    1.java的大部分接口的方法都是串行执行的,但是有些业务场景是不需要同步返回结果的,可以把结果直接返回,具体业务异步执行,也有些业务接口是需要并行获取数据,最后把数据聚合在统一返回给前端. 通常我们 ...

  4. 【模拟8.01】big(trie树)

    一道trie树的好题 首先我们发现后手对x的操作就是将x左移一位,溢出位在末尾补全 那么我们也可以理解为现将初值进行该操作,再将前i个元素异或和进行操作,与上等同. 那么我们等于转化了问题:     ...

  5. LCD1602液晶显示模块的单片机驱动深入详解之硬件篇

    (本文以HD44780主控芯片的LCD1602为蓝本进行描述,其中的截图也来自HD44780数据手册,用户可自行搜索其datasheet,有部分整理网上的,但绝对要比你看到的要深入得多) 一.接口 L ...

  6. moment常用方法

    1.subtract方法,时间加减处理 console.log(moment().format("YYYY-MM-DD HH:mm:ss")); //当前时间 console.lo ...

  7. Python中Random随机数返回值方式

    1.a=["1","2","3"] print(random.choice(a)),  随机返回列表a中的一个元素 print(random ...

  8. 27、路由 route

    优先于网卡中的配置,但是重启或重启网卡失效,最好加入到开机自启动服务中/etc/rc.local文件中: 27.1.添加网络路由: 目的网络.目的网络掩码,网关.网卡: 网络寻址以路由表中的路由优先: ...

  9. 7.4、Horizon部署

    1.horizon介绍: horizon只需要连上keystone即可: 这里在控制节点controller进行操作: 提示:horizon和keystone运行时使用的都是apache软件,如果都装 ...

  10. POJ 3026 Borg Maze 广搜(BFS)+最小生成树

    题意:从S出发,去抓每一个A,求总路径最短长度.在S点和A点人可以分身成2人,不过一次只能让一个人走. 思路是先利用BFS求出各点之间的距离,建成图,再套用最小生成树模板. 一次性A了.不过觉得在判断 ...