def pull_instrument_run_snapshorts(): cmd = 'adb shell rename %s %s' % (SHELL_SNAPSHOTS_DIR_PATH, SHELL_SNAPSHOTS_DIR_NEW_PATH) print cmd if not WinSysUtils.run_sys_cmd(cmd): return time.sleep(1) cmd = 'adb pull %s %s' % (SHELL_SNAPSHOTS_DIR_NEW_PATH, REPORT_DIR_PATH) print cmd WinSysUtils.run_sys_cmd(cmd)
def adb_connect_to_device(device_ip): cmd = 'adb connect %s' % device_ip try_adb_connect_times = 3 wait_time = 3 for i in range(0, try_adb_connect_times): logging.debug('try to connect to adb device, %d times.' % (i + 1)) WinSysUtils.run_sys_cmd(cmd) if verify_adb_devices_serialno(): # verify connect success return True time.sleep(wait_time) logging.error('Failed to connect to device!') return False
def copy_app_to_system_app_dir(): print 'Copying app from sdcard to /system/app/' cmd = 'adb shell cp %s%s %s' % (SHELL_TMP_DIR_PATH, target_app_name, SHELL_SYSTEM_APP_DIR_PATH) if not WinSysUtils.run_sys_cmd(cmd): print 'Failed to copy app from sdcard to /system/app/' exit(1)
def get_process_id_by_pkg_name(pkg_name): cmd = 'adb shell ps | findstr ' + pkg_name tmp_lines = mysys.run_sys_cmd_and_ret_lines(cmd) if len(tmp_lines) != 1: return '-1' return tmp_lines[0].split()[1]
def get_network_data_flow(pkg_name, is_wifi=False): ''' return: (int receive_data, int transmit_data), KB ''' def _get_filter_keyword(is_wifi): if is_wifi: return 'wlan0' return 'eth0' def _parse_net_data(net_data): return int(net_data) / 1024 tmp_pid = get_process_id_by_pkg_name(pkg_name) if tmp_pid == '-1': return INIT_NETWORK_DATA cmd = 'adb shell cat /proc/%s/net/dev' % tmp_pid tmp_lines = mysys.run_sys_cmd_and_ret_lines(cmd) if len(tmp_lines) == 0: print 'Error, return empty for command => ' + cmd exit(1) filter_keyword = _get_filter_keyword(is_wifi) for line in tmp_lines: tmp_fields = line.split() if tmp_fields[0].strip().startswith(filter_keyword): tmp_receive_data = _parse_net_data(tmp_fields[1]) tmp_transmit_data = _parse_net_data(tmp_fields[9]) return (tmp_receive_data, tmp_transmit_data) return INIT_NETWORK_DATA
def change_mod_for_app_in_system(): print 'Chmod for app to RO.' cmd = 'adb shell chmod 644 %s%s' % (SHELL_SYSTEM_APP_DIR_PATH, target_app_name) if not WinSysUtils.run_sys_cmd(cmd): print 'Failed to chmod for app in system!' exit(1)
def adb_remount(): cmd = 'adb remount' output = WinSysUtils.run_sys_cmd_and_ret_content(cmd) if ('succeeded' in output): return True return False
def change_mod_for_app_in_system(): print 'Chmod for app to RO.' cmd = 'adb shell chmod 644 %s%s' % (g_const_shell_system_app_path, g_target_app_name) if (not WinSysUtils.run_sys_cmd(cmd)): print 'Failed to chmod for app in system!' exit(1)
def is_testing_package_on_top(pkg_name): cmd = 'adb shell dumpsys activity | findstr mFocusedActivity | findstr ' + pkg_name tmp_lines = WinSysUtils.run_sys_cmd_and_ret_lines(cmd) if len(tmp_lines) == 0: return False return True
def copy_app_from_sdcard_to_system_app(): print 'Copying app from sdcard to /system/app' cmd = 'adb shell cp %s%s %s' % (g_const_shell_sdcard_path, g_target_app_name, g_const_shell_system_app_path) if (not WinSysUtils.run_sys_cmd(cmd)): print 'Failed to copy app from sdcard to /system/app' exit(1)
def verify_adb_devices_connect(): logging.debug('verify adb devices connected.') cmd = 'adb devices' if ':5555' in WinSysUtils.run_sys_cmd_and_ret_content(cmd): return True else: return False
def get_instrument_run_listener_results_files_name(): cmd = 'adb shell ls %s' % SHELL_TEST_LOG_DIR_PATH print cmd ret_lines = WinSysUtils.run_sys_cmd_and_ret_lines(cmd) return [ line.rstrip('\r\n') for line in ret_lines if line.rstrip('\r\n').endswith('.xml') ]
def push_app_to_sdcard(): print 'Start to push app to andorid sdcard.' app_full_path = os.path.join(g_target_app_path, g_target_app_name) cmd = 'adb push %s %s' % (app_full_path, g_const_shell_sdcard_path) if not WinSysUtils.run_sys_cmd(cmd): print 'Failed to push app to sdcard!' exit(1)
def check_change_mod_for_app(): cmd = 'adb shell ls -l %s | findstr %s' % (g_const_shell_system_app_path, g_target_app_name) output = WinSysUtils.run_sys_cmd_and_ret_content(cmd) if (not output.startswith(g_const_read_only_auth)): print 'Check failed for chmod for app in system!' exit(1)
def check_app_copy_to_system_app(): cmd = 'adb shell ls %s | findstr %s' % (g_const_shell_system_app_path, g_target_app_name) output = WinSysUtils.run_sys_cmd_and_ret_content(cmd) if (not g_target_app_name in output): print 'Check failed for copy app to /system/app' exit(1)
def check_app_push_to_sdcard(): cmd = 'adb shell ls %s | findstr %s' % (g_const_shell_sdcard_path, g_target_app_name) output = WinSysUtils.run_sys_cmd_and_ret_content(cmd) if (not g_target_app_name in output): print 'Check failed for push app to sdcard!' exit(1)
def verify_adb_devices_serialno(): logging.debug('verify adb devices connected.') cmd = 'adb get-serialno' if 'unknown' in WinSysUtils.run_sys_cmd_and_ret_content(cmd): return False else: return True
def check_app_copy_to_system_app_dir(): cmd = 'adb shell ls %s | findstr %s' % (SHELL_SYSTEM_APP_DIR_PATH, target_app_name) output = WinSysUtils.run_sys_cmd_and_ret_content(cmd) if not target_app_name in output: print 'Check failed for copy app to /system/app' exit(1)
def check_app_push_to_shell_tmp_dir(): cmd = 'adb shell ls %s | findstr %s' % (SHELL_TMP_DIR_PATH, target_app_name) output = WinSysUtils.run_sys_cmd_and_ret_content(cmd) if not target_app_name in output: print 'Check failed for push app to /data/local/tmp/ !' exit(1)
def push_app_to_shell_tmp_dir(): print 'Start to push app to shell /data/local/tmp/' app_full_path = os.path.join(TARGET_APP_DIR_PATH, target_app_name) cmd = 'adb push %s %s' % (app_full_path, SHELL_TMP_DIR_PATH) if not WinSysUtils.run_sys_cmd(cmd): print 'Failed to push app to /data/local/tmp/ !' exit(1)
def get_uid_by_package_name(pkg_name): cmd = 'adb shell su -c cat /data/system/packages.list | findstr ' + pkg_name ret_lines = mysys.run_sys_cmd_and_ret_lines(cmd) if len(ret_lines) != 1: print 'Error, return empty from packages.list for package:', pkg_name exit(1) return ret_lines[0].split()[1]
def check_change_mod_for_app(): cmd = 'adb shell ls -l %s | findstr %s' % (SHELL_SYSTEM_APP_DIR_PATH, target_app_name) output = WinSysUtils.run_sys_cmd_and_ret_content(cmd) read_only_auth = '-rw-r--r--' if not output.startswith(read_only_auth): print 'Check failed for chmod for app in system!' exit(1)
def verify_adb_devices_serialno(): logging.debug('verify adb devices connected.') cmd = 'adb get-serialno' ret_content = WinSysUtils.run_sys_cmd_and_ret_content(cmd) if len(ret_content) == 0: return False if ('unknown' in ret_content) or ('error' in ret_content): return False return True
def run_instrument_tests_v2(cmd): input_lines = WinSysUtils.run_sys_cmd_and_ret_lines(cmd) if len(input_lines) == 0: return output_lines = [] for line in input_lines: output_lines.append(line.rstrip('\r\n') + '\n') FileUtils.append_lines_to_file(g_local_inst_run_log_file_path, (output_lines)) check_test_case_force_closed(output_lines) check_test_case_failed(output_lines)
def get_current_frames(): def _parse_hex_value(frames): return int(frames, 16) cmd = 'adb shell su -c service call SurfaceFlinger 1013' ret_content = mysys.run_sys_cmd_and_ret_content(cmd) ret_content = ret_content[ret_content.index('('):] m = re.search('[0-9|a-f]+', ret_content) if m is not None: return _parse_hex_value(m.group()) return -1
def adb_root(): output_lines = WinSysUtils.run_sys_cmd_in_subprocess('adb root') if (len(output_lines) == 0 or output_lines == ''): return True for line in output_lines: if 'already' in line: logging.debug('adbd is already running as root.') return True if 'adbd as root' in line: logging.debug('adb root success.') return True logging.error('Adb root failed!') for line in output_lines: logging.error(line) return False
def init_local_report_paths(): report_dir_name = 'logs_%s' % WinSysUtils.get_current_date_and_time() report_dir_path = os.path.join(os.getcwd(), 'logs', report_dir_name) global g_local_report_dir_path g_local_report_dir_path = report_dir_path if not os.path.exists(report_dir_path): os.makedirs(report_dir_path) global g_local_inst_run_log_file_path inst_run_log_file_name = 'InstrutmentRunLog.log' g_local_inst_run_log_file_path = os.path.join(report_dir_path, inst_run_log_file_name) global g_local_test_logging_file_path logging_name = 'TestLoggingReport.log' g_local_test_logging_file_path = os.path.join(report_dir_path, logging_name) global g_local_logcat_log_file_path logcat_log_file_name = 'LogcatLogByTag.log' g_local_logcat_log_file_path = os.path.join(report_dir_path, logcat_log_file_name)
def send_reuqest_and_write_response(): city_ids = g_city_ids url = 'http://apis.baidu.com/apistore/weatherservice/recentweathers' header_parms = {'apikey':'7705cca8df9fb3dbe696ce2310979a62'} file_name = 'baidu_weather_data_update_freq_src_%s.log' % g_cur_date file_path = os.path.join(g_log_dir, file_name) output_lines = [] for id in city_ids: resp = None try: resp = HttpJsonUtils.send_get_request_with_header_and_return(url, header_parms, {'cityid':id}) except Exception, e: logging.error('Exception: %s' % e) if resp is None: return line = '%s: %s\n' % (WinSysUtils.get_current_date_and_time(), resp.decode('unicode_escape')) output_lines.append(line)
def init_path_vars(): cur_date = WinSysUtils.get_current_date() global g_log_root_path global g_log_dir_path_for_win global g_log_dir_path_for_shell g_log_root_path = os.path.join(os.getcwd(), 'MonkeyReprots', cur_date) g_log_dir_path_for_win = r'%s\%s_%s' % (g_log_root_path, cur_date, g_run_num) g_log_dir_path_for_shell = '/sdcard/monkey_test_logs' # profile log path global g_profile_log_dir_path g_profile_log_dir_path = os.path.join(g_log_dir_path_for_win, 'profile_logs') # screen captures path global g_captures_dir_path_for_win global g_captures_dir_path_for_shell global g_capture_path_for_shell g_captures_dir_path_for_win = r'%s\captures' % g_log_dir_path_for_win g_captures_dir_path_for_shell = '%s/captures' % g_log_dir_path_for_shell g_capture_path_for_shell = '%s/capture_%s' % (g_captures_dir_path_for_shell, cur_date) # logcat log path global g_logcat_log_path_for_shell g_logcat_log_path_for_shell = '%s/logcat_log.log' % g_log_dir_path_for_shell # monkey log, local global g_monkey_log_path g_monkey_log_path = r'%s\monkey_log.log' % g_log_dir_path_for_win # rom props path, local global g_rom_props_file_path g_rom_props_file_path = r'%s\rom_props.log' % g_log_dir_path_for_win # whitelist path global g_whitelist_file_path_for_win global g_whitelist_file_path_for_shell g_whitelist_file_path_for_win = os.path.join(os.getcwd(), 'whitelist.txt') g_whitelist_file_path_for_shell = '%s/whitelist.txt' % g_log_dir_path_for_shell
def send_reuqest_and_write_response(): city_ids = g_city_ids url = 'http://apis.baidu.com/apistore/weatherservice/recentweathers' header_parms = {'apikey': '7705cca8df9fb3dbe696ce2310979a62'} file_name = 'baidu_weather_data_update_freq_src_%s.log' % g_cur_date file_path = os.path.join(g_log_dir, file_name) output_lines = [] for id in city_ids: resp = None try: resp = HttpJsonUtils.send_get_request_with_header_and_return( url, header_parms, {'cityid': id}) except Exception, e: logging.error('Exception: %s' % e) if resp is None: return line = '%s: %s\n' % (WinSysUtils.get_current_date_and_time(), resp.decode('unicode_escape')) output_lines.append(line)
def rm_old_captures_on_remote(): cmd = 'adb shell ls %s' % g_remote_tmp_dir_path lines_files = WinSysUtils.run_sys_cmd_and_ret_lines(cmd) flag_found = False for line in lines_files: if g_remote_tmp_captures_dir_name in line: flag_found = True break if flag_found: cmd = 'adb shell ls %s' % g_remote_tmp_captures_dir_path lines_capture = WinSysUtils.run_sys_cmd_and_ret_lines(cmd) if len(lines_capture) > 0: cmd = 'adb shell rm %s/*.png' % g_remote_tmp_captures_dir_path WinSysUtils.run_sys_cmd(cmd) else: cmd = 'adb shell mkdir -p %s' % g_remote_tmp_captures_dir_path WinSysUtils.run_sys_cmd(cmd)
def dump_logcat_by_tag(tag, file_path): cmd = 'adb logcat -c && adb logcat -s %s -v time -d > %s' % (tag, file_path) print cmd WinSysUtils.run_sys_cmd(cmd)
def adb_stop(): cmd = 'adb kill-server' return WinSysUtils.run_sys_cmd(cmd)
def main_test_fun_weather_api(): log_env_setup() log_report_header() WinSysUtils.exec_fun_and_log_exec_time(main_get_city_weather_data) log_report_trailer()
def run_instrument_tests_v1(cmd): WinSysUtils.run_sys_cmd(cmd)
''' import os import time import logging from ZJPyUtils import HttpJsonUtils, FileUtils, WinSysUtils, LogUtils # ---------------------------------------------------- # Variables # ---------------------------------------------------- g_city_ids = ('101010100', '101200101', '101200105', '101200601' ) # 北京,武汉,江夏,黄石 g_log_dir = os.path.join(os.getcwd(), 'logs', 'baidu_weather_data_update_freq') g_cur_date = WinSysUtils.get_current_date() # ---------------------------------------------------- # Collect weather data # ---------------------------------------------------- def init_log(): FileUtils.create_dir(g_log_dir) log_name = 'baidu_weather_data_update_freq_run_log_%s.log' % g_cur_date log_file = os.path.join(g_log_dir, log_name) LogUtils.init_log_config(logging.DEBUG, logging.INFO, log_file) def send_reuqest_and_write_response(): city_ids = g_city_ids
def get_net_send_data_by_uid(uid): cmd = 'adb shell cat /proc/uid_stat/%s/tcp_snd' % uid ret_content = mysys.run_sys_cmd_and_ret_content(cmd) return ret_content.strip()
1) Run test cases and dump execution logs. 2) Start and dump logcat log. 3) Pull run listener results files and snapshots. 4) Generate html testing report. ''' import os import time import subprocess from ZJPyUtils import AdbUtils, WinSysUtils # ---------------------------------------------------- # Constants # ---------------------------------------------------- CUR_DATE_TIME = WinSysUtils.get_current_date_and_time() REPORT_DIR_NAME = 'logs_inst_test_%s' % CUR_DATE_TIME REPORT_DIR_PATH = os.path.join(os.getcwd(), 'logs', REPORT_DIR_NAME) LOGCAT_FILE_NAME = 'logcat_log.txt' LOGCAT_FILE_PATH = os.path.join(REPORT_DIR_PATH, LOGCAT_FILE_NAME) INST_TEST_LOG_FILE_NAME = 'instrument_test_log.txt' INST_TEST_LOG_FILE_PATH = os.path.join(REPORT_DIR_PATH, INST_TEST_LOG_FILE_NAME) SHELL_TEST_LOG_DIR_PATH = '/sdcard/auto_test_logs/' SHELL_SNAPSHOTS_DIR_PATH = SHELL_TEST_LOG_DIR_PATH + '.snapshots' SHELL_SNAPSHOTS_DIR_NEW_PATH = SHELL_TEST_LOG_DIR_PATH + 'snapshots' DATA_DIR_PATH = os.path.join(os.getcwd(), 'data') JAR_FILE_PATH = os.path.join(DATA_DIR_PATH, 'XmlTransform.jar') XSLT_FILE_PATH = os.path.join(DATA_DIR_PATH, 'testsuites.xstl')
def open_app_details_settings(pkg_name): action = 'android.settings.APPLICATION_DETAILS_SETTINGS' cmd = 'adb shell am start -a "%s" -d "package:%s"' % (action, pkg_name) print cmd WinSysUtils.run_sys_cmd(cmd)
2) Parse the collected weather data to show the data refresh frequency. ''' import os import time import logging from ZJPyUtils import HttpJsonUtils, FileUtils, WinSysUtils, LogUtils # ---------------------------------------------------- # Variables # ---------------------------------------------------- g_city_ids = ('101010100', '101200101', '101200105', '101200601') # 北京,武汉,江夏,黄石 g_log_dir = os.path.join(os.getcwd(), 'logs', 'baidu_weather_data_update_freq') g_cur_date = WinSysUtils.get_current_date() # ---------------------------------------------------- # Collect weather data # ---------------------------------------------------- def init_log(): FileUtils.create_dir(g_log_dir) log_name = 'baidu_weather_data_update_freq_run_log_%s.log' % g_cur_date log_file = os.path.join(g_log_dir, log_name) LogUtils.init_log_config(logging.DEBUG, logging.INFO, log_file) def send_reuqest_and_write_response(): city_ids = g_city_ids url = 'http://apis.baidu.com/apistore/weatherservice/recentweathers'
def delete_old_instrument_run_listener_logs(): cmd = 'adb shell rm -rf ' + SHELL_TEST_LOG_DIR_PATH print cmd WinSysUtils.run_sys_cmd(cmd)
def pull_remote_captures_to_local(): # pull both dir and files in the dir from remote cmd = 'adb pull %s %s' % (g_remote_tmp_captures_dir_path, g_local_report_dir_path) WinSysUtils.run_sys_cmd(cmd)
def main_get_city_weather_data(): for line in read_city_list(): WinSysUtils.exec_fun_and_log_exec_time(log_weather_data_line, line.rstrip('\n'))
def get_instrument_run_listener_results_files_name(): cmd = 'adb shell ls %s' % SHELL_TEST_LOG_DIR_PATH print cmd ret_lines = WinSysUtils.run_sys_cmd_and_ret_lines(cmd) return [line.rstrip('\r\n') for line in ret_lines if line.rstrip('\r\n').endswith('.xml')]
def create_html_report(src_results): cmd = 'java -jar %s -f=%s -x=%s' % (JAR_FILE_PATH, src_results, XSLT_FILE_PATH) print cmd WinSysUtils.run_sys_cmd(cmd)
def pull_instrument_run_listener_results_file(file_name): cmd = 'adb pull %s %s' % (SHELL_TEST_LOG_DIR_PATH + file_name, REPORT_DIR_PATH) print cmd WinSysUtils.run_sys_cmd(cmd)