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