使用方法基本說明:http://www.javashuo.com/article/p-vveoiusj-gy.html,https://blog.csdn.net/Eugene_3972/article/details/76629066html
環境準備:http://www.javashuo.com/article/p-ydbmgonw-cb.htmljava
簡單實例python
1.自動化安裝與卸載 :https://my.oschina.net/yangyanxing/blog/498403android
2.自動化登陸測試 :http://www.cnblogs.com/leizivshell
注:安卓6.0以上的手機不會自動安裝app-uiautomator.apk和app-uiautomator-test.apk,須要手動安裝,不然報錯ioerror RPC server not startedapp
uiautomator +python+HTMLTestRunner 安卓UI自動化實現測試
#coding:utf-8 from uiautomator import Device import os import unittest from HTMLTestRunner import HTMLTestRunner import time d=Device('127.0.0.1:62001') print d.info class TestEle(unittest.TestCase): def setUp(self): cmd = 'adb shell am start me.ele/me.ele.application.ui.home.HomeActivity' os.system(cmd) cmd = 'adb shell am start me.ele/me.ele.shopping.ui.search.SearchActivity' os.system(cmd) def tearDown(self): cmd='adb shell am force-stop me.ele' os.system(cmd) def testSearch(self): d(resourceId='me.ele:id/editor').set_text('coco') d(resourceId='me.ele:id/search').click() d.screenshot(r"E:\code\auto\ele_search.png") self.assertTrue(d(text="coco").exists) if __name__ == '__main__': suite = unittest.TestSuite() suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestEle)) now = time.strftime('%Y-%m-%d', time.localtime(time.time())) test_dir = r'E:\code\auto' filename = test_dir + '/' + now + 'test_result.html' fp = file(filename,'wb') runner = HTMLTestRunner(stream=fp, title='ele demo by uiautomator', description= u'測試結果') runner.run(suite)
注:ui
1.經過adb devices獲取設備名,127.0.0.1:62001是夜神模擬器的設備名spa
2.經過adb shell dumpsys window | findstr mCurrentFocus命令獲取apk應用名和.MainActivity,如me.ele/me.ele.application.ui.home.HomeActivity.net
3.經過'adb shell am start me.ele/me.ele.application.ui.home.HomeActivity' 啓動模擬器上app的activity,使用真機沒法啓動,多是app應用不容許外部訪問
C:\Users\admin>adb shell am start me.ele/me.ele.application.ui.home.HomeActivity
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=me.ele/.application.ui.home.HomeActivity }
java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER]
flg=0x10000000 cmp=me.ele/.application.ui.home.HomeActivity } from null (pid=21101, uid=2000) not exported from uid 10046
at android.os.Parcel.readException(Parcel.java:1468)
at android.os.Parcel.readException(Parcel.java:1422)
at android.app.ActivityManagerProxy.startActivityAsUser(ActivityManagerNative.java:2150)
at com.android.commands.am.Am.runStart(Am.java:694)
at com.android.commands.am.Am.onRun(Am.java:272)
at com.android.internal.os.BaseCommand.run(BaseCommand.java:47)
at com.android.commands.am.Am.main(Am.java:78)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:243)
at dalvik.system.NativeStart.main(Native Method)
4.經過uiautomatorviewer獲取resourceId
運行結果:d(text="coco").exists包含了搜索欄的'coco',故而斷言成功