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