# -*- coding: utf-8 -*- # Copyright (c) 2022 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from ast import parse import json import sys import os import time import argparse import re import subprocess def MyPrint(str): print(str) with open(os.path.join(args.save_path, 'shot_test.log'), mode='a', encoding='utf-8') as log_file: console = sys.stdout sys.stdout = log_file print(str) sys.stdout = console log_file.close() def EnterCmd(mycmd, waittime = 0, printresult = 1): if mycmd == "": return with open(os.path.join(args.save_path, 'shot_test.bat'), mode='a', encoding='utf-8') as cmd_file: cmd_file.write(mycmd + '\n') cmd_file.close() with os.popen(mycmd) as p: bf = p._stream.buffer.read() try: result=bf.decode().strip() except UnicodeDecodeError: result=bf.decode('gbk', errors='ignore').strip() if printresult == 1: MyPrint(mycmd) MyPrint(result) sys.stdout.flush() if waittime != 0: time.sleep(waittime) with open(os.path.join(args.save_path, 'shot_test.bat'), mode='a', encoding='utf-8') as cmd_file: cmd_file.write("ping -n {} 127.0.0.1>null\n".format(waittime)) cmd_file.close() return result def connect_to_wifi(tools_path): EnterCmd("hdc_std shell mkdir /data/l2tool", 1) EnterCmd("hdc_std file send {}\\l2tool\\busybox /data/l2tool".format(tools_path), 1) EnterCmd("hdc_std file send {}\\l2tool\\dhcpc.sh /data/l2tool".format(tools_path), 1) EnterCmd("hdc_std file send {}\\l2tool\\entry-debug-rich-signed.hap /data/l2tool".format(tools_path), 1) EnterCmd("hdc_std file send {}\\l2tool\\hostapd.conf /data/l2tool".format(tools_path), 1) EnterCmd("hdc_std file send {}\\l2tool\\iperf /data/l2tool".format(tools_path), 1) EnterCmd("hdc_std file send {}\\l2tool\\p2p_supplicant.conf /data/l2tool".format(tools_path), 1) EnterCmd("hdc_std file send {}\\l2tool\\p2p_supplicant1.conf /data/l2tool".format(tools_path), 1) EnterCmd("hdc_std file send {}\\l2tool\\udhcpd.conf /data/l2tool".format(tools_path), 1) EnterCmd("hdc_std file send {}\\l2tool\\wpa_supplicant.conf /data/l2tool".format(tools_path), 1) EnterCmd("hdc_std shell wpa_supplicant -B -d -i wlan0 -c /data/l2tool/wpa_supplicant.conf", 1) EnterCmd("hdc_std shell chmod 777 ./data/l2tool/busybox", 1) cnt = 2 while cnt: try: MyPrint("hdc_std shell ./data/l2tool/busybox udhcpc -i wlan0 -s /data/l2tool/dhcpc.sh") p = subprocess.check_output("hdc_std shell ./data/l2tool/busybox udhcpc -i wlan0 -s /data/l2tool/dhcpc.sh", timeout=8) MyPrint(p.decode(encoding="utf-8")) with open(os.path.join(args.save_path, 'shot_test.bat'), mode='a', encoding='utf-8') as cmd_file: cmd_file.write('hdc_std shell ./data/l2tool/busybox udhcpc -i wlan0 -s /data/l2tool/dhcpc.sh' + '\n') cmd_file.close() ret_code = 0 except subprocess.TimeoutExpired as time_e: MyPrint(time_e) ret_code = 1 if ret_code == 0: ip = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", p.decode(encoding="utf-8")) MyPrint(ip) if len(ip) <= 0: break if len(re.findall(r'(?>>>>>>>>>>>>>>>>>>>>>>Try again:\n") with open(os.path.join(args.save_path, 'shot_test.bat'), mode='a', encoding='utf-8') as cmd_file: cmd_file.write("\n::::::Last failed, Try again \n") cmd_file.close() EnterCmd("hdc_std shell \"rm /data/log/hilog/*;hilog -r;hilog -w start -l 400000000 -m none\"", 1) if single_app['entry'] != "": EnterCmd(call_app_cmd, 5) MyPrint(single_app['all_actions']) for single_action in single_app['all_actions']: #shot_cmd is stable, different to other cmd,so handle it specialy if type(single_action[1]) == str and single_action[1] == 'shot_cmd': if len(single_action) == 3: pic_name = single_action[2] + ".png" raw_pic_name = single_action[2] + ".pngraw" else: pic_name = single_app['app_name'] + ".png" raw_pic_name = single_app['app_name'] + ".pngraw" EnterCmd(capture_screen_cmd.format(pic_name), 1) EnterCmd(recv_file_cmd.format(pic_name, args.save_path), 1) EnterCmd(recv_file_cmd.format(raw_pic_name, args.save_path), 1) p = EnterCmd("hdc_std shell ls -al /data/screen_test/{}".format(raw_pic_name), 1) no_such = re.findall(r'No such file or directory', p) print(no_such) if type(no_such) == list and len(no_such) > 0 and no_such[0] == 'No such file or directory': MyPrint("ERROR: {} screenshot failed!\n\n".format(raw_pic_name)) MyPrint("End of check, test failed!") sys.exit(255) next_cmd = "" #cmp_cmd-level is stable, different to other cmd,so handle it specialy elif type(single_action[1]) == str and single_action[1] == 'cmp_cmd-level': next_cmd = "" MyPrint(send_file_cmd.format(os.path.join(args.anwser_path, raw_pic_name), "/data/screen_test/train_set")) sys.stdout.flush() EnterCmd(send_file_cmd.format(os.path.join(args.anwser_path, raw_pic_name), "/data/screen_test/train_set")) new_cmp_cmd = cmp_cmd.format(raw_pic_name, raw_pic_name) if len(single_action) == 3: tolerance = single_action[2] else: tolerance = global_pos['cmp_cmd-level'][1] p = EnterCmd(new_cmp_cmd, single_action[0]) num = re.findall(r'[-+]?\d+', p) MyPrint(num) if type(num) == list and len(num) > 0 and int(num[0]) < tolerance: testok = 1 MyPrint("{} screenshot check is ok!\n\n".format(raw_pic_name)) else: testok = -1 MyPrint("{} screenshot check is abnarmal!\n\n".format(raw_pic_name)) sys.stdout.flush() elif type(single_action[1]) == str and single_action[1] == 'recv_log-file': next_cmd = "" if len(single_action) == 3: logfilepath = single_action[2] next_cmd = "hdc_std file recv {} {}".format(logfilepath, args.save_path) elif type(single_action[1]) == str and single_action[1] == 'connect_wifi': next_cmd = "" connect_to_wifi(args.tools_path) #other cmd handle elif type(single_action[1]) == str: if single_action[1] not in single_app.keys(): target_ = global_pos[single_action[1]] else: target_ = single_app[single_action[1]] #this cmd is real cmd,and have a except answer if type(target_[0]) == str: next_cmd = "" p = EnterCmd(target_[0], single_action[0]) result = "".join(p) if len(target_) > 1: findsome = result.find(target_[1], 0, len(result)) if findsome != -1: testok = 1 MyPrint("\"{}\" check execut result is ok, find \"{}\"!\n".format(target_[0], target_[1])) else: testok = -1 MyPrint("\"{}\" check execut result is not ok, not find \"{}\"!\n".format(target_[0], target_[1])) sys.stdout.flush() #this cmd only is a name of x,y postion, to get x,y an click it else: next_cmd = "hdc_std shell uinput -M -m {} {} -c 0".format(target_[0], target_[1]) #uinput x,y postion, to click it else: next_cmd = "hdc_std shell uinput -M -m {} {} -c 0".format(single_action[1], single_action[2]) EnterCmd(next_cmd, single_action[0]) if fail_idx_list.count(idx): fail_idx_list.remove(idx) if fail_name_list.count(single_app['app_name']): fail_name_list.remove(single_app['app_name']) if testok == 1: MyPrint("testcase {}, {} is ok!\n\n".format(idx, single_app['app_name'])) testcnt = 0 elif testok == -1: MyPrint("ERROR:testcase {}, {} is failed!\n\n".format(idx, single_app['app_name'])) fail_idx_list.append(idx) fail_name_list.append(single_app['app_name']) if testcnt == 1 and single_app['app_name'] == 'launcher': MyPrint("ERROR: name {}, index {}, these testcase is failed".format(fail_name_list, fail_idx_list)) MyPrint("End of check, test failed!") sys.stdout.flush() sys.exit(len(fail_idx_list)) testcnt -= 1 else: testcnt = 0 EnterCmd("hdc_std shell hilog -w stop", 1) if len(fail_idx_list) != 0: MyPrint("ERROR: {}, these testcase is failed".format(fail_idx_list)) MyPrint("End of check, test failed!") else: MyPrint("All testcase is ok") MyPrint("End of check, test succeeded!") sys.stdout.flush() sys.exit(len(fail_idx_list))