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相似,就是環境上有點不同而已