Skip to content

Commit 6ba4be9

Browse files
chenqichenqi
authored andcommitted
Add MonkeyRunner Script
1 parent 33a5e56 commit 6ba4be9

File tree

3 files changed

+159
-1
lines changed

3 files changed

+159
-1
lines changed

MonkeyRunner/README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
### uiparser (MonkeyRunner is DEAD!)
2+
3+
Setup
4+
> export ANDROID_HOME="/Users/chenqi/Library/Android/sdk"
5+
6+
Run
7+
> $ANDROID_HOME/tools/bin/monkeyrunner uiparser.py
8+
9+
### MonkeyRunner
10+
https://developer.android.google.cn/studio/test/monkeyrunner
11+
12+
#### MonkeyDevce.shell()
13+
>object shell (string cmd)
14+
Executes an `adb` shell command and returns the result, if any.
15+
16+
等同于调用`adb shell`命令。
17+
https://developer.android.google.cn/studio/command-line/adb#shellcommands
18+
19+
#### MonkeyDevice.getProperty()
20+
> object getProperty (string key)
21+
Given the name of a system environment variable, returns its value for this device.
22+
23+
获取设备系统环境变量。
24+
等同于调用`adb shell getprop <keyword>`
25+
不同厂商的设备,key可能不同。
26+
使用`adb shell getprop`,显示所有系统环境变量的key字符串
27+
28+
### adb
29+
https://developer.android.google.cn/studio/command-line/adb
30+
31+
#### adb shell am
32+
https://developer.android.google.cn/studio/command-line/adb#am
33+
使用 Activity Manager (am) 工具发出命令以执行各种系统操作,如启动 Activity、强行停止进程、广播 intent、修改设备屏幕属性及其他操作。
34+
35+
#### adb shell pm
36+
https://developer.android.google.cn/studio/command-line/adb#pm
37+
使用软件包管理器 Package Manager (pm) 工具发出命令,安装,卸载,查询安装包。
38+
39+
#### adb shell uiautomator
40+
获取当前界面的层级结构XML信息。
41+
```
42+
adb shell uiautomator dump /sdcard/uiparser/ui.xml
43+
adb pull /sdcard/uiparser/ui.xml ./ui.xml
44+
```
45+
Usage:
46+
```
47+
Usage: uiautomator <subcommand> [options]
48+
49+
Available subcommands:
50+
51+
help: displays help message
52+
53+
runtest: executes UI automation tests
54+
runtest <class spec> [options]
55+
<class spec>: <JARS> < -c <CLASSES> | -e class <CLASSES> >
56+
<JARS>: a list of jar files containing test classes and dependencies. If
57+
the path is relative, it's assumed to be under /data/local/tmp. Use
58+
absolute path if the file is elsewhere. Multiple files can be
59+
specified, separated by space.
60+
<CLASSES>: a list of test class names to run, separated by comma. To
61+
a single method, use TestClass#testMethod format. The -e or -c option
62+
may be repeated. This option is not required and if not provided then
63+
all the tests in provided jars will be run automatically.
64+
options:
65+
--nohup: trap SIG_HUP, so test won't terminate even if parent process
66+
is terminated, e.g. USB is disconnected.
67+
-e debug [true|false]: wait for debugger to connect before starting.
68+
-e runner [CLASS]: use specified test runner class instead. If
69+
unspecified, framework default runner will be used.
70+
-e <NAME> <VALUE>: other name-value pairs to be passed to test classes.
71+
May be repeated.
72+
-e outputFormat simple | -s: enabled less verbose JUnit style output.
73+
74+
dump: creates an XML dump of current UI hierarchy
75+
dump [--verbose][file]
76+
[--compressed]: dumps compressed layout information.
77+
[file]: the location where the dumped XML should be stored, default is
78+
/sdcard/window_dump.xml
79+
80+
events: prints out accessibility events until terminated
81+
```

MonkeyRunner/uiparser.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#! $ANDROID_HOME/tools/bin monkeyrunner
2+
# -*- coding: utf-8 -*-
3+
'''uiparser'''
4+
5+
import os
6+
import sys
7+
import subprocess
8+
import datetime
9+
import logging
10+
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage #pylint: disable=import-error
11+
12+
class NullHandler(logging.Handler):
13+
def emit(self, record):
14+
pass
15+
16+
logging.getLogger(__name__).addHandler(NullHandler())
17+
logging.basicConfig(level=logging.DEBUG)
18+
19+
SHORT = 1
20+
MIDDLE = 5
21+
LONG = 15
22+
23+
ADB = os.path.join(os.environ['ANDROID_HOME'], 'platform-tools', 'adb')
24+
25+
# Example of Ctrip Android Apk
26+
TARGET_PACKAGE = 'ctrip.android.view'
27+
LAUNCH_ACTIVITY = 'ctrip.business.splash.CtripSplashActivity'
28+
HOME_ACTIVITY = 'ctrip.android.publicproduct.home.view.CtripHomeActivity'
29+
FLIGHT_ACTIVITY = 'ctrip.android.flight.view.inland.FlightInquireActivity'
30+
START_COMPONENT = TARGET_PACKAGE + '/' + LAUNCH_ACTIVITY
31+
32+
DEVICE_DIR = '/sdcard/uiparser/'
33+
HOST_DIR = './'
34+
35+
36+
def capture(device, index):
37+
''''''
38+
_dumpXML = DEVICE_DIR + index + '.xml'
39+
_localXML = HOST_DIR + index + '.xml'
40+
_localImage = HOST_DIR + index + '.png'
41+
42+
_shell = [ADB, 'shell', 'uiautomator', 'dump', _dumpXML]
43+
logging.debug(datetime.datetime.now())
44+
subprocess.call(_shell) # Stupid uiautomator, always failed here!
45+
logging.debug(datetime.datetime.now())
46+
#MonkeyRunner.sleep(MIDDLE)
47+
48+
_shell = [ADB, 'pull', _dumpXML, _localXML]
49+
subprocess.call(_shell)
50+
51+
_image = device.takeSnapshot()
52+
_image.writeToFile(_localImage, 'png')
53+
54+
55+
def uiparser():
56+
'''Main Entry'''
57+
device = MonkeyRunner.waitForConnection(MIDDLE)
58+
59+
_shell = [ADB, 'shell', 'rm', '-rf', DEVICE_DIR]
60+
subprocess.call(_shell)
61+
62+
_shell = [ADB, 'shell', 'mkdir', '-p', DEVICE_DIR]
63+
subprocess.call(_shell)
64+
65+
device.startActivity(component=START_COMPONENT)
66+
MonkeyRunner.sleep(MIDDLE)
67+
68+
capture(device, str(0))
69+
70+
71+
if __name__ == "__main__":
72+
# MonkeyRunner Jython version is 2.5.3 (Stupid!)
73+
logging.info(sys.version)
74+
uiparser()

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
## My Python Script
1+
# My Python Script
2+
23
1. [auc_pr_roc](/auc_pr_roc)
34
Python scikit-learn计算PR ROC曲线AUC值。
45
用于 `携程旅行网 云海竞赛平台` [携程机票航班延误预测算法大赛](https://yunhai.ctrip.com/Games/11),竞赛算法结果核算。
@@ -22,3 +23,5 @@ Python leveldb Utils 常用方法封装。
2223
Python爬虫 - [全国组织结构代码管理中心](http://www.nacao.org.cn)V1.0。
2324
11. [nacao_v2](/nacao_v2)
2425
Python爬虫 - [全国组织结构代码管理中心](http://www.nacao.org.cn)V2.0。
26+
12. [MonkeyRunner](/MonkeyRunner)
27+
MonkeyRunner is STUPID!

0 commit comments

Comments
 (0)