【问题标题】:Is there a Simple Way to Take Screenshot in Espresso Android Studio for Every Test Case是否有一种简单的方法可以在 Espresso Android Studio 中为每个测试用例截屏
【发布时间】:2022-07-10 15:28:11
【问题描述】:

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

【问题讨论】:

  • 欢迎来到 Stack Overflow。通常在 stackoverflow 中不鼓励请求代码,并且很可能会被关闭。请参阅here 了解如何编写有效的问题。
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: java android-espresso


【解决方案1】:

我会尝试根据您提供的信息进行指导。 首先,您需要知道测试用例的名称。 其次,我假设您已经授予写入/读取外部存储权限 第三,我假设您还添加了 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()

【讨论】:

    猜你喜欢
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多