【轉】Python + Android + Uiautomator自動化測試

來源:互聯網
上載者:User

標籤:裝置   print   https   工具   布局   登入失敗   搭建   from   手工   

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 dimport timeimport sysimport randomimport unittestimport HTMLTestRunnerreload(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", eif __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類似,就是環境上有點不一樣而已

【轉】Python + Android + Uiautomator自動化測試

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.