有没有办法以编程方式将文件从Android Studio模拟器移动到主机设备?

huangapple go评论56阅读模式
英文:

Is there a way to move files from the Android Studio emulator to host device programmatically?

问题

我正在为我的应用程序实现屏幕截图测试功能,在JUnit测试中通过将根视图的位图写入模拟器文件系统中的文件来获取屏幕截图。我希望将该屏幕截图文件保存到托管存储库所在的主机文件系统中;然而,屏幕截图保存在模拟器的文件系统中。此外,在当前启用的设置中,模拟器在每次运行测试后都会重新启动,因此在测试运行后失去了屏幕截图。

在我的JUnit测试案例中使用Kotlin代码,我想知道是否有人了解如何在模拟器运行时以编程方式将位于模拟器中的文件发送到主机文件系统。我不确定是否有可能实现这一点;然而,对于解决这个问题的方法的任何指导都将不胜感激。谢谢!

我已经尝试在测试用例运行后发送文件;然而,由于模拟器在每次测试用例运行后重新启动,所以除了在最后一个测试用例中生成的屏幕截图之外,所有其他屏幕截图都丢失了,尽管我期望它们会保留下来。

英文:

I'm implementing functionality for screenshot testing for my application where I take a screenshot within JUnit test by writing the bitmap of the root view to a file in the emulator file system. I'm looking to save that screenshot file to the host file system where my repository exists; however, the screenshot is saved on the emulator's file system. Furthermore, in the current set of settings that are enabled, the emulator restarts after each test is run, so it looses the screenshot after a test is run.

Using Kotlin code in my JUnit test cases, I was wondering if anybody would be knowledgeable on how to send files that are in the emulator to the host file system programmatically in the emulator run-time. I'm unsure if this is possible; however, any guidance on approaching this issue would be greatly appreciated. Thank you!

I've tried sending files after the test cases are run; however, all screenshots except those that are made in the final test case are lost due to the emulator restarting after each test case runs, despite my expectation that they would persist.

答案1

得分: 0

使用androidx.test.services中的TestStorage API可以解决问题,它允许将文件保存在特定文件夹中,不会在每次测试后重置。

英文:

Just figured it out, using the TestStorage API from androidx.test.services allows for files to be maintained throughout the state of the application by saving them into a specific folder that does not reset after each test.

huangapple
  • 本文由 发表于 2023年6月9日 02:59:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定