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

Side by Side 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.base.test;
6
7 import android.support.test.InstrumentationRegistry;
8 import android.support.test.uiautomator.UiDevice;
9
10 import org.junit.runners.model.Statement;
11
12 import org.chromium.base.CommandLine;
13 import org.chromium.base.Log;
14 import org.chromium.base.test.util.UrlUtils;
15
16 import java.io.File;
17
18 /**
19 * Statement that captures screenshots if |base| statement fails.
20 *
21 * If --screenshot-path commandline flag is given, this |Statement|
22 * will save a screenshot to the specified path in the case of a test failure.
23 */
24 public class ScreenshotOnFailureStatement extends Statement {
25 private static final String TAG = "ScreenshotOnFail";
26
27 private final Statement mBase;
28
29 public ScreenshotOnFailureStatement(final Statement base) {
30 mBase = base;
31 }
32
33 @Override
34 public void evaluate() throws Throwable {
35 try {
36 mBase.evaluate();
37 } catch (Throwable e) {
38 takeScreenshot();
39 throw e;
40 }
41 }
42
43 private void takeScreenshot() {
44 String screenshotRelativeFilePath = CommandLine.isInitialized()
45 ? CommandLine.getInstance().getSwitchValue("screenshot-file")
46 : null;
47 if (screenshotRelativeFilePath == null) {
48 Log.d(TAG,
49 "Did not save screenshot of failure. Must specify --screensh ot-file "
50 + "command-line flag to enable this feature.");
51 return;
52 }
53
54 UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInst rumentation());
55 File screenshotFile = new File(UrlUtils.getTestFileOutputPath(screenshot RelativeFilePath));
56 File screenshotDir = screenshotFile.getParentFile();
57 if (screenshotDir == null) {
58 Log.d(TAG,
59 String.format(
60 "Failed to create parent directory for %s. Can't sav e screenshot.",
61 screenshotFile));
62 return;
63 }
64 if (!screenshotDir.exists()) {
65 if (!screenshotDir.mkdirs()) {
66 Log.d(TAG,
67 String.format(
68 "Failed to create %s. Can't save screenshot.", s creenshotDir));
69 return;
70 }
71 }
72 Log.d(TAG, String.format("Saving screenshot of test failure, %s", screen shotFile));
73 uiDevice.takeScreenshot(screenshotFile);
74 }
75 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698