Support custom filename in `AndroidDevice.take_screenshot` (#730)
diff --git a/mobly/controllers/android_device.py b/mobly/controllers/android_device.py
index e448f21..dc16180 100644
--- a/mobly/controllers/android_device.py
+++ b/mobly/controllers/android_device.py
@@ -1010,16 +1010,17 @@
self.log.debug('Bugreport taken at %s.', full_out_path)
return full_out_path
- def take_screenshot(self, destination):
+ def take_screenshot(self, destination, prefix='screenshot'):
"""Takes a screenshot of the device.
Args:
destination: string, full path to the directory to save in.
+ prefix: string, prefix file name of the screenshot.
Returns:
string, full path to the screenshot file on the host.
"""
- filename = self.generate_filename('screenshot', extension_name='png')
+ filename = self.generate_filename(prefix, extension_name='png')
device_path = os.path.join('/storage/emulated/0/', filename)
self.adb.shell(['screencap', '-p', device_path],
timeout=TAKE_SCREENSHOT_TIMEOUT_SECOND)
diff --git a/tests/mobly/controllers/android_device_test.py b/tests/mobly/controllers/android_device_test.py
index 7b337aa..4d17083 100755
--- a/tests/mobly/controllers/android_device_test.py
+++ b/tests/mobly/controllers/android_device_test.py
@@ -856,6 +856,26 @@
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.create_dir')
+ @mock.patch('mobly.logger.get_log_file_timestamp')
+ def test_AndroidDevice_take_screenshot_with_prefix(
+ self, get_log_file_timestamp_mock, create_dir_mock,
+ FastbootProxy, 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, 'page_a')
+
+ self.assertEqual(
+ full_pic_path,
+ os.path.join(self.tmp_dir,
+ 'page_a,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', return_value='process')
@mock.patch('mobly.utils.stop_standing_subprocess')
def test_AndroidDevice_change_log_path(self, stop_proc_mock, start_proc_mock,