表格、恢复出厂、正则
import os import re import subprocess import sys import uiautomator2 as u2 import time from pandas.tests.io.excel.test_xlrd import xlrd, xlwt from xlutils.copy import copy def set_up(): d = u2.connect(device) time.sleep(1) d.screen_on() d.shell('input keyevent 82') d.shell('input keyevent 3') def exists(path): is_exists = os.path.exists(path) if is_exists: return True else: os.mkdir(path) # 创建结果文件夹 def create_xls(xls_name): # 结果文件夹 exists(os.path.join(os.getcwd(), 'Result')) result_dir = os.path.join(os.getcwd(), 'Result') # print(result_dir) # 判断目标结果excel文件是否存在,若无创建新文件 result_path = os.path.join(result_dir, xls_name) if not os.path.exists(result_path): # 创建xls表格 workbook = xlwt.Workbook(encoding='utf-8') # 创建一个worksheet worksheet = workbook.add_sheet('result') worksheet.col(0).width = 40 * 300 # Set the column width worksheet.col(1).width = 40 * 200 # Set the column width worksheet.col(2).width = 40 * 200 # Set the column width worksheet.col(3).width = 40 * 200 # Set the column width # 写入excel # 参数对应 行, 列, 值 worksheet.write(0, 0, 'country') worksheet.write(0, 1, 'camera_info') worksheet.write(0, 2, 'storage_used') worksheet.write(0, 3, 'result') # worksheet.write(0, 3, 'rate') workbook.save(result_path) else: pass return result_path def write_excel_xls_append(path, country=[], camera_info=[], storage_used=[], result=[]): global worksheet workbook = xlrd.open_workbook(path, formatting_info=True) try: worksheet = workbook.sheet_by_name('result') except: print('xls文件sheet名称错误,默认名称是result,请检查') # 在写入之前需要获取已经写入的行数 old_nrows = worksheet.nrows # print(old_nrows) new_workbook = copy(workbook) # 将xlrd对象拷贝转化为xlwt对象 new_worksheet = new_workbook.get_sheet(0) # 获取转化后工作簿中的第一个表格 # 开始进行写操作 new_worksheet.write(old_nrows + 1, 0, country) new_worksheet.write(old_nrows + 1, 1, camera_info) new_worksheet.write(old_nrows + 1, 2, storage_used) new_worksheet.write(old_nrows + 1, 3, result) new_workbook.save(path) # 到设置里面的平板信息 def open_to_the_camera(): for i in range(5): d.app_start('com.android.settings') time.sleep(1) if d(resourceId='android:id/button2').wait(timeout=3): d(resourceId='android:id/button2').click() time.sleep(4) d(scrollable=True).fling.toEnd() time.sleep(1) d.dump_hierarchy() try: d.xpath( '//*[@resource-id="com.android.settings:id/recycler_view"]/android.widget.LinearLayout[' '9]/android.widget.ImageView[1]').click() time.sleep(1) d.xpath( '//*[@resource-id="com.android.settings:id/recycler_view"]/android.widget.LinearLayout[' '1]/android.widget.LinearLayout[1]/android.widget.ImageView[1]').click() time.sleep(4) d(scrollable=True).fling.toEnd() time.sleep(1) break except: print('未能找到设置里面System的xpath,再次执行该操作') continue # 到设置里面的存储界面 def open_to_the_storage(): os.system('adb -s %s shell input keyevent 82' % device) os.system('adb -s %s shell input keyevent 82' % device) for i in range(5): try: d.app_start('com.android.settings') time.sleep(4) d(scrollable=True).fling.toEnd() time.sleep(1) d.dump_hierarchy() d.xpath( '//*[@resource-id="com.android.settings:id/recycler_view"]/android.widget.LinearLayout[' '7]/android.widget.ImageView[1]').click() time.sleep(2) break except: print('未能找到设置里面Storage的xpath,再次执行该操作') continue # 获取camera里面的信息并判断 def camera_text(): d.dump_hierarchy() for i in range(5): for j in range(5): if not d.xpath('//*[@resource-id="com.android.settings:id/recycler_view"]/android.widget.LinearLayout[' '9]/android.widget.RelativeLayout[1]/android.widget.TextView[2]').exists: time.sleep(2) else: break try: for i in d.xpath( '//*[@resource-id="com.android.settings:id/recycler_view"]/android.widget.LinearLayout[' '9]/android.widget.RelativeLayout[1]/android.widget.TextView[2]').all(): text = i.text print(text) # 正则表达式 排除里面没有特殊字符+- 只有8和5 result_1 = re.findall('[\+,\-,=,\*,#,@,!,\$,%,\^,~,\(,\),`]', text) if len(result_1) != 0: print('有特殊字符') result = 'False' return text, result else: result = re.findall( '([\+,\-,=,\*,#,@,!,\$,%,\^,~,\(,\),\.`,\/]*\d+[\+,\-,=,\*,#,@,!,\$,%,\^,~,\(,\),\.`,\/]*)+', text) print('Camera_text', result) if len(result) != 2: print("数字不是默认的2个数字") result = 'False' return text, result for i in range(len(result)): if '8' != result[i] and '13' != result[i]: # print('Camera_text', result) print("数字不是默认值") result = 'False' return text, result else: result = "True" return text, result except: print('读取Camera信息失败,再次操作') continue # 获取stroage里面的信息并判断 def storage_text(): for i in range(5): try: d.dump_hierarchy() for i in range(10): if d.xpath('//*[@resource-id="com.android.settings:id/recycler_view"]/android.widget.LinearLayout[' '1]/android.widget.LinearLayout[1]/android.widget.TextView[1]').exists: break else: time.sleep(2) for i in d.xpath( '//*[@resource-id="com.android.settings:id/recycler_view"]/android.widget.LinearLayout[' '1]/android.widget.LinearLayout[1]/android.widget.TextView[1]').all(): text_use = i.text print('已经使用', text_use) for i in d.xpath( '//*[@resource-id="com.android.settings:id/recycler_view"]/android.widget.LinearLayout[' '1]/android.widget.LinearLayout[1]/android.widget.TextView[2]').all(): text = i.text print('内存', text) shuzi = re.findall('\d+', str(text)) # print(shuzi) result = "" if "64" in shuzi: info = d.xpath( '//*[@resource-id="com.android.settings:id/donut"]').info result_use = info["contentDescription"] print(result_use) # print(result_use) if '31' not in result_use: print('存储空间使用不是默认的值31%') result = "False" else: result = "True" elif "128" in shuzi: info = d.xpath( '//*[@resource-id="com.android.settings:id/donut"]').info # print(info) result_use = info["contentDescription"] print(result_use) if '16' not in result_use: print('存储空间使用不是默认的值16%') result = "False" else: result = "True" else: info = d.xpath( '//*[@resource-id="com.android.settings:id/donut"]').info info = dict(info) result_use = info["contentDescription"] print(result_use) # 阿拉伯语无法数字判断 默认都false # result_use = re.findall('\w[\%]+', str(info)) # result_use = "".join(result_use).encode("gbk", 'ignore').decode("gbk", "ignore") result = "False" # print('阿拉博语言存储空间:', info["contentDescription"]) # if '16%' and '31%' not in result_use: # print(result_use, '存储空间使用不是默认的值16%/31%') result_use = ''.join(str(s) for s in result_use) return result_use, result except: print('读取storage信息失败') continue # 进入工厂模式 def enter_factory(): d = u2.connect(device) os.system('adb -s %s shell input keyevent 82' % device) os.system('adb -s %s shell input keyevent 82' % device) for i in range(5): try: d.app_start('com.android.settings') time.sleep(4) d.xpath( '//*[@resource-id="android:id/action_bar"]/android.widget.LinearLayout[1]/android.widget.TextView').click() time.sleep(2) d.send_keys('####6030#') for i in range(5): if d(text='AE United Arab Emirates').exists: time.sleep(1) break else: time.sleep(2) break except: print('进入工厂模式失败') continue # 切换国家码 def change_code_list(start, end): d.dump_hierarchy() if d(className='android.widget.RadioButton', index=start).exists and d(className='android.widget.RadioButton', index=end).exists: d(className='android.widget.RadioButton', index=start).click() country = d(className='android.widget.RadioButton', index=start).info["text"] time.sleep(1) if d(resourceId='com.lenovo.EngineeringCode:id/ok').exists: d(resourceId='com.lenovo.EngineeringCode:id/ok').click() time.sleep(1) if d(resourceId='android:id/button1').wait(timeout=2): d(resourceId='android:id/button1').click() time.sleep(260) for i in range(5): try: # 等待恢复出厂设置 os.system("adb -s %s root" % device) os.system("adb -s %s remount" % device) os.system("adb -s %s shell settings put secure user_setup_complete 1" % device) os.system("adb -s %s shell settings put global device_provisioned 1" % device) os.system("adb -s %s reboot" % device) time.sleep(85) # 重启 os.system('adb -s %s shell input keyevent 82' % device) os.system('adb -s %s shell input keyevent 82' % device) os.system('adb -s %s shell input keyevent 82' % device) os.system('adb -s %s shell input keyevent 82' % device) os.system('adb -s %s shell settings put system screen_off_timeout 600000' % device) os.system('adb -s %s shell settings put system screen_off_timeout 600000' % device) os.system('adb -s %s shell input keyevent 3' % device) os.system('adb -s %s shell input keyevent 3' % device) set_up() break except: print('开机失败重新再次尝试') continue print(country) main() try: write_excel_xls_append(path=result_path, country=country, camera_info=camera_info, storage_used=storage_used, result=result) except: print('请勿在执行过程中打开结果文件') def main(): global camera_info, storage_used, result open_to_the_camera() time.sleep(2) result_1 = camera_text() time.sleep(2) open_to_the_storage() time.sleep(4) result_2 = storage_text() result_3 = result_1 + result_2 # print(result_3) # print(result_3) # 结果默认为True 有一个错误即为错 result = "True" for i in range(len(result_3)): if "False" in result_3[i]: result = "False" elif "/" in result_3[i]: camera_info = result_3[i] elif "%" in result_3[i]: storage_used = result_3[i] # 排除没有百分比的特殊情况 if "%" not in result_2: storage_used = result_2 # print(storage_used) for m in range(len(storage_used)): if storage_used[m] != 'True' and storage_used[m] != 'False': storage_used = storage_used[m] break # 排除没有/ 的情况 if "/" not in result_1: camera_info = result_1 # print(camera_info) for m in range(len(camera_info)): if camera_info[m] != 'True' and camera_info[m] != 'False': camera_info = camera_info[m] # print(camera_info) break return camera_info, storage_used, result # 选择设备的id def device_input(): device_id = input('请输入需要进行操作的设备id:') return device_id if __name__ == '__main__': result_path = create_xls("CheckInDifferentCountry.xls") device = device_input() d = u2.connect(device) time.sleep(1) code_1 = 1 code_2 = 15 code_3 = 30 code_4 = 45 code_5 = 59 # AR -> EC Ecuador for i in range(14): enter_factory() while True: if d(text='AE United Arab Emirates').exists and d(text='EC Ecuador').exists: if code_1 < 15: change_code_list(code_1, 15) code_1 += 1 break else: os.system('adb -s %s shell input swipe 798 327 781 248' % device) print('第一页国家码结束') # EE -> IT Italy for i in range(14): enter_factory() while True: if d(text='EC Ecuador').exists and d(text='IT Italy').exists: if code_2 < 30: change_code_list(code_2, 30) code_2 += 1 break else: os.system('adb -s %s shell input swipe 798 327 781 248' % device) print('第二页国家码结束') # Japan -> PT Portugal for i in range(14): enter_factory() while True: if d(text='IT Italy').exists and d(text='PT Portugal').exists: if code_3 < 45: change_code_list(code_3, 45) code_3 += 1 break else: os.system('adb -s %s shell input swipe 798 327 781 248' % device) print('第三页国家码结束') # RO Romanial-> US United States of America for i in range(14): enter_factory() while True: # ZA South Africa if d(text='PT Portugal').exists and d(text='US United States of America').exists: if code_4 < 60: change_code_list(code_4, 60) code_4 += 1 break else: os.system('adb -s %s shell input swipe 798 327 781 248' % device) # 点击最后二个国家码 for i in range(2): enter_factory() while True: if d(text='ZA South Africa').exists and d(text='VN Vietnam').exists: change_code_list(code_5, 61) code_5 += 1 break else: os.system('adb -s %s shell input swipe 798 327 781 248' % device) # 点击第一个阿拉伯 enter_factory() if d(text='AE United Arab Emirates').exists: change_code_list(0, 60) print('第四页国家码结束')