How to use logcat method in Airtest

Best Python code snippet using Airtest

logcat_monitor_test.py

Source: logcat_monitor_test.py Github

copy

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 devil.android import logcat_monitor11from devil.android.sdk 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(actual.group('proc_id'), expected[0])45 self.assertEqual(actual.group('thread_id'), expected[1])46 self.assertEqual(actual.group('log_level'), expected[2])47 self.assertEqual(actual.group('component'), expected[3])48 self.assertEqual(actual.group('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 actual_match.group(0))64 self.assertEqual('error', actual_match.group(1))65 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

Blogs

Check out the latest blogs from LambdaTest on this topic:

QA’s and Unit Testing – Can QA Create Effective Unit Tests

Unit testing is typically software testing within the developer domain. As the QA role expands in DevOps, QAOps, DesignOps, or within an Agile team, QA testers often find themselves creating unit tests. QA testers may create unit tests within the code using a specified unit testing tool, or independently using a variety of methods.

LIVE With Automation Testing For OTT Streaming Devices ????

People love to watch, read and interact with quality content — especially video content. Whether it is sports, news, TV shows, or videos captured on smartphones, people crave digital content. The emergence of OTT platforms has already shaped the way people consume content. Viewers can now enjoy their favorite shows whenever they want rather than at pre-set times. Thus, the OTT platform’s concept of viewing anything, anytime, anywhere has hit the right chord.

Why Agile Is Great for Your Business

Agile project management is a great alternative to traditional methods, to address the customer’s needs and the delivery of business value from the beginning of the project. This blog describes the main benefits of Agile for both the customer and the business.

Options for Manual Test Case Development & Management

The purpose of developing test cases is to ensure the application functions as expected for the customer. Test cases provide basic application documentation for every function, feature, and integrated connection. Test case development often detects defects in the design or missing requirements early in the development process. Additionally, well-written test cases provide internal documentation for all application processing. Test case development is an important part of determining software quality and keeping defects away from customers.

Test strategy and how to communicate it

I routinely come across test strategy documents when working with customers. They are lengthy—100 pages or more—and packed with monotonous text that is routinely reused from one project to another. Yawn once more— the test halt and resume circumstances, the defect management procedure, entrance and exit criteria, unnecessary generic risks, and in fact, one often-used model replicates the requirements of textbook testing, from stress to systems integration.

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:

YouTube

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?

Helpful

NotHelpful