繁体   English   中英

是否有一种简单的方法可以在 Espresso Android Studio 中为每个测试用例截屏

[英]Is there a Simple Way to Take Screenshot in Espresso Android Studio for Every Test Case

伙计们,我是 Espresso 的初学者,请求您的宝贵帮助,谁能分享我可以为我执行的每个测试截取屏幕截图的代码?

我会尝试根据您提供的信息进行指导。 首先,您需要知道测试用例的名称。 其次,我假设您已经授予写入/读取外部存储权限。第三,我假设您还添加了 UI Automator 依赖项。 所以实现将是这样的:

    object ScreenshotUtil : TestWatcher() {
    
        lateinit var description: Description

        override fun starting(description: Description) {
            super.starting(description)
            this.description = description
        }

        fun captureScreenshot() {
           val capture = Screenshot.capture()
           capture.format = Bitmap.CompressFormat.PNG
           capture.name = description.methodName
           val processors = HashSet<ScreenCaptureProcessor>()
           processors.add(BasicScreenCaptureProcessor())
           try {
               capture.process(processors)
           } catch (e: IOException) {
               throw IllegalStateException(e)
           }
        }

   }

然后你可以像这样使用它:

ScreenshotUtil.captureScreenshot()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM