|
| 1 | +package com.sample.browserstack.samplecalculator; |
| 2 | + |
| 3 | +import android.graphics.Bitmap; |
| 4 | +import android.os.Build; |
| 5 | +import android.os.Environment; |
| 6 | +import android.util.Log; |
| 7 | + |
| 8 | +import androidx.test.core.app.ApplicationProvider; |
| 9 | +import androidx.test.runner.screenshot.BasicScreenCaptureProcessor; |
| 10 | +import androidx.test.runner.screenshot.ScreenCapture; |
| 11 | +import androidx.test.runner.screenshot.Screenshot; |
| 12 | + |
| 13 | +import java.io.File; |
| 14 | +import java.io.IOException; |
| 15 | + |
| 16 | +public class ScreenshotUtils { |
| 17 | + |
| 18 | + private final String methodName; |
| 19 | + private final String className; |
| 20 | + private final CustomScreencaptureProcessor customProcesssor; |
| 21 | + |
| 22 | + ScreenshotUtils() { |
| 23 | + StackTraceElement testClass = findTestClassTraceElement(Thread.currentThread().getStackTrace()); |
| 24 | + className = testClass.getClassName().replaceAll("[^A-Za-z0-9._-]", "_"); |
| 25 | + methodName = testClass.getMethodName(); |
| 26 | + customProcesssor = new CustomScreencaptureProcessor(); |
| 27 | + } |
| 28 | + |
| 29 | + public void captureScreenshot(String screenshotName) { |
| 30 | + ScreenCapture capture = Screenshot.capture(); |
| 31 | + capture.setFormat(Bitmap.CompressFormat.PNG); |
| 32 | + capture.setName(screenshotName); |
| 33 | + |
| 34 | + try { |
| 35 | + customProcesssor.process(capture); |
| 36 | + } catch (IOException e) { |
| 37 | + e.printStackTrace(); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /* |
| 42 | + Determine the className and testName of the current test being executed from the stacktrace. |
| 43 | + */ |
| 44 | + private StackTraceElement findTestClassTraceElement(StackTraceElement[] trace) { |
| 45 | + for(int i = trace.length - 1; i >= 0; --i) { |
| 46 | + StackTraceElement element = trace[i]; |
| 47 | + if ("android.test.InstrumentationTestCase".equals(element.getClassName()) && "runMethod".equals(element.getMethodName())) { |
| 48 | + return extractStackElement(trace, i); |
| 49 | + } |
| 50 | + |
| 51 | + if ("org.junit.runners.model.FrameworkMethod$1".equals(element.getClassName()) && "runReflectiveCall".equals(element.getMethodName())) { |
| 52 | + return extractStackElement(trace, i); |
| 53 | + } |
| 54 | + |
| 55 | + if ("cucumber.runtime.model.CucumberFeature".equals(element.getClassName()) && "run".equals(element.getMethodName())) { |
| 56 | + return extractStackElement(trace, i); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + throw new IllegalArgumentException("Could not find test class!"); |
| 61 | + } |
| 62 | + |
| 63 | + private StackTraceElement extractStackElement(StackTraceElement[] trace, int i) { |
| 64 | + int testClassTraceIndex = Build.VERSION.SDK_INT >= 23 ? i - 2 : i - 3; |
| 65 | + return trace[testClassTraceIndex]; |
| 66 | + } |
| 67 | + |
| 68 | + private class CustomScreencaptureProcessor extends BasicScreenCaptureProcessor { |
| 69 | + CustomScreencaptureProcessor() { |
| 70 | +// Storage Directory: /storage/emulated/0/Android/data/<bundle_id>/files/screenshots |
| 71 | +// File screenshotDir = new File(String.valueOf(ApplicationProvider.getApplicationContext().getExternalFilesDir(null)), "screenshots"); |
| 72 | + |
| 73 | +// Storage Directory: /storage/emulated/0/Download/screenshots |
| 74 | + File screenshotDir = new File(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)), "screenshots"); |
| 75 | + File classDir = new File(screenshotDir, className); |
| 76 | + mDefaultScreenshotPath = new File(classDir, methodName); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + protected String getFilename(String filename) { |
| 81 | + String screenshotName = System.currentTimeMillis() + "_" + filename; |
| 82 | + return screenshotName; |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments