標籤:
分類: adb2014-06-22 01:24 837人閱讀 評論(0) 收藏 舉報
androidadb測試monkeyrunnerpython
在android測試中,經常需要知道啟動一個Activity所需要的component,例如在monkeyrunner中啟動一個系統設定:startActivity(component="com.android.settings/com.android.settings.Settings"),那如何擷取該component 呢?
有如下方法:
1.在有root 許可權並且開啟了view server 的前提下,使用sdk/tools目錄下hierarchyviewer.bat 工具可以獲得
2.在sdk/build-tools目錄下有個aapt工具,使用aapt dump badging *.apk可以獲得
3.在CMD視窗中執行adb logcat -v time -s ActivityManager,然後點擊應用進入,如點擊系統設定,進入後會有相應的日誌資訊列印出來,在資訊中尋找 cmp=com.android.settings/.Settings
4.通過adb shell dumpsys命令獲得,這也是我準備主要介紹的方法
在CMD視窗中執行adb shell dumpsys window -h,會顯示下面的協助內容:
C:\Users\xuxu>adb shell dumpsys window -h
Window managerdump options:
[-a] [-h] [cmd]...
cmd may be oneof:
l[astanr]: lastANR information
p[policy]:policy state
a[animator]:animator state
s[essions]:active sessions
d[isplays]:active display contents
t[okens]:token list
w[indows]:window list
cmd may also bea NAME to dump windows. NAME may
be a partialsubstring in a window name, a
Window hexobject identifier, or
"all"for all windows, or
"visible"for the visible windows.
-a:include all available server state.
我們使用windows選項,執行adb shell dumpsys window w,在輸出結果中我們可以找到開啟的當前應用的component,而component中總是含有斜杠“/”,所以我們可以使用這個命令得到輸出(進入系統設定應用),adb shell dumpsyswindow w | findstr \/ ,需要轉義斜杠“/”,在linux下需要把findstr換成grep,此時輸出的內容還是會比較多,不容易尋找,再結果分析,發現可以再尋找字串“name=”,
接下來重新執行adb shell dumpsyswindow w | findstr \/ | findstr name= ,會輸出下面的結果:
C:\Users\xuxu>adb shell dumpsyswindow w | findstr \/ | findstr name=
mSurface=Surface(name=com.android.settings/com.android.settings.Settings)
com.android.settings/com.android.settings.Settings 就是我們需要的component
接下來用python語句來擷取該component:
[python] view plaincopy
- import os
- import re
-
- def getFocusedPackageAndActivity():
-
- pattern = re.compile(r"[a-zA-Z0-9\.]+/[a-zA-Z0-9\.]+")
- out = os.popen("adb shell dumpsys window windows | findstr \/ | findstr name=").read()
- list = pattern.findall(out)
- component = list[0]
- 10.
- 11. return component
12. print getFocusedPackageAndActivity()
列印結果:com.android.settings/com.android.settings.Settings
如此就可以在使用monkeyrunner中的startActivity方法時調用該方法將擷取到的component傳入參數了!
附出處:http://blog.csdn.net/gb112211/article/details/33073191
通過adb shell dumpsys命令擷取當前應用的component