微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

UIAutomator-失败后的屏幕截图

如何解决UIAutomator-失败后的屏幕截图

我是Android测试的新手。我正在使用UIAutomator进行一些黑盒测试。每当测试失败时,我都想截图。我试图通过在测试类中添加规则来使用TestWatcher类。但是,它不起作用。我想我们在使用UIAutomator时不能使用@Rule注释。 Espresso有很多相关主题,但我没有为UIAutomator找到任何东西。 有人有办法解决我的问题吗?

谢谢!

解决方法

是的,您可以通过以下方式做到这一点:

@RunWith(AndroidJUnit4.class)
public class TestClass {
  @Rule
  public ScreenshotTakingRule rule = new ScreenshotTakingRule();

  @Test
  public void yourTest() {
  
  }
}

/**
 * Junit rule that takes a screenshot when a test fails.
 */
public class ScreenshotTakingRule extends TestWatcher {
    @Override
    protected void failed(Throwable e,Description description) {
        Log.d(TAG,"taking screenshot..." + description.getMethodName());
        takeScreenshot(description.getMethodName());
    }

    private void takeScreenshot(String screenShotName) {
        ScreenCapture screenCapture = Screenshot.capture();
        try {
            screenCapture.setName(screenShotName);
            Set<ScreenCaptureProcessor> processorSet = new HashSet<>();
            processorSet.add(new SimpleNameScreenCaptureProcessor());
            screenCapture.process(processorSet);
        } catch (IOException ex) {
            Log.d(TAG,"error while taking screenshot " + ex.getMessage());
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。