1.首先来介绍下UIAutomator工具

UIAutomator是Android官方推出的安卓应用界面自动化测试工具,是最理想的针对APK进行自动化功能回归测试的利器。

2.UIAutomator测试环境搭建

2.1 在pypi.Python.org网站下载uiautomator压缩包,解压后python setup.py install安装;或者直接pip install uiautomator

2.2 对于uiautomator工具而言,adb是必不可少的,adb提供的adb shell可实现android的远程操作,安装好adb,adb device可查看USB连接的手机设备,安装adb建议直接安装91手机助手,91手机助手会自动帮你安装adb,

2.3 介绍UIAutomator测试框架的UI工具:uiautomatorviewer 如下图: 

uiautomatorviewer位于sdk/tools目录下,可以扫描、分析待测试应用界面,分析结果可以导出为xml与截图。通过该工具可以分析出UI控件的id,text,focusable等等各种属性,甚至布局上的层次关系。 
可以通过./uiautomatorviewer启动该工具。

3.UIAutomator工具的使用

先上一个小Demo代码:

# -*- coding:utf-8 -*-
from uiautomator import device as d
import time
import sys
import random
import unittest
import HTMLTestRunner reload(sys)
sys.setdefaultencoding("utf-8") class My_Test_Suite(unittest.TestCase):
def setUp(self):
try:
d.press.home()
d(text="***").click()
time.sleep(2)
if d(text="我的").exists:
d(text="我的").click()
d(text="注销").click()
d(text="确定").click()
if d(text="登录").exists:
d(resourceId="com.isentech.attendance:id/title_back").click()
else:
time.sleep(3)
print u"开启APP"
except Exception, e:
print u"Error: 开启APP失败\n", e # 测试注册
def test_reg(self):
try:
d(text="注册").click()
# 测试已注册手机号
d(text="请输入手机号码").set_text("1313384****")
d(text="获取验证码").click()
# 测试注册
d(text="请输入手机号码").set_text(phone_number)
d(text="请输入验证码").set_text("")
d(resourceId="com.isentech.attendance:id/regis_pass").set_text("")
d(resourceId="com.isentech.attendance:id/regis_passAgain").set_text("")
d(text="注册").click()
time.sleep(2)
if d(text="立刻去登录").exists:
d(text="立刻去登录").click()
d(resourceId="com.isentech.attendance:id/txtLoginPassword").set_text("")
d(text="登录").click()
except Exception, e:
print u"Error: 注册失败\n", e # 测试登陆
def test_login(self, phone):
try:
d(text="登录").click()
d(resourceId="com.isentech.attendance:id/txtLoginUserName").clear_text()
d(resourceId="com.isentech.attendance:id/txtLoginUserName").set_text(phone)
d(resourceId="com.isentech.attendance:id/txtLoginPassword").set_text("")
d(text="登录").click()
d(text="请输入您的姓名").set_text("")
d(text="完成").click()
time.sleep(2)
if d(text="签到").exists:
print u"登录成功"
except Exception, e:
print u"Error: 登录失败\n", e # 测试忘记密码
def test_forget_password(self):
try:
pass # 一些测试步骤
except Exception, e:
print u"Error: 重置密码or修改密码失败\n", e #......更多的测试模块用例 def tearDown(self):
try:
d.press.home()
d.press.recent()
time.sleep(3)
d.swipe(200, 500, 200, 0, steps=10)
d.press.home()
print u"关闭APP"
except Exception, e:
print u"Error: 关闭APP失败\n", e if __name__ == "__main__":
phone_number = random.choice(['', '', '', '', '', ''])+"".join(random.choice("") for i in range(8))
test_unit = unittest.TestSuite()
test_unit.addTest(My_Test_Suite("test_reg"))
filename = './Result_auto_android.html'
fp = file(filename, 'wb')
runner = HTMLTestRunner.HTMLTestRunner(stream=fp,title=u"测试报告",description=u"测试结果详情:")
runner.run(test_unit)
 

以上代码是一个APP的整体测试框架,有些代码隐藏掉了,对代码的理解我希望大家去查看这个文章:https://github.com/Xuyangting/uiautomator


为什么要Android自动化呢?!每次回归测试,就像打地鼠一样,打下去一个又冒出来另一个,真的很心痛,测试人员太苦逼了,每天拿着手机点呀点,所以才选择用uiautomator工具进行协助测试,但这个工具也有很多不好的地方,很多测试场景很难模拟出来,还得人工去进行手工测试。写完这个Android得自动化,接下来要写iOS自动化测试工具-Appium,这个工具和uiautomator类似,就是环境上有点不一样而已

最新文章

  1. Android Studio 使用Lambda
  2. 无法打开键: UNKNOWN\Components\BE1FB738077DBE490AF18C3B9B1A1EE8\E5F5286B58B542741A00A0A9AA420B27
  3. php错误收集
  4. openerp 经典收藏 workflow中的‘非典型’自动触发器trigger_model(转载)
  5. java基础知识回顾之javaIO类--管道流PipedOutputStream和PipedIutputStream
  6. untiy 插件工具: 游戏中 策划数据Excel 导出到项目中
  7. 在Windows下编译ffmpeg完全手册
  8. bzoj 2761 [JLOI2011]不重复数字(哈希表)
  9. lua IDE for cocos2d-x development
  10. 【编程之美】java二进制实现重建
  11. uci随笔
  12. 大数据查询——HBase读写设计与实践
  13. linux 逆向映射
  14. eclipse_maven换源
  15. GraphQL: Object doesn't support property or method 'from'
  16. 大数据处理框架之Strom:kafka storm 整合
  17. python 中的比较==和is
  18. Easyui + asp.net MVC 系列教程 第09-17 节 完成登录 高清录制
  19. 逆向工程生成的Mapper.xml以及*Example.java详解
  20. PyQt 5 的学习引言

热门文章

  1. php后端程序开发学习网站梳理
  2. Android setVisibility(View.GONE)无效的问题及原因分析
  3. 自定义可伸缩的imageView
  4. Vivado增量式编译
  5. github入门一
  6. codevs 1487 大批整数排序(水题日常)
  7. codevs 3344 迷宫
  8. IOS修改系统音量
  9. Python基础篇 -- 小数据池和再谈编码
  10. jwt 登录