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