Skip to content

Commit 4773611

Browse files
committed
feat: Use androidx screenshots to support newer android versions
1 parent ad55940 commit 4773611

File tree

3 files changed

+115
-12
lines changed

3 files changed

+115
-12
lines changed

app/src/androidTest/java/com/sample/browserstack/samplecalculator/EnsureInputTests.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import androidx.test.rule.ActivityTestRule;
55
import androidx.test.ext.junit.runners.AndroidJUnit4;
66

7-
import com.squareup.spoon.Spoon;
8-
97
import org.junit.Before;
108
import org.junit.Rule;
119
import org.junit.Test;
@@ -39,18 +37,24 @@ public void setUp() {
3937

4038
@Test
4139
public void ensureSingleInputIsHandled() {
42-
Spoon.screenshot(mainActivity, "initial_state");
40+
ScreenshotUtils screenshotUtils = new ScreenshotUtils();
41+
screenshotUtils.captureScreenshot("initial_state");
42+
4343
onView(withId(R.id.buttonOne)).perform(click());
4444
onView(withId(R.id.editText)).check(matches(withText("1")));
45-
Spoon.screenshot(mainActivity, "post_multiple_btn_click");
45+
46+
screenshotUtils.captureScreenshot("post_single_btn_click");
4647
}
4748

4849
@Test
4950
public void ensureMultipleInputIsHandled() {
50-
Spoon.screenshot(mainActivity, "initial_state");
51+
ScreenshotUtils screenshotUtils = new ScreenshotUtils();
52+
screenshotUtils.captureScreenshot("initial_state");
53+
5154
onView(withId(R.id.buttonOne)).perform(click());
5255
onView(withId(R.id.buttonTwo)).perform(click());
5356
onView(withId(R.id.editText)).check(matches(withText("12")));
54-
Spoon.screenshot(mainActivity, "post_multiple_btn_click");
57+
58+
screenshotUtils.captureScreenshot("post_multiple_btn_click");
5559
}
5660
}

app/src/androidTest/java/com/sample/browserstack/samplecalculator/EnsureOperationTests.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
import androidx.test.rule.ActivityTestRule;
55
import androidx.test.ext.junit.runners.AndroidJUnit4;
66

7-
import com.squareup.spoon.Spoon;
8-
97
import org.junit.Before;
108
import org.junit.Rule;
119
import org.junit.Test;
@@ -37,6 +35,14 @@ public void setUp() {
3735
mainActivity = activityRule.getActivity();
3836
}
3937

38+
void childScreenshotMethod(String screenshotName) {
39+
ScreenshotUtils screenshotUtils = new ScreenshotUtils();
40+
screenshotUtils.captureScreenshot(screenshotName);
41+
}
42+
43+
void parentScreenshotMethod(String screenshotName) {
44+
childScreenshotMethod(screenshotName);
45+
}
4046

4147
@Test
4248
public void ensureAdditionWorks() {
@@ -47,19 +53,25 @@ public void ensureAdditionWorks() {
4753
onView(withId(R.id.buttonOne)).perform(click());
4854
onView(withId(R.id.buttonEqual)).perform(click());
4955
onView(withId(R.id.editText)).check(matches(withText("33")));
50-
Spoon.screenshot(mainActivity, "post_addition");
56+
57+
ScreenshotUtils screenshotUtils = new ScreenshotUtils();
58+
screenshotUtils.captureScreenshot("post_addition");
5159
}
5260

5361
@Test
5462
public void ensureSubtractionWorks() {
63+
ScreenshotUtils screenshotUtils = new ScreenshotUtils();
64+
screenshotUtils.captureScreenshot("pre_subtraction");
65+
5566
onView(withId(R.id.buttonTwo)).perform(click());
5667
onView(withId(R.id.buttonTwo)).perform(click());
5768
onView(withId(R.id.buttonSubtract)).perform(click());
5869
onView(withId(R.id.buttonOne)).perform(click());
5970
onView(withId(R.id.buttonOne)).perform(click());
6071
onView(withId(R.id.buttonEqual)).perform(click());
6172
onView(withId(R.id.editText)).check(matches(withText("11")));
62-
Spoon.screenshot(mainActivity, "post_subtraction");
73+
74+
screenshotUtils.captureScreenshot("post_subtraction");
6375
}
6476

6577
@Test
@@ -70,7 +82,8 @@ public void ensureMultiplicationWorks() {
7082
onView(withId(R.id.buttonFive)).perform(click());
7183
onView(withId(R.id.buttonEqual)).perform(click());
7284
onView(withId(R.id.editText)).check(matches(withText("60")));
73-
Spoon.screenshot(mainActivity, "post_multiplication");
85+
86+
childScreenshotMethod("post_multiplication");
7487
}
7588

7689
@Test
@@ -81,6 +94,7 @@ public void ensureDivisionWorks() {
8194
onView(withId(R.id.buttonThree)).perform(click());
8295
onView(withId(R.id.buttonEqual)).perform(click());
8396
onView(withId(R.id.editText)).check(matches(withText("4")));
84-
Spoon.screenshot(mainActivity, "post_division");
97+
98+
parentScreenshotMethod("post_division");
8599
}
86100
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

Comments
 (0)