我正在寻找一种在测试失败之后和关闭之前拍摄设备屏幕截图的方法。
我发现最简单的方法:
@Rule
public TestRule watcher = new TestWatcher() {
@Override
protected void failed(Throwable e, Description description) {
// Save to external storage (usually /sdcard/screenshots)
File path = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/screenshots/" + getTargetContext().getPackageName());
if (!path.exists()) {
path.mkdirs();
}
// Take advantage of UiAutomator screenshot method
UiDevice device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
String filename = description.getClassName() + "-" + description.getMethodName() + ".png";
device.takeScreenshot(new File(path, filename));
}
};
以前答案的另一个改进。我正在使用实验性的Screenshot API
public class ScreenshotTestRule extends TestWatcher {
@Override
protected void failed(Throwable e, Description description) {
super.failed(e, description);
takeScreenshot(description);
}
private void takeScreenshot(Description description) {
String filename = description.getTestClass().getSimpleName() + "-" + description.getMethodName();
ScreenCapture capture = Screenshot.capture();
capture.setName(filename);
capture.setFormat(CompressFormat.PNG);
HashSet<ScreenCaptureProcessor> processors = new HashSet<>();
processors.add(new CustomScreenCaptureProcessor());
try {
capture.process(processors);
} catch (IOException e) {
e.printStackTrace();
}
}
}
我创建了CustomScreenCaptureProcessor,因为BasicScreenCaptureProcessor使用/ sdcard / Pictures /文件夹,我在创建文件夹/图像时遇到了某些设备上的IOExceptions。请注意,您需要将处理器放在同一个包装中
package android.support.test.runner.screenshot;
public class CustomScreenCaptureProcessor extends BasicScreenCaptureProcessor {
public CustomScreenCaptureProcessor() {
super(
new File(
InstrumentationRegistry.getTargetContext().getExternalFilesDir(DIRECTORY_PICTURES),
"espresso_screenshots"
)
);
}
}
然后,在您的基础Espresso测试类中添加
@Rule
public ScreenshotTestRule screenshotTestRule = new ScreenshotTestRule();
如果你想使用一些受保护的文件夹,这可以在模拟器上完成,但它在物理设备上不起作用
@Rule
public RuleChain screenshotRule = RuleChain
.outerRule(GrantPermissionRule.grant(permission.WRITE_EXTERNAL_STORAGE))
.around(new ScreenshotTestRule());
我还没有在Android测试中使用屏幕截图,但我知道一些可能有用的解决方案:
最好的方法是使用Emma
或Spoon
框架。
在这里您可以找到一些有用的信息:http://elekslabs.com/2014/05/creating-test-reports-for-android-with-spoon-and-emma.html
访问Spoon
的官方Github网站:https://github.com/square/spoon及其Gradle的插件:https://github.com/stanfy/spoon-gradle-plugin
并检查相关主题:How to get Spoon to take screenshots for Espresso tests?
你也可以尝试这个Facebook的库:https://facebook.github.io/screenshot-tests-for-android/
正如我已经知道的那样,使用Robotium
测试框架可以创建带截图的测试。检查:Correct way to take screenshot with Robotium and Cucumber
如果您不想使用任何库,请查看Robotium
框架类的源代码ScreenshotTaker.java
[点击链接查看]并编写您自己的ScreenshotTaker
类。
希望它会有所帮助。
像解释的其他答案一样编写自定义TestWatcher是要走的路。
但是(我们花了很长时间才注意到这一点)有一个警告:规则可能起得太晚,即你的活动已经被摧毁。这将为您提供设备主屏幕的屏幕截图,而不是失败活动的屏幕截图。
你可以使用RuleChain解决这个问题:而不是写作
@Rule
public final ActivityTestRule<MainActivity> _activityRule = new ActivityTestRule<>(MainActivity.class);
@Rule
public ScreenshotTestWatcher _screenshotWatcher = new ScreenshotTestWatcher();
你必须写:
private final ActivityTestRule<MainActivity> _activityRule = new ActivityTestRule<>(MainActivity.class);
@Rule
public final TestRule activityAndScreenshotRule = RuleChain
.outerRule(_activityRule)
.around(new ScreenshotTestWatcher());
这样可以确保首先拍摄屏幕截图,然后销毁活动
我对this的答案做了一些改进。无需为UiAutomator添加额外的依赖项,它也可以在api级别18下运行。
public class ScreenshotTestWatcher extends TestWatcher
{
private static Activity currentActivity;
@Override
protected void failed(Throwable e, Description description)
{
Bitmap bitmap;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
{
bitmap = getInstrumentation().getUiAutomation().takeScreenshot();
}
else
{
// only in-app view-elements are visible.
bitmap = Screenshot.capture(getCurrentActivity()).getBitmap();
}
// Save to external storage '/storage/emulated/0/Android/data/[package name app]/cache/screenshots/'.
File folder = new File(getTargetContext().getExternalCacheDir().getAbsolutePath() + "/screenshots/");
if (!folder.exists())
{
folder.mkdirs();
}
storeBitmap(bitmap, folder.getPath() + "/" + getFileName(description));
}
private String getFileName(Description description)
{
String className = description.getClassName();
String methodName = description.getMethodName();
String dateTime = Calendar.getInstance().getTime().toString();
return className + "-" + methodName + "-" + dateTime + ".png";
}
private void storeBitmap(Bitmap bitmap, String path)
{
BufferedOutputStream out = null;
try
{
out = new BufferedOutputStream(new FileOutputStream(path));
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (out != null)
{
try
{
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
private static Activity getCurrentActivity()
{
getInstrumentation().runOnMainSync(new Runnable()
{
public void run()
{
Collection resumedActivities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(
RESUMED);
if (resumedActivities.iterator().hasNext())
{
currentActivity = (Activity) resumedActivities.iterator().next();
}
}
});
return currentActivity;
}
}
然后在测试类中包含以下行:
@Rule
public TestRule watcher = new ScreenshotTestWatcher();
@Maragues答案移植到Kotlin:
助手班:
package utils
import android.graphics.Bitmap
import android.os.Environment.DIRECTORY_PICTURES
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import androidx.test.runner.screenshot.BasicScreenCaptureProcessor
import androidx.test.runner.screenshot.ScreenCaptureProcessor
import androidx.test.runner.screenshot.Screenshot
import org.junit.rules.TestWatcher
import org.junit.runner.Description
import java.io.File
import java.io.IOException
class IDTScreenCaptureProcessor : BasicScreenCaptureProcessor() {
init {
mTag = "IDTScreenCaptureProcessor"
mFileNameDelimiter = "-"
mDefaultFilenamePrefix = "Giorgos"
mDefaultScreenshotPath = getNewFilename()
}
private fun getNewFilename(): File? {
val context = getInstrumentation().getTargetContext().getApplicationContext()
return context.getExternalFilesDir(DIRECTORY_PICTURES)
}
}
class ScreenshotTestRule : TestWatcher() {
override fun finished(description: Description?) {
super.finished(description)
val className = description?.testClass?.simpleName ?: "NullClassname"
val methodName = description?.methodName ?: "NullMethodName"
val filename = "$className - $methodName"
val capture = Screenshot.capture()
capture.name = filename
capture.format = Bitmap.CompressFormat.PNG
val processors = HashSet<ScreenCaptureProcessor>()
processors.add(IDTScreenCaptureProcessor())
try {
capture.process(processors)
} catch (ioException: IOException) {
ioException.printStackTrace()
}
}
}
用法:
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import androidx.test.rule.ActivityTestRule
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import utils.ScreenshotTestRule
@RunWith(AndroidJUnit4::class)
@LargeTest
class DialogActivityTest {
@get:Rule
val activityRule = ActivityTestRule(DialogActivity::class.java)
@get:Rule
val screenshotTestRule = ScreenshotTestRule()
@Test
fun dialogLaunch_withTitleAndBody_displaysDialog() {
// setup
val title = "title"
val body = "body"
// assert
onView(withText(title)).check(matches(isCompletelyDisplayed()))
onView(withText(body)).check(matches(isCompletelyDisplayed()))
}
}
在app的build.gradle
中声明的库:
androidTestImplementation "androidx.test.espresso:espresso-core:3.1.1"
androidTestImplementation "androidx.test.espresso:espresso-intents:3.1.1"
androidTestImplementation "androidx.test.ext:junit:1.1.0"
androidTestImplementation "androidx.test:runner:1.1.1"
androidTestImplementation "androidx.test:rules:1.1.1"
每次测试在文件夹中完成时,此设置都会保存屏幕截图:/sdcard/Android/data/your.package.name/files/Pictures
通过Android Studio的设备文件资源管理器导航(在右侧边栏上)
如果您只想为失败的测试保存屏幕截图,请覆盖failed
的TestWatcher
方法而不是finished