Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Screenshot sample ot latest versions and API. #511

Merged
merged 1 commit into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ui/espresso/ScreenshotSample/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {

defaultConfig {
applicationId "com.example.android.testing.espresso.screenshotsample"
minSdk 18
minSdk 19
targetSdkVersion 33
versionCode 1
versionName "1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@
import static androidx.test.core.app.DeviceCapture.takeScreenshot;
import static androidx.test.core.graphics.BitmapStorage.writeToTestStorage;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.captureToBitmap;
import static androidx.test.espresso.matcher.ViewMatchers.isRoot;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static androidx.test.espresso.screenshot.ViewInteractionCapture.captureToBitmap;

import android.view.View;
import android.graphics.Bitmap;

import androidx.concurrent.futures.ResolvableFuture;
import androidx.test.core.app.ActivityScenario;
import androidx.test.core.view.ViewCapture;
import androidx.test.espresso.action.ViewActions;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;

Expand All @@ -21,8 +19,6 @@
import org.junit.runner.RunWith;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
* Equivalent of {@link ScreenshotTest} for java.
Expand All @@ -42,15 +38,35 @@ public class ScreenshotJavaTest {
*/
@Test
public void saveActivityBitmap() throws IOException {
writeToTestStorage(captureToBitmap(onView(isRoot())), getClass().getSimpleName() + "_" + nameRule.getMethodName());
onView(isRoot()).perform(captureToBitmap(new ViewActions.BitmapReceiver() {
@Override
public void onBitmapCaptured(Bitmap bitmap) {
try {
writeToTestStorage(bitmap, ScreenshotJavaTest.class.getSimpleName() + "_" + nameRule.getMethodName());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}));
}

/**
* Captures and saves an image of the 'Hello world' view.
*/
@Test
public void saveViewBitmap() throws IOException {
writeToTestStorage(captureToBitmap(onView(withText("Hello World!"))), getClass().getSimpleName() + "_" + nameRule.getMethodName());
onView(withText("Hello World!")).perform(captureToBitmap(new ViewActions.BitmapReceiver() {
@Override
public void onBitmapCaptured(Bitmap bitmap) {
try {
writeToTestStorage(bitmap, ScreenshotJavaTest.class.getSimpleName() + "_" + nameRule.getMethodName());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}));


}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.example.android.testing.espresso.screenshotsample

import android.graphics.Bitmap
import androidx.test.core.app.takeScreenshot
import androidx.test.core.graphics.writeToTestStorage
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.action.ViewActions.captureToBitmap
import androidx.test.espresso.matcher.ViewMatchers.isRoot
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.espresso.screenshot.captureToBitmap

import androidx.test.ext.junit.rules.activityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Rule
Expand All @@ -23,43 +26,41 @@ import java.io.IOException
@RunWith(AndroidJUnit4::class)
class ScreenshotTest {

// a handy JUnit rule that stores the method name, so it can be used to generate unique
// screenshot files per test method
@get:Rule
var nameRule = TestName()
// a handy JUnit rule that stores the method name, so it can be used to generate unique
// screenshot files per test method
@get:Rule
var nameRule = TestName()

@get:Rule
val activityScenarioRule = activityScenarioRule<MainActivity>()
@get:Rule
val activityScenarioRule = activityScenarioRule<MainActivity>()

/**
* Captures and saves an image of the entire [MainActivity] contents.
*/
@Test
@Throws(IOException::class)
fun saveActivityBitmap() {
onView(isRoot())
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")
}
/**
* Captures and saves an image of the entire [MainActivity] contents.
*/
@Test
@Throws(IOException::class)
fun saveActivityBitmap() {
onView(isRoot())
.perform(captureToBitmap({ bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}") }))
}

/**
* Captures and saves an image of the 'Hello world' view.
*/
@Test
@Throws(IOException::class)
fun saveViewBitmap() {
onView(withText("Hello World!"))
.captureToBitmap()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")
}
/**
* Captures and saves an image of the 'Hello world' view.
*/
@Test
@Throws(IOException::class)
fun saveViewBitmap() {
onView(withText("Hello World!"))
.perform(captureToBitmap({ bitmap: Bitmap -> bitmap.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}") }))
}

/**
* Captures and saves an image of the entire device screen to storage.
*/
@Test
@Throws(IOException::class)
fun saveDeviceScreenBitmap() {
takeScreenshot()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")
}
/**
* Captures and saves an image of the entire device screen to storage.
*/
@Test
@Throws(IOException::class)
fun saveDeviceScreenBitmap() {
takeScreenshot()
.writeToTestStorage("${javaClass.simpleName}_${nameRule.methodName}")
}
}
Loading