1# -*- coding: utf-8 -*- 2# Copyright (c) 2023 Huawei Device Co., Ltd. 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import argparse 16import datetime 17import json 18import os 19import subprocess 20import sys 21import time 22import traceback 23 24import cv2 25import numpy 26import pytesseract 27 28BASE_DIR = os.path.dirname(os.path.dirname(__file__)) 29 30sys.path.append(os.path.join(BASE_DIR, 'acls_check')) 31sys.path.append(os.path.join(BASE_DIR, 'APL_compare_03')) 32from acl_check import * 33from compare import * 34from pytesseract import Output 35from PIL import Image 36 37 38def print_to_log(str): 39 time = datetime.datetime.now() 40 str = "[{}] {}".format(time, str) 41 print(str) 42 with open(os.path.join(args.save_path, 43 'test_{}.log'.format(args.device_num)), 44 mode='a', 45 encoding='utf-8') as file: 46 console = sys.stdout 47 sys.stdout = file 48 print(str) 49 sys.stdout = console 50 file.close() 51 52 53def enter_cmd(mycmd, waittime=0, printresult=1): 54 if mycmd == "": 55 return 56 global cmd_retry_cnt 57 cmd_retry_cnt = 1 58 enter_cmdRetry = 2 59 while enter_cmdRetry: 60 enter_cmdRetry -= 1 61 try: 62 p = subprocess.Popen(mycmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) 63 result, unused_err = p.communicate(timeout=25) 64 try: 65 result = result.decode(encoding="utf-8") 66 except UnicodeDecodeError: 67 result = result.decode('gbk', errors='ignore') 68 break 69 except Exception as e: 70 result = 'retry failed again' 71 print_to_log(e) 72 cmd_retry_cnt += 1 73 p.kill() 74 if printresult == 1: 75 print_to_log(mycmd) 76 print_to_log(result) 77 sys.stdout.flush() 78 if waittime != 0: 79 time.sleep(waittime) 80 return result 81 82 83def enter_shell_cmd(shellcmd, waittime=1, printresult=1): 84 if shellcmd == "": 85 return 86 cmd = 'hdc -l0 -t {} shell "{}"'.format(args.device_num, shellcmd) 87 return enter_cmd(cmd, waittime, printresult) 88 89 90def sys_exit(): 91 enter_shell_cmd("cd /data/log/faultlog/temp && tar -cf after_test_cppcrash{}.tar cppcrash*".format(args.device_num)) 92 file_from_dev("/data/log/faultlog/temp/after_test_cppcrash{}.tar".format(args.device_num), os.path.normpath(args.save_path)) 93 enter_shell_cmd("cd /data/log/faultlog/faultlogger && tar -cf after_test_jscrash{}.tar jscrash*".format(args.device_num)) 94 file_from_dev("/data/log/faultlog/faultlogger/after_test_jscrash{}.tar".format(args.device_num), os.path.normpath(args.save_path)) 95 print_to_log("SmokeTest: SmokeTest find some key problems!") 96 print_to_log("SmokeTest: End of check, test failed!") 97 sys.exit(98) 98 99 100def file_to_dev(src, dst): 101 cmd = 'hdc -l0 -t {} file send "{}" "{}"'.format(args.device_num, src, dst) 102 return enter_cmd(cmd, 1, 1) 103 104 105def file_from_dev(src, dst): 106 cmd = 'hdc -l0 -t {} file recv "{}" "{}"'.format(args.device_num, src, dst) 107 return enter_cmd(cmd, 1, 1) 108 109 110def connect_check(): 111 connection_status = enter_cmd("hdc list targets", 2) 112 connection_cnt = 0 113 while args.device_num not in connection_status and connection_cnt < 15: 114 connection_status = enter_cmd("hdc list targets", 2) 115 connection_cnt += 1 116 if connection_cnt == 15: 117 print_to_log("SmokeTest: Device disconnection!!") 118 print_to_log("SmokeTest: End of check, test failed!") 119 sys.exit(101) 120 121 122def sandbox_check(process): 123 print_to_log("SmokeTest: start to check sandbox path") 124 medialibrarydata_pidnum = enter_shell_cmd("pgrep -f {}".format(process), 1) 125 medialibrarydata_pidnum = medialibrarydata_pidnum.strip() 126 sandboxf = enter_shell_cmd('echo "ls /storage/media/local/"|nsenter -t {} -m sh'.format(medialibrarydata_pidnum), 1) 127 if "files" not in sandboxf: 128 print_to_log("SmokeTest: error: can not find sandbox path : /storage/media/local/files") 129 return -1 130 else: 131 print_to_log("SmokeTest: success: find sandbox path : /storage/media/local/files") 132 return 1 133 134 135def get_coordinate(path, target): 136 wifi_numbers = 8 137 height = 97 138 wifi_range = [236, 286, 45, 300] 139 coordinate = [] 140 img = cv2.imread(path) 141 tessdata_dir_config = '--tessdata-dir "C:\\Program Files (x86)\\Tesseract-OCR\\tessdata"' 142 while wifi_numbers: 143 wifi_range[0] += height 144 wifi_range[1] += height 145 print_to_log(wifi_range) 146 data_img = img[wifi_range[0]:wifi_range[1], wifi_range[2]:wifi_range[3]] 147 data = pytesseract.image_to_data(data_img, 148 output_type=Output.DICT, 149 config=tessdata_dir_config, 150 lang='eng') 151 for i in range(len(data['text'])): 152 if data['text'][i] == target: 153 dx = int((wifi_range[2] + wifi_range[3]) / 2) 154 dy = int((wifi_range[0] + wifi_range[1]) / 2) 155 coordinate.append(dx) 156 coordinate.append(dy) 157 wifi_numbers -= 1 158 if coordinate: 159 break 160 return coordinate 161 162 163def connect_wifi(prefix, pic): 164 try: 165 data = get_coordinate("{}\\{}_{}".format(args.save_path, prefix, pic), 166 "testapold") 167 enter_shell_cmd("uinput -M -m {} {} -c 0".format(data[0], data[1]), 168 WAIT_TIME_TWO) 169 enter_shell_cmd("uinput -M -m 360 200 -c 0") 170 enter_shell_cmd("uinput -K -d 2032 -u 2032 -d 2017 -u 2017 -d 2035" 171 " -u 2035 -d 2035 -u 2035 -d 2039 -u 2039 -d 2000" 172 " -u 2000 -d 2034 -u 2034 -d 2020 -u 2020 -d 2001 -u 2001") 173 enter_shell_cmd("uinput -M -m 360 200 -c 0") 174 enter_shell_cmd("uinput -M -m 50 1140 -c 0") 175 enter_shell_cmd("uinput -M -m 500 1020 -c 0") 176 enter_shell_cmd("uinput -M -m 50 1140 -c 0") 177 enter_shell_cmd("uinput -K -d 2054 -u 2054") 178 enter_shell_cmd("snapshot_display -f /data/local/tmp/screen_test/{}".format("testapold.jpeg")) 179 file_from_dev("/data/local/tmp/screen_test/{}".format("testapold.jpeg"), args.save_path) 180 enter_shell_cmd("uinput -M -m 550 680 -c 0", single_action[0]) 181 except Exception as e: 182 print(e) 183 print_to_log("SmokeTest: wifi list loading errror!") 184 185 186def calculate(image1, image2): 187 image1 = cv2.cvtColor(numpy.asarray(image1), cv2.COLOR_RGB2BGR) 188 image2 = cv2.cvtColor(numpy.asarray(image2), cv2.COLOR_RGB2BGR) 189 hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0]) 190 hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0]) 191 degree = 0 192 for i in range(len(hist1)): 193 if hist1[i] != hist2[i]: 194 degree = degree + (1 - abs(hist1[i] - hist2[i]) / max(hist1[i], hist2[i])) 195 else: 196 degree = degree + 1 197 degree = degree / len(hist1) 198 return degree 199 200 201def classify_hist_with_split(image1, image2, size=(256, 256)): 202 image1 = Image.open(image1) 203 image2 = Image.open(image2) 204 image1 = cv2.cvtColor(numpy.asarray(image1), cv2.COLOR_RGB2BGR) 205 image2 = cv2.cvtColor(numpy.asarray(image2), cv2.COLOR_RGB2BGR) 206 image1 = cv2.resize(image1, size) 207 image2 = cv2.resize(image2, size) 208 sub_image1 = cv2.split(image1) 209 sub_image2 = cv2.split(image2) 210 sub_data = 0 211 for im1, im2 in zip(sub_image1, sub_image2): 212 sub_data += calculate(im1, im2) 213 sub_data = sub_data / 3 214 return sub_data 215 216 217def crop_picture(prefix, pic, crop_range): 218 try: 219 pic_path = os.path.join(args.save_path, '{}_{}'.format(prefix, pic)) 220 save_path = os.path.join(args.save_path, '{}_{}'.format(prefix, pic)) 221 im = cv2.imread(pic_path) 222 im = im[crop_range[0]:crop_range[1], crop_range[2]:crop_range[3]] 223 cv2.imwrite(save_path, im) 224 except Exception as e: 225 pass 226 227 228def cmp_picture(prefix, pic, num=1): 229 if num == 1: 230 img1_path = os.path.join(args.anwser_path, pic) 231 else: 232 img1_path = os.path.join(args.anwser_path, '2_{}'.format(pic)) 233 print('img1_path: {}'.format(img1_path)) 234 235 img2_path = os.path.join(args.save_path, '{}_{}'.format(prefix, pic)) 236 print('img2_path: {}'.format(img2_path)) 237 cmp_init = 0 238 try: 239 cmp_result = classify_hist_with_split(img1_path, img2_path) 240 print("compare result:" + "%.6f%%" % (cmp_result * 100)) 241 return cmp_result * 100 242 except: 243 print(traceback.format_exc()) 244 return cmp_init 245 246 247def shot_and_cmp(image): 248 prefix = args.device_num 249 enter_shell_cmd( 250 "snapshot_display -f /data/local/tmp/screen_test/{}_{}".format(prefix, image)) 251 file_from_dev("/data/local/tmp/screen_test/{}_{}".format(prefix, image), 252 args.save_path) 253 similarity = cmp_picture(prefix, image) 254 print_to_log("SmokeTest: launcher similarity is {}%".format(similarity)) 255 return similarity 256 257 258def distributed_test(): 259 if "1/2" in args.test_num or "2/2" in args.test_num: 260 report_path = os.path.normpath( 261 os.path.join(args.save_path, "distributed_report.txt")) 262 if args.test_num == "2/2": 263 enter_shell_cmd("ifconfig eth0 192.168.0.1") 264 ping_result = enter_shell_cmd("ping 192.168.0.2 -i 1 -c 2", 3) 265 file_is_exist = enter_shell_cmd( 266 "cd /data; find . -name distributed_report.txt") 267 ping_cnt = 0 268 wait_cnt = 0 269 while "2 packets transmitted, 2 received" not in ping_result and ping_cnt < 20: 270 ping_result = enter_shell_cmd("ping 192.168.0.2 -i 1 -c 2", 271 WAIT_TIME_FOUR) 272 ping_cnt += 1 273 if ping_cnt == 30: 274 print_to_log("SmokeTest: Ping failed, timeout of 80s") 275 sys_exit() 276 while "distributed_report.txt" not in file_is_exist and wait_cnt < 30: 277 print_to_log("SmokeTest: waiting for the distributed test to end ") 278 file_is_exist = enter_shell_cmd( 279 "cd /data; find . -name distributed_report.txt", WAIT_TIME_FOUR) 280 wait_cnt += 1 281 elif args.test_num == "1/2": 282 enter_shell_cmd("ifconfig eth0 192.168.0.2") 283 ping_result = enter_shell_cmd( 284 "ping 192.168.0.1 -i 1 -c 2", WAIT_TIME_FOUR) 285 ping_cnt = 0 286 while "2 packets transmitted, 2 received" not in ping_result and ping_cnt < 20: 287 ping_result = enter_shell_cmd("ping 192.168.0.1 -i 1 -c 2", 288 WAIT_TIME_FOUR) 289 ping_cnt += 1 290 if ping_cnt == 30: 291 print_to_log("SmokeTest: Ping failed, timeout of 80s") 292 print_to_log("SmokeTest: ##### case 0 : distributed test start #####") 293 execute_path = os.path.normpath(os.path.join(args.tools_path, "resource")) 294 os.system( 295 "cd {} && python distributedtest.py --path {}".format(execute_path, 296 args.save_path)) 297 distributed_result = "" 298 try: 299 with open(report_path, mode='r', encoding='utf-8', errors='ignore') as f: 300 f.seek(0) 301 distributed_result = f.read() 302 f.close() 303 except Exception as reason: 304 print_to_log("SmokeTest: distributed_report.txt do not exist!") 305 if "distributedcalc" in distributed_result: 306 print_to_log("SmokeTest: testcase 0, distributed is ok!") 307 else: 308 print_to_log("SmokeTest: error:testcase 0, distributed failed!") 309 sys_exit() 310 enter_shell_cmd("ifconfig eth0 down") 311 312 313def open_wlan(): 314 enter_shell_cmd( 315 "aa start -a com.ohos.settings.MainAbility -b com.ohos.settings", 316 WAIT_TIME_FOUR) 317 enter_shell_cmd("uinput -M -m 300 300 -c 0", WAIT_TIME_TWO) 318 enter_shell_cmd("uinput -M -m 640 200 -c 0", WAIT_TIME_FOUR) 319 time.sleep(WAIT_TIME_FOUR) 320 enter_shell_cmd("killall com.ohos.settings", WAIT_TIME_TWO) 321 322 323if __name__ == "__main__": 324 parser = argparse.ArgumentParser(description='manual to this script') 325 parser.add_argument('--config', type=str, default='app_capture_screen_test_config.json') 326 parser.add_argument('--test_num', type=str, default='1/1') 327 parser.add_argument('--tools_path', type=str, default='D:\\DeviceTestTools\\screenshot') 328 parser.add_argument('--anwser_path', type=str, default='D:\\DeviceTestTools\\screenshot\\resource') 329 parser.add_argument('--save_path', type=str, default='D:\\DeviceTestTools\\screenshot') 330 parser.add_argument('--device_num', type=str, default='null') 331 parser.add_argument('--pr_url', type=str, default='developtools_integration_verification') 332 args = parser.parse_args() 333 334 try: 335 if args.device_num == 'null': 336 result = enter_cmd("hdc list targets", 1, 0) 337 print(result) 338 args.device_num = result.split()[0] 339 with open(args.config) as f: 340 all_app = json.load(f) 341 cmp_status = 0 342 global_pos = all_app[0] 343 344 WAIT_TIME_TWO = 2 345 WAIT_TIME_FOUR = 4 346 347 # 保存hilog 348 enter_shell_cmd("hilog -w stop") 349 enter_shell_cmd('rm -rf /data/log/hilog/*') 350 enter_shell_cmd('hilog -w start -l 1M -n 1000 -m zlib -j 11') 351 reboot_cnt = 2 352 while reboot_cnt: 353 reboot_cnt -= 1 354 enter_shell_cmd("mkdir -p /data/local/tmp/screen_test/train_set") 355 enter_shell_cmd("power-shell wakeup;power-shell setmode 602") 356 rmlock_cnt = 3 357 while rmlock_cnt: 358 enter_shell_cmd("uinput -T -m 425 400 425 1000;uinput -T -m 425 1000 425 400") 359 rmlock_cnt -= 1 360 # enter_shell_cmd("hilog -w stop") 361 # enter_shell_cmd( 362 # "cd /data/log/hilog && tar -cf system_start_log_{}.tar *".format(args.device_num)) 363 # file_from_dev("/data/log/hilog/system_start_log_{}.tar".format(args.device_num), 364 # args.save_path) 365 connect_check() 366 launcher_similarity = shot_and_cmp("launcher.jpeg") 367 power_state = enter_shell_cmd("hidumper -s 3308") 368 if "State=2" not in power_state: 369 print_to_log("SmokeTest: ERROR, DISPLAY POWER MANAGER DUMP State ≠ 2") 370 if launcher_similarity >= 50: 371 print_to_log("SmokeTest: launcher screenshot comparison is ok!") 372 break 373 elif reboot_cnt >= 1: 374 print_to_log("SmokeTest: launcher screenshot comparison failed, reboot and try!!!") 375 enter_shell_cmd("rm -rf /data/*;reboot") 376 for i in range(5): 377 enter_cmd("hdc list targets", 10) 378 else: 379 print_to_log("SmokeTest: launcher screenshot comparison failed") 380 sys_exit() 381 382 enter_shell_cmd("cat /proc/`pidof foundation`/smaps_rollup") 383 384 print_to_log("\nSmokeTest: ########## First check key processes start ##############") 385 lose_process = [] 386 process_pid = {} 387 with open(os.path.join(args.tools_path, 'process.txt'), "r+") as f: 388 text = f.read() 389 two_check_process_list = text.split('#####')[1].split()[0:-1] 390 other_process_list = text.split('#####')[2].split() 391 for pname in two_check_process_list: 392 pids = enter_cmd("hdc -l0 -t {} shell pidof {}".format(args.device_num, pname), 0, 1) 393 try: 394 pidlist = pids.split() 395 int(pidlist[0]) 396 for pid in pidlist: 397 int(pid) 398 process_pid[pname] = pidlist 399 except: 400 lose_process.append(pname) 401 all_p = enter_shell_cmd("ps -elf") 402 for pname in other_process_list: 403 findp = all_p.find(pname, 0, len(all_p)) 404 if findp == -1: 405 lose_process.append(pname) 406 407 enter_shell_cmd("hilog -w stop") 408 enter_shell_cmd("cd /data/log/hilog && tar -cf system_start_log_{}.tar *".format(args.device_num)) 409 file_from_dev("/data/log/hilog/system_start_log_{}.tar".format(args.device_num),args.save_path) 410 if lose_process: 411 print_to_log("SmokeTest: error: %s, These processes do not exist!!!" % lose_process) 412 sys_exit() 413 # 414 # acl_check_main(args.device_num) 415 # native_sa = os.path.normpath(os.path.join(args.tools_path, "acls_check", "native_sa.log")) 416 # try: 417 # with open(native_sa, mode='r', encoding='utf-8', errors='ignore') as native_file: 418 # native_file.seek(0) 419 # acl_result = native_file.read() 420 # native_file.close() 421 # except Exception as reason: 422 # print_to_log("SmokeTest: error: native_sa.log do not exist!") 423 # if "ACL check failed" in acl_result: 424 # print_to_log("SmokeTest: error: acl check failed") 425 # sys_exit() 426 427 try: 428 args.test_num.index('/') 429 idx_total = args.test_num.split('/') 430 if len(idx_total) != 2: 431 print_to_log("SmokeTest: test_num is invaild !!!") 432 sys_exit() 433 elif idx_total[1] == '1': 434 idx_list = global_pos['DEVICE_2'] + global_pos['DEVICE_1'] 435 else: 436 idx_list = global_pos['DEVICE_{}'.format(idx_total[0])] 437 except ValueError as e: 438 print_to_log(e) 439 idx_list = list(map(eval, args.test_num.split())) 440 print_to_log("SmokeTest: start to carry out the following testcases: ") 441 print_to_log("SmokeTest: testcase number: {} ".format(idx_list)) 442 443 # open_wlan() 444 fail_idx_list = [] 445 fail_name_list = [] 446 smoke_first_failed = '' 447 for idx in idx_list: 448 single_app = all_app[idx] 449 sys.stdout.flush() 450 call_app_cmd = single_app['entry'] 451 capture_screen_cmd = "snapshot_display -f /data/local/tmp/screen_test/{}_{}" 452 print_to_log("\nSmokeTest: ##### case {} : {} test start #####".format(idx, single_app['app_name'])) 453 testcnt = 3 454 while testcnt: 455 testok = 0 456 if testcnt != 3: 457 print_to_log("SmokeTest: this testcase try again >>>>>>:\n") 458 if single_app['entry'] != "": 459 enter_shell_cmd(call_app_cmd, WAIT_TIME_FOUR) 460 print_to_log("SmokeTest: execute command {}".format(single_app['all_actions'])) 461 prefix = args.device_num 462 raw_pic_name = '' 463 pic_name = '' 464 for single_action in single_app['all_actions']: 465 if type(single_action[1]) == str and single_action[1] == 'shot_cmd': 466 if len(single_action) == 3: 467 pic_name = "{}{}".format(single_action[2], ".jpeg") 468 else: 469 pic_name = "{}{}".format(single_app['app_name'], ".jpeg") 470 enter_shell_cmd("rm /data/local/tmp/screen_test/*{}".format(pic_name)) 471 enter_shell_cmd(capture_screen_cmd.format(prefix, pic_name)) 472 file_from_dev("/data/local/tmp/screen_test/{}_{}".format(prefix, pic_name), args.save_path) 473 next_cmd = "" 474 elif type(single_action[1]) == str and single_action[1] == 'cmp_twice': 475 next_cmd = "" 476 sys.stdout.flush() 477 pic = "{}{}".format(single_action[2], ".jpeg") 478 similarity = single_action[3] 479 crop_range = single_app[single_action[4]] 480 crop_picture(prefix, pic, crop_range) 481 first_similarity = cmp_picture(prefix, pic) 482 second_similarity = cmp_picture(prefix, pic, WAIT_TIME_TWO) 483 print_to_log("SmokeTest: first picture similarity is {}%".format(first_similarity)) 484 print_to_log("SmokeTest: second picture similarity is {}%".format(second_similarity)) 485 if first_similarity >= similarity or second_similarity >= similarity: 486 if testok != -1: 487 testok = 1 488 print_to_log("SmokeTest: {} screenshot check is ok".format(pic)) 489 else: 490 testok = -1 491 print_to_log("SmokeTest: {} screenshot check is abnarmal".format(pic)) 492 elif type(single_action[1]) == str and single_action[1] == 'cmp_cmd-level': 493 next_cmd = "" 494 sys.stdout.flush() 495 if len(single_action) == 4: 496 similarity = single_action[3] 497 else: 498 similarity = global_pos['cmp_cmd-level'][1] 499 similarity = int(similarity) 500 print_to_log("SmokeTest: start to contrast screenshot") 501 pic = "{}{}".format(single_action[2], ".jpeg") 502 crop_range = [80, 1200, 0, 720] 503 crop_picture(prefix, pic, crop_range) 504 pic_similarity = cmp_picture(prefix, pic) 505 print_to_log("SmokeTest: picture similarity is {}%".format(pic_similarity)) 506 if len(single_action) >= 3: 507 if pic_similarity >= similarity: 508 if testok != -1: 509 testok = 1 510 print_to_log("SmokeTest: {} screenshot check is ok".format(pic)) 511 else: 512 testok = -1 513 print_to_log("SmokeTest: {} screenshot check is abnarmal".format(pic)) 514 elif type(single_action[1]) == str and single_action[1] == 'install_hap': 515 next_cmd = "" 516 if len(single_action) == 3: 517 enter_cmd("hdc -l0 -t {} install \"{}\"".format(args.device_num, 518 os.path.normpath(os.path.join(args.tools_path, single_action[2])))) 519 elif type(single_action[1]) == str and single_action[1] == 'get_file_from_dev': 520 next_cmd = "" 521 if len(single_action) == 3: 522 enter_cmd("hdc -l0 -t {} file recv \"{}\" \"{}\"".format(args.device_num, 523 single_action[2], os.path.normpath(args.save_path))) 524 elif type(single_action[1]) == str and single_action[1] == 'send_file_to_dev': 525 next_cmd = "" 526 if len(single_action) == 4: 527 enter_cmd("hdc -l0 -t {} file send \"{}\" \"{}\"".format(args.device_num, 528 os.path.normpath(os.path.join(args.tools_path, single_action[2])), single_action[3])) 529 elif type(single_action[1]) == str and single_action[1] == 'connect_wifi': 530 next_cmd = "" 531 pic = "{}{}".format(single_action[2], ".jpeg") 532 # connect_wifi(prefix, pic) 533 elif type(single_action[1]) == str and single_action[1] == 'sandbox_path_check': 534 next_cmd = "" 535 if sandbox_check("com.ohos.medialibrary.medialibrarydata") == 1 and testok == 1: 536 testok = 1 537 else: 538 testok = -1 539 elif type(single_action[1]) == str and single_action[1] == 'process_crash_check': 540 next_cmd = "" 541 if len(single_action) == 3: 542 p = enter_shell_cmd("cd /data/log/faultlog/temp && grep \"Process name\" -rnw ./", 543 single_action[0]) 544 result = "".join(p) 545 findsome = result.find(single_action[2], 0, len(result)) 546 if findsome != -1: 547 testok = -1 548 print_to_log("SmokeTest: \"{}\" error:find fatal crash \"{}\"!".format(single_action[1], 549 single_action[2])) 550 sys_exit() 551 else: 552 testok = 1 553 print_to_log("SmokeTest: \"{}\" result is ok, not find fatal\ 554 crash \"{}\"!".format(single_action[1], single_action[2])) 555 sys.stdout.flush() 556 elif type(single_action[1]) == str: 557 if single_action[1] not in single_app.keys(): 558 target_ = global_pos[single_action[1]] 559 else: 560 target_ = single_app[single_action[1]] 561 if type(target_[0]) == str: 562 next_cmd = "" 563 p = enter_shell_cmd(target_[0], single_action[0]) 564 result = "".join(p) 565 if len(target_) > 1: 566 findsome = result.find(target_[1], 0, len(result)) 567 if findsome != -1: 568 testok = 1 569 print_to_log("SmokeTest: \"{}\" check ok, find \"{}\"!".format(target_[0], target_[1])) 570 else: 571 testok = -1 572 print_to_log("SmokeTest: \"{}\" check failed, no \"{}\"!".format(target_[0], target_[1])) 573 sys.stdout.flush() 574 else: 575 next_cmd = "uinput -M -m {} {} -c 0".format(target_[0], target_[1]) 576 else: 577 next_cmd = "uinput -M -m {} {} -c 0".format(single_action[1], single_action[2]) 578 enter_shell_cmd(next_cmd, single_action[0]) 579 580 if testok == 1: 581 print_to_log("SmokeTest: testcase {}, {} is ok!".format(idx, single_app['app_name'])) 582 testcnt = 0 583 elif testok == -1 and smoke_first_failed == '': 584 if testcnt == 1: 585 fail_idx_list.append(idx) 586 fail_name_list.append(single_app['app_name']) 587 smoke_first_failed = single_app['app_name'] 588 print_to_log("SmokeTest: error:testcase {}, {} is failed!".format(idx, single_app['app_name'])) 589 testcnt -= 1 590 elif testok == -1 and smoke_first_failed != '': 591 fail_idx_list.append(idx) 592 fail_name_list.append(single_app['app_name']) 593 print_to_log("SmokeTest: error:testcase {}, {} is failed!".format(idx, single_app['app_name'])) 594 testcnt = 0 595 else: 596 testcnt = 0 597 connect_check() 598 599 enter_shell_cmd("cd /data/log/faultlog/temp && grep \"Process name\" -rnw ./", 1) 600 enter_shell_cmd("cd /data/log/faultlog/faultlogger && grep \"Process name\" -rnw ./", 1) 601 602 fail_str_list = [str(x) for x in fail_idx_list] 603 reboot_test_num = " ".join(fail_str_list) 604 if len(fail_idx_list) != 0: 605 print_to_log("SmokeTest: failed testcase number: {} ".format(fail_str_list)) 606 print_to_log("SmokeTest: check \"reboot\" in reboot.txt".format(args.save_path)) 607 with open(os.path.normpath(os.path.join(args.tools_path, "reboot.txt")), mode='a+') as f: 608 f.seek(0) 609 reboot_result = f.read() 610 f.close() 611 if len(reboot_result) < 1 and reboot_cnt >= 1: 612 print_to_log("SmokeTest: no \"reboot\" found in the reboot.txt") 613 print_to_log("SmokeTest: the device will reboot and try the failed testcase") 614 print_to_log("SmokeTest: mkdir {}\\reboot".format(args.save_path)) 615 os.system("mkdir {}\\reboot".format(args.save_path)) 616 print_to_log("SmokeTest: write \"reboot\" into reboot.txt".format(args.save_path)) 617 with open(os.path.normpath(os.path.join(args.tools_path, "reboot.txt")), mode='w') as f: 618 f.write("reboot") 619 f.close() 620 print_to_log("SmokeTest: error: name {}, index {}, failed, reboot".format(fail_name_list, fail_idx_list)) 621 enter_shell_cmd("rm -rf /data/* && reboot") 622 reboot_result_list = enter_cmd("hdc list targets", 2) 623 number = 0 624 while args.device_num not in reboot_result_list and number < 15: 625 reboot_result_list = enter_cmd("hdc list targets", 2) 626 number += 1 627 enter_shell_cmd("rm /data/log/hilog/*;hilog -r;hilog -w start -l 400000000 -m none", 1) 628 py_cmd = os.system("python {}\\resource\\capturescreentest.py --config \ 629 {}\\resource\\app_capture_screen_test_config.json --anwser_path {} \ 630 --save_path {}\\reboot --tools_path {} --device_num {} --test_num \"{}\"".format(args.tools_path, 631 args.tools_path, args.anwser_path, args.save_path, args.tools_path, args.device_num, 632 reboot_test_num)) 633 if py_cmd == 0: 634 sys.exit(0) 635 elif py_cmd == 98: 636 sys.exit(98) 637 else: 638 sys.exit(101) 639 else: 640 print_to_log("SmokeTest: error: name {}, index {}, failed".format(fail_name_list, fail_idx_list)) 641 sys_exit() 642 else: 643 print_to_log("SmokeTest: all testcase is ok") 644 print_to_log("SmokeTest: End of check, test succeeded!") 645 sys.exit(0) 646 except: 647 print(traceback.format_exc()) 648 sys_exit() 649