How to use logcat method in Airtest

Best Python code snippet using Airtest Github


Full Screen

1#!/usr/bin/env python2# Copyright 2015 The Chromium Authors. All rights reserved.3# Use of this source code is governed by a BSD-style license that can be4# found in the LICENSE file.5# pylint: disable=protected-access6import itertools7import threading8import unittest9from devil import devil_env10from import logcat_monitor11from import adb_wrapper12with devil_env.SysPath(devil_env.PYMOCK_PATH):13  import mock  # pylint: disable=import-error14def _CreateTestLog(raw_logcat=None):15  test_adb = adb_wrapper.AdbWrapper('0123456789abcdef')16  test_adb.Logcat = mock.Mock(return_value=(l for l in raw_logcat))17  test_log = logcat_monitor.LogcatMonitor(test_adb, clear=False)18  return test_log19class LogcatMonitorTest(unittest.TestCase):20  _TEST_THREADTIME_LOGCAT_DATA = [21      '01-01 01:02:03.456  7890  0987 V LogcatMonitorTest: '22          'verbose logcat monitor test message 1',23      '01-01 01:02:03.457  8901  1098 D LogcatMonitorTest: '24          'debug logcat monitor test message 2',25      '01-01 01:02:03.458  9012  2109 I LogcatMonitorTest: '26          'info logcat monitor test message 3',27      '01-01 01:02:03.459  0123  3210 W LogcatMonitorTest: '28          'warning logcat monitor test message 4',29      '01-01 01:02:03.460  1234  4321 E LogcatMonitorTest: '30          'error logcat monitor test message 5',31      '01-01 01:02:03.461  2345  5432 F LogcatMonitorTest: '32          'fatal logcat monitor test message 6',33      '01-01 01:02:03.462  3456  6543 D LogcatMonitorTest: '34          'last line'35  ]36  def assertIterEqual(self, expected_iter, actual_iter):37    for expected, actual in itertools.izip_longest(expected_iter, actual_iter):38      self.assertIsNotNone(39          expected,40          msg='actual has unexpected elements starting with %s' % str(actual))41      self.assertIsNotNone(42          actual,43          msg='actual is missing elements starting with %s' % str(expected))44      self.assertEqual('proc_id'), expected[0])45      self.assertEqual('thread_id'), expected[1])46      self.assertEqual('log_level'), expected[2])47      self.assertEqual('component'), expected[3])48      self.assertEqual('message'), expected[4])49    with self.assertRaises(StopIteration):50      next(actual_iter)51    with self.assertRaises(StopIteration):52      next(expected_iter)53  @mock.patch('time.sleep', mock.Mock())54  def testWaitFor_success(self):55    test_log = _CreateTestLog(56        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)57    test_log.Start()58    actual_match = test_log.WaitFor(r'.*(fatal|error) logcat monitor.*', None)59    self.assertTrue(actual_match)60    self.assertEqual(61        '01-01 01:02:03.460  1234  4321 E LogcatMonitorTest: '62            'error logcat monitor test message 5',63    self.assertEqual('error',    test_log.Stop()66    test_log.Close()67  @mock.patch('time.sleep', mock.Mock())68  def testWaitFor_failure(self):69    test_log = _CreateTestLog(70        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)71    test_log.Start()72    actual_match = test_log.WaitFor(73        r'.*My Success Regex.*', r'.*(fatal|error) logcat monitor.*')74    self.assertIsNone(actual_match)75    test_log.Stop()76    test_log.Close()77  @mock.patch('time.sleep', mock.Mock())78  def testWaitFor_buffering(self):79    # Simulate an adb log stream which does not complete until the test tells it80    # to. This checks that the log matcher can receive individual lines from the81    # log reader thread even if adb is not producing enough output to fill an82    # entire file io buffer.83    finished_lock = threading.Lock()84    finished_lock.acquire()85    def LogGenerator():86      for line in type(self)._TEST_THREADTIME_LOGCAT_DATA:87        yield line88      finished_lock.acquire()89    test_adb = adb_wrapper.AdbWrapper('0123456789abcdef')90    test_adb.Logcat = mock.Mock(return_value=LogGenerator())91    test_log = logcat_monitor.LogcatMonitor(test_adb, clear=False)92    test_log.Start()93    actual_match = test_log.WaitFor(r'.*last line.*', None)94    finished_lock.release()95    self.assertTrue(actual_match)96    test_log.Stop()97    test_log.Close()98  @mock.patch('time.sleep', mock.Mock())99  def testFindAll_defaults(self):100    test_log = _CreateTestLog(101        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)102    test_log.Start()103    test_log.WaitFor(r'.*last line.*', None)104    test_log.Stop()105    expected_results = [106        ('7890', '0987', 'V', 'LogcatMonitorTest',107         'verbose logcat monitor test message 1'),108        ('8901', '1098', 'D', 'LogcatMonitorTest',109         'debug logcat monitor test message 2'),110        ('9012', '2109', 'I', 'LogcatMonitorTest',111         'info logcat monitor test message 3'),112        ('0123', '3210', 'W', 'LogcatMonitorTest',113         'warning logcat monitor test message 4'),114        ('1234', '4321', 'E', 'LogcatMonitorTest',115         'error logcat monitor test message 5'),116        ('2345', '5432', 'F', 'LogcatMonitorTest',117         'fatal logcat monitor test message 6')]118    actual_results = test_log.FindAll(r'\S* logcat monitor test message \d')119    self.assertIterEqual(iter(expected_results), actual_results)120    test_log.Close()121  @mock.patch('time.sleep', mock.Mock())122  def testFindAll_defaults_miss(self):123    test_log = _CreateTestLog(124        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)125    test_log.Start()126    test_log.WaitFor(r'.*last line.*', None)127    test_log.Stop()128    expected_results = []129    actual_results = test_log.FindAll(r'\S* nothing should match this \d')130    self.assertIterEqual(iter(expected_results), actual_results)131    test_log.Close()132  @mock.patch('time.sleep', mock.Mock())133  def testFindAll_filterProcId(self):134    test_log = _CreateTestLog(135        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)136    test_log.Start()137    test_log.WaitFor(r'.*last line.*', None)138    test_log.Stop()139    actual_results = test_log.FindAll(140        r'\S* logcat monitor test message \d', proc_id=1234)141    expected_results = [142        ('1234', '4321', 'E', 'LogcatMonitorTest',143         'error logcat monitor test message 5')]144    self.assertIterEqual(iter(expected_results), actual_results)145    test_log.Close()146  @mock.patch('time.sleep', mock.Mock())147  def testFindAll_filterThreadId(self):148    test_log = _CreateTestLog(149        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)150    test_log.Start()151    test_log.WaitFor(r'.*last line.*', None)152    test_log.Stop()153    actual_results = test_log.FindAll(154        r'\S* logcat monitor test message \d', thread_id=2109)155    expected_results = [156        ('9012', '2109', 'I', 'LogcatMonitorTest',157         'info logcat monitor test message 3')]158    self.assertIterEqual(iter(expected_results), actual_results)159    test_log.Close()160  @mock.patch('time.sleep', mock.Mock())161  def testFindAll_filterLogLevel(self):162    test_log = _CreateTestLog(163        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)164    test_log.Start()165    test_log.WaitFor(r'.*last line.*', None)166    test_log.Stop()167    actual_results = test_log.FindAll(168        r'\S* logcat monitor test message \d', log_level=r'[DW]')169    expected_results = [170        ('8901', '1098', 'D', 'LogcatMonitorTest',171         'debug logcat monitor test message 2'),172        ('0123', '3210', 'W', 'LogcatMonitorTest',173         'warning logcat monitor test message 4')174    ]175    self.assertIterEqual(iter(expected_results), actual_results)176    test_log.Close()177  @mock.patch('time.sleep', mock.Mock())178  def testFindAll_filterComponent(self):179    test_log = _CreateTestLog(180        raw_logcat=type(self)._TEST_THREADTIME_LOGCAT_DATA)181    test_log.Start()182    test_log.WaitFor(r'.*last line.*', None)183    test_log.Stop()184    actual_results = test_log.FindAll(r'.*', component='LogcatMonitorTest')185    expected_results = [186        ('7890', '0987', 'V', 'LogcatMonitorTest',187         'verbose logcat monitor test message 1'),188        ('8901', '1098', 'D', 'LogcatMonitorTest',189         'debug logcat monitor test message 2'),190        ('9012', '2109', 'I', 'LogcatMonitorTest',191         'info logcat monitor test message 3'),192        ('0123', '3210', 'W', 'LogcatMonitorTest',193         'warning logcat monitor test message 4'),194        ('1234', '4321', 'E', 'LogcatMonitorTest',195         'error logcat monitor test message 5'),196        ('2345', '5432', 'F', 'LogcatMonitorTest',197         'fatal logcat monitor test message 6'),198        ('3456', '6543', 'D', 'LogcatMonitorTest',199         'last line')200    ]201    self.assertIterEqual(iter(expected_results), actual_results)202    test_log.Close()203if __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?