Add an API for taking screenshot in AndroidDevice. (#670)
Force merging since travis CI actually passed: https://travis-ci.org/github/google/mobly/builds/671961274
diff --git a/mobly/controllers/android_device.py b/mobly/controllers/android_device.py
index 7557ae2..2a38e0e 100644
--- a/mobly/controllers/android_device.py
+++ b/mobly/controllers/android_device.py
@@ -77,6 +77,9 @@
# Default Timeout to wait for boot completion
DEFAULT_TIMEOUT_BOOT_COMPLETION_SECOND = 15 * 60
+# Timeout for the adb command for taking a screenshot
+TAKE_SCREENSHOT_TIMEOUT_SECOND = 10
+
# Aliases of error types for backward compatibility.
Error = errors.Error
DeviceError = errors.DeviceError
@@ -1002,6 +1005,26 @@
self.log.debug('Bugreport taken at %s.', full_out_path)
return full_out_path
+ def take_screenshot(self, path):
+ """Takes a screenshot of the device.
+
+ Args:
+ path: string, full path to the directory to save in.
+
+ Returns:
+ string, full path to the screenshot file on the host.
+ """
+ filename = self.generate_filename('screenshot', extension_name='png')
+ device_path = os.path.join('/storage/emulated/0/', filename)
+ self.adb.shell(['screencap', '-p', device_path],
+ timeout=TAKE_SCREENSHOT_TIMEOUT_SECOND)
+ utils.create_dir(path)
+ self.adb.pull([device_path, path])
+ pic_path = os.path.join(path, filename)
+ self.log.debug('Screenshot taken, saved on the host: %s', pic_path)
+ self.adb.shell(['rm', device_path])
+ return pic_path
+
def run_iperf_client(self, server_host, extra_args=''):
"""Start iperf client on the device.
diff --git a/tests/mobly/controllers/android_device_test.py b/tests/mobly/controllers/android_device_test.py
index 22aa17c..4cd86c9 100755
--- a/tests/mobly/controllers/android_device_test.py
+++ b/tests/mobly/controllers/android_device_test.py
@@ -799,6 +799,21 @@
@mock.patch('mobly.controllers.android_device_lib.adb.AdbProxy',
return_value=mock_android_device.MockAdbProxy('1'))
+ @mock.patch('mobly.utils.create_dir')
+ @mock.patch('mobly.logger.get_log_file_timestamp')
+ def test_AndroidDevice_take_screenshot(self, get_log_file_timestamp_mock,
+ create_dir_mock, MockAdbProxy):
+ get_log_file_timestamp_mock.return_value = '07-22-2019_17-53-34-450'
+ mock_serial = '1'
+ ad = android_device.AndroidDevice(serial=mock_serial)
+ full_pic_path = ad.take_screenshot(self.tmp_dir)
+ self.assertEqual(
+ full_pic_path,
+ os.path.join(self.tmp_dir,
+ 'screenshot,1,fakemodel,07-22-2019_17-53-34-450.png'))
+
+ @mock.patch('mobly.controllers.android_device_lib.adb.AdbProxy',
+ return_value=mock_android_device.MockAdbProxy('1'))
@mock.patch('mobly.controllers.android_device_lib.fastboot.FastbootProxy',
return_value=mock_android_device.MockFastbootProxy('1'))
@mock.patch('mobly.utils.start_standing_subprocess',
@@ -1099,8 +1114,8 @@
@mock.patch.object(logcat.Logcat, '_open_logcat_file')
def test_AndroidDevice_handle_usb_disconnect(self, open_logcat_mock,
stop_proc_mock,
- start_proc_mock, FastbootProxy,
- MockAdbProxy):
+ start_proc_mock,
+ FastbootProxy, MockAdbProxy):
class MockService(base_service.BaseService):
def __init__(self, device, configs=None):
self._alive = False
@@ -1143,9 +1158,9 @@
return_value='process')
@mock.patch('mobly.utils.stop_standing_subprocess')
@mock.patch.object(logcat.Logcat, '_open_logcat_file')
- def test_AndroidDevice_handle_reboot(self, open_logcat_mock, stop_proc_mock,
- start_proc_mock, FastbootProxy,
- MockAdbProxy):
+ def test_AndroidDevice_handle_reboot(self, open_logcat_mock,
+ stop_proc_mock, start_proc_mock,
+ FastbootProxy, MockAdbProxy):
class MockService(base_service.BaseService):
def __init__(self, device, configs=None):
self._alive = False