Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2679)

Unified Diff: base/test/android/javatests/src/org/chromium/base/test/ScreenshotOnFailureStatement.java

Issue 2854823007: Move screenshot capture to Java-side. (Closed)
Patch Set: Move screenshot capture to Java-side. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/test/android/javatests/src/org/chromium/base/test/ScreenshotOnFailureStatement.java
diff --git a/base/test/android/javatests/src/org/chromium/base/test/ScreenshotOnFailureStatement.java b/base/test/android/javatests/src/org/chromium/base/test/ScreenshotOnFailureStatement.java
new file mode 100644
index 0000000000000000000000000000000000000000..ad22b4a2960864b3de5547fe99e034398152c8d7
--- /dev/null
+++ b/base/test/android/javatests/src/org/chromium/base/test/ScreenshotOnFailureStatement.java
@@ -0,0 +1,66 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.base.test;
+
+import android.support.test.InstrumentationRegistry;
+import android.support.test.uiautomator.UiDevice;
+
+import org.junit.runners.model.Statement;
+
+import org.chromium.base.CommandLine;
+import org.chromium.base.Log;
+import org.chromium.base.test.util.UrlUtils;
+
+import java.io.File;
+
+/**
+ * Statement that captures screenshots if |base| statement fails.
+ *
+ * If --screenshot-dir commandline flag is given, this statement
+ * will save a screenshot named failure.png to
+ * |UrlUtils.getIsolatedTestFilePath| under the subdirectory specified
+ * in the case of a test failure.
+ */
+public class ScreenshotOnFailureStatement extends Statement {
+ private static final String TAG = "ScreenshotOnFail";
+
+ private static final String SCREENSHOT_FILENAME = "failure.png";
+
+ private final Statement mBase;
+
+ public ScreenshotOnFailureStatement(final Statement base) {
+ mBase = base;
+ }
+
+ @Override
+ public void evaluate() throws Throwable {
+ try {
+ mBase.evaluate();
+ } catch (Throwable e) {
+ String subdir = CommandLine.isInitialized()
jbudorick 2017/05/05 01:21:58 This may be more readable/cleaner if you split the
mikecase (-- gone --) 2017/05/05 18:09:19 Done
+ ? CommandLine.getInstance().getSwitchValue("screenshot-dir")
+ : null;
+ if (subdir == null) {
+ Log.d(TAG,
+ "Did not save screenshot of failure. Must specify --screenshot-dir "
+ + "command-line flag to enable this feature.");
+ throw e;
+ }
+
+ UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
jbudorick 2017/05/05 01:21:58 I assume this was the only way to do this?
mikecase (-- gone --) 2017/05/05 18:09:19 I looked at basically every other option. So there
+ File path = new File(UrlUtils.getIsolatedTestFilePath(subdir));
jbudorick 2017/05/05 01:21:58 While I'm ok with using the isolated test file pat
mikecase (-- gone --) 2017/05/05 18:09:19 Added UrlUtils.getTestFileOutputPath
+ if (!path.exists()) {
+ if (!path.mkdirs()) {
+ Log.d(TAG, String.format("Failed to create %s. Can't save screenshot.", path));
+ throw e;
+ }
+ }
+ File file = new File(path + "/" + SCREENSHOT_FILENAME);
+ Log.d(TAG, String.format("Saving screenshot of test failure, %s", file));
+ uiDevice.takeScreenshot(file);
+ throw e;
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698