Best Python code snippet using Airtest
getlog.py
Source:getlog.py
1# -*- coding: utf-8 -*-2# #!/usr/bin/env python3__author__ = 'Administrator'4import sys,os,time5import re6class getAppLog():7 ''' å®ä¹åºç±» :8 å¤æandriod_homeæ¯å¦åå¨ï¼ç»§èå¤æadbå½ä»¤æ¯å¦åå¨9 å¤ædeviceæ¯å¦å¨çº¿ï¼å¦æå¨çº¿ï¼è·ådevicesä¿¡æ¯10 å¤æadb logcatæ¯å¦å¯ç¨11 æ¸
空ç¼åæ¥å¿12 è°ç¨adb logcatæè·æ¥å¿ï¼å¹¶éå®åå°æå®æ件13 å¦ææ¥å¿æ件è¶
è¿20Mï¼å¤ä»½åæ¥å¿æ件14 '''15 def def_log_file(self,log_name=None):16 ''' å®ä¹ æ¥å¿æ件 '''17 PATH = lambda p: os.path.abspath(os.path.join(os.path.dirname(__file__), p))18 log_name = 'test_logcat.txt' if log_name == None else log_name19 file_path = PATH(log_name)20 return file_path21 def envCheck(self):22 ''' æ ¡éª andriod sdk '''23 print os.environ24 if "ANDROID_HOME" in os.environ:25 rootDir = os.path.join(os.environ["ANDROID_HOME"], "platform-tools")26 for path, subdir, files in os.walk(rootDir):27 if "adb.exe" in files:28 return os.path.join(path, "adb.exe")29 else:30 print '\nadb.exe does not exist!\n',31 time.sleep(3)32 else:33 print "\nANDROID_HOME not exist! Please setup andriod SDK and set the environment variable ANDROID_HOME. \n",34 time.sleep(3)35 def get_device_id(self):36 ''' è·å deviceIdï¼å¹¶ä½å®¹éæ ¡éª '''37 #å°è¯åæ¢ãå¯å¨adb-serverï¼æ¹ä¾¿è°ç¨adbè·åä¿¡æ¯38 os.popen('adb kill-server')39 time.sleep(3)40 os.popen('adb start-server')41 time.sleep(5)42 out = os.popen("adb devices").read()43 if out == '':44 print '\n[ERROR] Terminal device has a problem, the program exits, replace the terminal equipment!\n'45 time.sleep(3)46 exit()47 elif out.startswith('error'):48 print '\n[ERROR] Get devices error,program exits!\n'49 time.sleep(3)50 exit()51 elif out.split('\n')[1].strip().endswith('unauthorized') and str(out).count('device') == 1:52 print '\n[ERROR] Device unauthorized,program exits!\n'53 time.sleep(3)54 exit()55 elif out.split('\n')[0].strip().endswith('attached') and str(out).count('device') == 1 and len(out.split('\n')) <= 3:56 print '\n[ERROR] Not get list of devices,program exits!\n'57 time.sleep(3)58 exit()59 else:60 deviceId = out.split('\n')[1].split('\t')[0]61 return deviceId62 def check_log_is_enable(self):63 ''' æ ¡éªææºè®¾å¤æ¯å¦æ¯ælogcatå½ä»¤ '''64 log_cat = os.popen("adb logcat -g 20").read()65 match = re.search(r'''Unable to open log device '/dev/log/main': No such file or directory''',log_cat)66 if match:67 print '\n[ERROR] ' + log_cat + ' \n'68 time.sleep(3)69 sys.exit()70 def get_log_to_file(self,deviceid,file_path):71 ''' è°ç¨logcatè·åæ¥å¿å°æ件72 adb -s deviceid logcat -c ï¼æ¸
é¤LOGCATçç¼åï¼73 adb -d -s deviceid logcat *:W >test_logcat.txt74 '''75 if deviceid != '':76 clear_buffer = 'adb -s '+ deviceid +' logcat -c '77 get_log = 'adb -d -s ' + deviceid + ' logcat -v time -s *:W >>' + file_path78 if ' ' in file_path:79 print '\n ' + file_path + ': The path contains spaces,program exits!\n'80 time.sleep(3)81 sys.exit()82 #æ§è¡å
·ä½çå½ä»¤83 os.system(clear_buffer)84 time.sleep(1)85 os.system(get_log)86 else:87 print '\ndeviceid not exist!\n'88 time.sleep(3)89 sys.exit()90 def log_file_size(self,file_path):91 ''' å¦ælogæ件è¿å¤§ï¼å¤ä»½å¹¶éæ°å¯ç¨ä¸ä¸ªæ¥å¿è¿è¡è®°å½ '''92 if os.path.exists(file_path):93 file_size = os.path.getsize(file_path)94 file_name = file_path.split("/")[-1].split('.')[0]95 file_Suffix =file_path.split("/")[-1].split('.')[-1]96 timeStamp = time.strftime('%Y_%m_%d_%H_%M_%S')97 new_name = file_name+'_'+ timeStamp + file_Suffix98 if file_size >= 20971520:99 os.rename(file_path, new_name)100if __name__ == '__main__':101 print '-' * 75102 log = getAppLog()103 log_file = log.def_log_file()104 log.log_file_size(log_file)105 log.envCheck()106 device_id = log.get_device_id()107 log.check_log_is_enable()...
test_python2.py
Source:test_python2.py
...22 self.assertEqual(23 Python2(self.file.name, timeout=10).run(self.device),24 'timeout'25 )26 def test_logcat(self):27 self.assertEqual(28 Python2(self.file.name, logcat_regex='').run(self.device),29 'logcat'30 )31if __name__ == '__main__':...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!