forked from Vieira-zj/zj_uiautomator_project
/
UiTestRunner.py
216 lines (171 loc) · 6.3 KB
/
UiTestRunner.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# -*- coding: utf-8 -*-
'''
Created on 2016-6-3
@author: zhengjin
'''
import os
import sys
# --------------------------------------------------------------
# Vars
# --------------------------------------------------------------
g_prj_root_path = os.getcwd()
# g_prj_root_path = r'E:\AndroidStudio_Workspace\FunSettingsUITest'
g_prj_output_path = os.path.join(g_prj_root_path, r'app\build\outputs\apk')
g_prj_output_debug_apk_name = 'app-debug.apk'
g_prj_output_debug_apk_path = os.path.join(g_prj_output_path, g_prj_output_debug_apk_name)
g_prj_output_release_apk_name = 'app-release-unsigned.apk'
g_prj_output_release_apk_path = os.path.join(g_prj_output_path, g_prj_output_release_apk_name)
g_prj_output_android_instrument_test_name = 'app-debug-androidTest-unaligned.apk'
g_prj_output_android_instrument_test_path = \
os.path.join(g_prj_output_path, g_prj_output_android_instrument_test_name)
g_shell_data_tmp_path = r'/data/local/tmp'
g_package_name = 'com.example.zhengjin.funsettingsuitest'
g_instrument_test_name = '%s.test' %(g_package_name)
g_push_apk_file_path = '%s/%s' %(g_shell_data_tmp_path, g_package_name)
g_push_instrument_test_file_path = '%s/%s' %(g_shell_data_tmp_path, g_instrument_test_name)
g_instrument_test_runner = \
'%s/android.support.test.runner.AndroidJUnitRunner' %(g_instrument_test_name)
# vars to be update
g_test_cases = '%s.testcasedemos.TestTaskLauncher' %(g_package_name)
g_flag_build = False;
g_flag_assemble_debug_apk = False;
g_flag_assemble_release_apk = False;
g_flag_run = True;
# --------------------------------------------------------------
# Functions, build app and instrument tests.
# --------------------------------------------------------------
def build_testing_prj():
run_prj_clean()
run_prj_assemble_android_test()
if g_flag_assemble_debug_apk:
run_prj_assemble_debug()
if g_flag_assemble_release_apk:
run_prj_assemble_release()
verify_build_output_files_exist()
def run_win_command(cmd):
print 'Run cmd:: %s' %(cmd)
ret = os.system(cmd)
if ret == 0:
return
else:
print 'Error, exec command %s' %(cmd)
sys.exit(1)
def run_prj_clean():
cmd = 'gradlew clean'
run_win_command(cmd)
def run_prj_assemble_android_test():
cmd = 'gradlew assembleAndroidTest'
run_win_command(cmd)
def run_prj_assemble_release():
cmd = 'gradlew assembleRelease'
run_win_command(cmd)
def run_prj_assemble_debug():
cmd = 'gradlew assembleDebug'
run_win_command(cmd)
def verify_file_exist(path):
if os.path.exists(path):
print 'verify file %s exist. ' %(path)
return
else:
print 'The file %s is NOT exist.' %(path)
sys.exit(1)
def verify_build_output_files_exist():
verify_file_exist(g_prj_output_debug_apk_path)
verify_file_exist(g_prj_output_android_instrument_test_path)
# --------------------------------------------------------------
# Functions, execute the UI test cases.
# --------------------------------------------------------------
def run_win_command_with_output(cmd):
print 'Run cmd:: %s' %(cmd)
output = os.popen(cmd)
return output.readlines()
def verify_adb_connection():
cmd = 'adb get-serialno'
lines = run_win_command_with_output(cmd)
if (len(lines) == 1):
if (lines[0] == 'unknown\n'):
print 'There is no adb connection.'
sys.exit(1)
else:
print 'The adb connection: %s' %(lines[0])
return
elif (len(lines) > 1):
print 'There are more than one adb connection.'
sys.exit(1)
else:
print 'unknown error, checking the adb connection.'
sys.exit(1)
def push_and_install_debug_apk_file():
push_cmd = 'adb push'
cmd = '%s %s %s' %(push_cmd, g_prj_output_debug_apk_path, g_push_apk_file_path)
run_win_command(cmd)
install_cmd = 'adb shell pm install -r'
cmd = '%s %s' %(install_cmd, g_push_apk_file_path)
run_win_command(cmd)
def push_and_install_testing_instrument_file():
push_cmd = 'adb push'
cmd = '%s %s %s' %(
push_cmd, g_prj_output_android_instrument_test_path, g_push_instrument_test_file_path)
run_win_command(cmd)
install_cmd = 'adb shell pm install -r'
cmd = '%s %s' %(install_cmd, g_push_instrument_test_file_path)
run_win_command(cmd)
def run_instrument_test():
instrument_cmd = 'adb shell am instrument -w -r -e debug false -e class'
test_cases = g_test_cases
instrument_test_runner = g_instrument_test_runner
cmd = '%s %s %s' %(instrument_cmd, test_cases, instrument_test_runner)
return run_win_command_with_output(cmd)
def push_and_run_instrument_test():
push_and_install_debug_apk_file()
push_and_install_testing_instrument_file()
run_instrument_test()
# --------------------------------------------------------------
# Test report
# --------------------------------------------------------------
def write_test_output_to_file():
print 'TODO'
def filter_and_format_test_output(lines):
test_case = ''
test_class = ''
test_cases = []
flag_num = True
test_number = ''
flag_time = True
test_time = ''
for line in lines:
if 'test=' in line:
test_case = line.split(' ')[1].strip('\n')
continue
if 'class=' in line:
test_class = line.split(' ')[1].strip('\n')
test_cases.append('%s::%s' %(test_class, test_case))
continue
if flag_num and ('numtests=' in line):
test_number = line.split(' ')[1].strip('\n')
flag_num = False
continue
if flag_time and ('Time:' in line):
test_time = line.split(' ')[1].strip('\n')
flag_time = False
print 'Test number: %s' %(test_number)
print 'Total test time: %s' %(test_time)
for case in set(test_cases):
print 'Test case: %s' %(case)
# return test_number, test_time, set(test_cases)
def generate_xml_test_report():
print 'TODO'
def generate_html_test_report():
print 'TODO'
# --------------------------------------------------------------
# Main
# --------------------------------------------------------------
def test_main():
if g_flag_build:
build_testing_prj()
if g_flag_run:
push_and_run_instrument_test()
if __name__ == '__main__':
test_main()
print "***** %s done." %(__file__)
pass