From 12e9cc4d86e294e14ef8736c08f7eed5d00485a8 Mon Sep 17 00:00:00 2001 From: hzl Date: Tue, 24 May 2016 12:02:59 -0700 Subject: [PATCH] Added the --screenshot-directory option for Android instrumentation tests. --screenshot option was broken for instrumentation tests. In this cl, the option is enabled. With argument --screenshot, the device will take a screenshot whenever an instrumentation test fails. BUG=613405 Review-Url: https://codereview.chromium.org/1998383002 Cr-Original-Commit-Position: refs/heads/master@{#395662} Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src Cr-Mirrored-Commit: 35d5e3dd14ae616db29806f3052b400f2de1945a --- .../instrumentation/instrumentation_test_instance.py | 5 +++++ .../device/local_device_instrumentation_test_run.py | 9 +++++++++ android/test_runner.py | 2 +- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/android/pylib/instrumentation/instrumentation_test_instance.py b/android/pylib/instrumentation/instrumentation_test_instance.py index 502167bc3..1d63f6b9a 100644 --- a/android/pylib/instrumentation/instrumentation_test_instance.py +++ b/android/pylib/instrumentation/instrumentation_test_instance.py @@ -389,6 +389,7 @@ class InstrumentationTestInstance(test_instance.TestInstance): self._driver_apk = None def _initializeTestControlAttributes(self, args): + self._screenshot_dir = args.screenshot_dir self._timeout_scale = args.timeout_scale or 1 def _initializeTestCoverageAttributes(self, args): @@ -430,6 +431,10 @@ class InstrumentationTestInstance(test_instance.TestInstance): def package_info(self): return self._package_info + @property + def screenshot_dir(self): + return self._screenshot_dir + @property def suite(self): return self._suite diff --git a/android/pylib/local/device/local_device_instrumentation_test_run.py b/android/pylib/local/device/local_device_instrumentation_test_run.py index 30118cf67..4b940dbd1 100644 --- a/android/pylib/local/device/local_device_instrumentation_test_run.py +++ b/android/pylib/local/device/local_device_instrumentation_test_run.py @@ -274,6 +274,15 @@ class LocalDeviceInstrumentationTestRun( if any(r.GetType() not in (base_test_result.ResultType.PASS, base_test_result.ResultType.SKIP) for r in results): + if self._test_instance.screenshot_dir: + file_name = '%s-%s.png' % ( + test_display_name, + time.strftime('%Y%m%dT%H%M%S', time.localtime())) + saved_dir = device.TakeScreenshot( + os.path.join(self._test_instance.screenshot_dir, file_name)) + logging.info( + 'Saved screenshot for %s to %s.', + test_display_name, saved_dir) logging.info('detected failure in %s. raw output:', test_display_name) for l in output: logging.info(' %s', l) diff --git a/android/test_runner.py b/android/test_runner.py index 4fc470015..c1f1e2a5b 100755 --- a/android/test_runner.py +++ b/android/test_runner.py @@ -337,7 +337,7 @@ def AddJavaTestOptions(argument_group): help=('Comma-separated list of annotations. Exclude tests with these ' 'annotations.')) argument_group.add_argument( - '--screenshot', dest='screenshot_failures', action='store_true', + '--screenshot-directory', dest='screenshot_dir', help='Capture screenshots of test failures') argument_group.add_argument( '--save-perf-json', action='store_true',