OLD | NEW |
---|---|
(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-dir commandline flag is given, this statement | |
22 * will save a screenshot named failure.png to | |
23 * |UrlUtils.getIsolatedTestFilePath| under the subdirectory specified | |
24 * in the case of a test failure. | |
25 */ | |
26 public class ScreenshotOnFailureStatement extends Statement { | |
27 private static final String TAG = "ScreenshotOnFail"; | |
28 | |
29 private static final String SCREENSHOT_FILENAME = "failure.png"; | |
30 | |
31 private final Statement mBase; | |
32 | |
33 public ScreenshotOnFailureStatement(final Statement base) { | |
34 mBase = base; | |
35 } | |
36 | |
37 @Override | |
38 public void evaluate() throws Throwable { | |
39 try { | |
40 mBase.evaluate(); | |
41 } catch (Throwable e) { | |
42 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
| |
43 ? CommandLine.getInstance().getSwitchValue("screenshot-dir") | |
44 : null; | |
45 if (subdir == null) { | |
46 Log.d(TAG, | |
47 "Did not save screenshot of failure. Must specify --scre enshot-dir " | |
48 + "command-line flag to enable this feature."); | |
49 throw e; | |
50 } | |
51 | |
52 UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.get Instrumentation()); | |
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
| |
53 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
| |
54 if (!path.exists()) { | |
55 if (!path.mkdirs()) { | |
56 Log.d(TAG, String.format("Failed to create %s. Can't save sc reenshot.", path)); | |
57 throw e; | |
58 } | |
59 } | |
60 File file = new File(path + "/" + SCREENSHOT_FILENAME); | |
61 Log.d(TAG, String.format("Saving screenshot of test failure, %s", fi le)); | |
62 uiDevice.takeScreenshot(file); | |
63 throw e; | |
64 } | |
65 } | |
66 } | |
OLD | NEW |