Category: Adb2014-06-22 01:24 837 people read reviews (0) Favorite reports
ANDROIDADB Test Monkeyrunnerpython
In Android testing, it is often necessary to know the component required to start an activity, such as starting a system setting in Monkeyrunner: StartActivity (component= " Com.android.settings/com.android.settings.settings "), how to get the component?
There are the following methods:
1. With root access and view server enabled, use the Hierarchyviewer.bat tool in the Sdk/tools directory to get
2. In the Sdk/build-tools directory there is a AAPT tool that uses AAPT dump badging *.apk to obtain
3. Execute the ADB logcat-v time-s activitymanager in the cmd window, then click on the application to enter, such as click System Settings, enter the corresponding log information will be printed out, in the information to find cmp=com.android.settings/. Settings
4. Obtained through the ADB Shell Dumpsys Command, which is the approach I'm going to focus on.
Executing adb shell Dumpsys window-h in the cmd window displays the following Help content:
C:\USERS\XUXU>ADB Shell Dumpsys window-h
Window managerdump Options:
[-A] [-h] [cmd] ...
CMD May oneof:
L[ASTANR]: LASTANR information
P[policy]:p olicy 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.
We use the Windows option to execute the ADB shell dumpsys window W, and in the output we can find the component of the current application that is open, and the component always contains a slash "/", So we can use this command to get the output (into the System setup application), adb shell Dumpsyswindow W | FINDSTR/, need to escape the slash "/", in Linux need to change findstr to grep, at this time the output of the content will be more, not easy to find, and then the results of analysis, found to find the string "Name=",
Next re-executing adb shell Dumpsyswindow W | FINDSTR \ | Findstr Name=, the following results are output:
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 is the component we need.
Next, use the Python statement to get the 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]
-
- one. return component
print getfocusedpackageandactivity ()
Print Result: com.android.settings/com.android.settings.settings
So you can call the component passed in parameter when you use the StartActivity method in Monkeyrunner!
Attached Source: http://blog.csdn.net/gb112211/article/details/33073191
Get the current app's component via the ADB Shell Dumpsys command