1.首先来介绍下UIAutomator工具html
UIAutomator是Android官方推出的安卓应用界面自动化测试工具,是最理想的针对APK进行自动化功能回归测试的利器。python
2.UIAutomator测试环境搭建android
2.1 在pypi.Python.org网站下载uiautomator压缩包,解压后python setup.py install安装;或者直接pip install uiautomatorios
2.2 对于uiautomator工具而言,adb是必不可少的,adb提供的adb shell可实现android的远程操做,安装好adb,adb device可查看USB链接的手机设备,安装adb建议直接安装91手机助手,91手机助手会自动帮你安装adb, git
2.3 介绍UIAutomator测试框架的UI工具:uiautomatorviewer 以下图: github
uiautomatorviewer位于sdk/tools目录下,能够扫描、分析待测试应用界面,分析结果能够导出为xml与截图。经过该工具能够分析出UI控件的id,text,focusable等等各类属性,甚至布局上的层次关系。
能够经过./uiautomatorviewer启动该工具。shell
3.UIAutomator工具的使用框架
先上一个小Demo代码:dom
# -*- 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("8888") d(resourceId="com.isentech.attendance:id/regis_pass").set_text("123456") d(resourceId="com.isentech.attendance:id/regis_passAgain").set_text("123456") d(text="注册").click() time.sleep(2) if d(text="马上去登陆").exists: d(text="马上去登陆").click() d(resourceId="com.isentech.attendance:id/txtLoginPassword").set_text("123456") 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("123456") d(text="登陆").click() d(text="请输入您的姓名").set_text("123456") 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(['139', '188', '185', '136', '158', '151'])+"".join(random.choice("0123456789") 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相似,就是环境上有点不同而已