How to use test_logcat method in Airtest

Best Python code snippet using Airtest Github


Full Screen

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 ='''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()...

Full Screen

Full Screen Github


Full Screen

...22 self.assertEqual(23 Python2(, timeout=10).run(self.device),24 'timeout'25 )26 def test_logcat(self):27 self.assertEqual(28 Python2(, logcat_regex='').run(self.device),29 'logcat'30 )31if __name__ == '__main__':...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:


You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Airtest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?