|
| 1 | +package com.example.android.testing.espresso.screenshotsample; |
| 2 | + |
| 3 | +import static androidx.test.core.app.DeviceCapture.takeScreenshot; |
| 4 | +import static androidx.test.core.graphics.BitmapStorage.writeToTestStorage; |
| 5 | +import static androidx.test.espresso.Espresso.onView; |
| 6 | +import static androidx.test.espresso.matcher.ViewMatchers.isRoot; |
| 7 | +import static androidx.test.espresso.matcher.ViewMatchers.withText; |
| 8 | +import static androidx.test.espresso.screenshot.ViewInteractionCapture.captureToBitmap; |
| 9 | + |
| 10 | +import android.view.View; |
| 11 | + |
| 12 | +import androidx.concurrent.futures.ResolvableFuture; |
| 13 | +import androidx.test.core.app.ActivityScenario; |
| 14 | +import androidx.test.core.view.ViewCapture; |
| 15 | +import androidx.test.ext.junit.rules.ActivityScenarioRule; |
| 16 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 17 | + |
| 18 | +import org.junit.Rule; |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.rules.TestName; |
| 21 | +import org.junit.runner.RunWith; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.util.concurrent.ExecutionException; |
| 25 | +import java.util.concurrent.Future; |
| 26 | + |
| 27 | +/** |
| 28 | + * Equivalent of {@link ScreenshotTest} for java. |
| 29 | + */ |
| 30 | +@RunWith(AndroidJUnit4.class) |
| 31 | +public class ScreenshotJavaTest { |
| 32 | + // a handy JUnit rule that stores the method name |
| 33 | + @Rule |
| 34 | + public TestName nameRule = new TestName(); |
| 35 | + |
| 36 | + @Rule |
| 37 | + public ActivityScenarioRule<MainActivity> activityScenarioRule = |
| 38 | + new ActivityScenarioRule<>(MainActivity.class); |
| 39 | + |
| 40 | + /** |
| 41 | + * Captures and saves an image of the entire {@link MainActivity} contents. |
| 42 | + */ |
| 43 | + @Test |
| 44 | + public void saveActivityBitmap() throws IOException { |
| 45 | + writeToTestStorage(captureToBitmap(onView(isRoot())), nameRule.getMethodName()); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Captures and saves an image of the 'Hello world' view. |
| 50 | + */ |
| 51 | + @Test |
| 52 | + public void saveViewBitmap() throws IOException { |
| 53 | + writeToTestStorage(captureToBitmap(onView(withText("Hello World!"))), nameRule.getMethodName()); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Captures and saves an image of the entire device screen to storage. |
| 58 | + */ |
| 59 | + @Test |
| 60 | + public void saveDeviceScreenBitmap() throws IOException { |
| 61 | + writeToTestStorage(takeScreenshot(), nameRule.getMethodName()); |
| 62 | + } |
| 63 | +} |
0 commit comments