如何在Espresso测试失败时截取屏幕截图?

时间:2016-07-22 06:25:28

标签: android testing automated-tests android-espresso

我正在寻找一种在测试失败后和关闭之前拍摄设备屏幕截图的方法。

6 个答案:

答案 0 :(得分:14)

我找到最简单的方法:

@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));
  }
};

答案 1 :(得分:7)

Another improvement to previous answers. I'm using the experimental 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());

答案 2 :(得分:4)

我还没有在我的Android测试中使用屏幕截图,但我知道一些可能有用的解决方案:

最好的方法是使用EmmaSpoon框架。

在这里您可以找到一些有用的信息: 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的ScreenshotTaker

正如我已经知道的那样,使用Robotium测试框架可以创建带有屏幕截图的测试。检查:Correct way to take screenshot with Robotium and Cucumber

如果您不想使用任何库,请查看名为ScreenshotTaker.javaRobotium框架类的源代码[点击链接查看]并编写您自己的ScreenshotTaker

希望它会有所帮助。

答案 3 :(得分:2)

编写自定义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());

这可以确保先截取屏幕截图,然后销毁活动

答案 4 :(得分:0)

我对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();

答案 5 :(得分:0)

@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()))
    }


}

在应用的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的{​​{1}}方法而不是TestWatcher