【发布时间】:2018-08-08 22:52:50
【问题描述】:
我正在使用 Spoon 2.0.0 快照使用 gradle-spoon-plugin 为 Android UI 测试设置 Spoon。我的项目是使用 Android Gradle 插件 3.0.1 设置的。
当通过spoonRule.screenshot(activity, "hello") 截屏时,我得到了这个 RuntimeException:
java.lang.RuntimeException: Unable to create output dir: /storage/emulated/0/app_spoon-screenshots
at com.squareup.spoon.SpoonRule.createDir(SpoonRule.java:167)
at com.squareup.spoon.SpoonRule.createDir(SpoonRule.java:164)
at com.squareup.spoon.SpoonRule.createDir(SpoonRule.java:164)
at com.squareup.spoon.SpoonRule.obtainDirectory(SpoonRule.java:108)
at com.squareup.spoon.SpoonRule.screenshot(SpoonRule.java:66)
如果我在 Nexus 4 API 19 模拟器上运行它,一切正常,但它不适用于 Pixel 2 API 27 模拟器。权限已从 19 更改为 27,因此这并不完全出乎意料。
我已经尝试了当前可用的大多数建议,包括在我的 androidTest 目录中添加一个清单,以授予读写外部存储(有和没有maxSdkVersion):
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="tv.twitch.android.test">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="18"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18"/>
<uses-sdk tools:overrideLibrary="android.support.test.uiautomator.v18"/>
</manifest>
在这两种情况下,我都看到这些权限被合并到我的应用 AndroidManifest 的最终清单中(不确定如何检查测试应用的清单)。
我尝试通过 UIAutomator 向应用程序和测试包授予权限:
val device = UiDevice.getInstance(getInstrumentation())
device.executeShellCommand("pm grant tv.twitch.android.test android.permission.READ_EXTERNAL_STORAGE")
device.executeShellCommand("pm grant tv.twitch.android.debug android.permission.READ_EXTERNAL_STORAGE")
device.executeShellCommand("pm grant tv.twitch.android.test android.permission.WRITE_EXTERNAL_STORAGE")
device.executeShellCommand("pm grant tv.twitch.android.debug android.permission.WRITE_EXTERNAL_STORAGE")
这会向 Logcat 输出 Permission denied 并导致上述相同的异常。
如果我尝试利用 GrantPermissionRule 喜欢:
@get:Rule var runtimePermissionRule = GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
我得到一个不同的例外:
junit.framework.AssertionFailedError: Failed to grant permissions, see logcat for details
at junit.framework.Assert.fail(Assert.java:50)
at android.support.test.runner.permission.PermissionRequester.requestPermissions(PermissionRequester.java:110)
at android.support.test.rule.GrantPermissionRule$RequestPermissionStatement.evaluate(GrantPermissionRule.java:108)
GrantPermissionCallable: Permission: android.permission.WRITE_EXTERNAL_STORAGE cannot be granted!
删除 Manifest.permission.WRITE_EXTERNAL_STORAGE 并只留下已读的,让我们回到原来的异常:java.lang.RuntimeException: Unable to create output dir: /storage/emulated/0/app_spoon-screenshots
在 Android Studio 中或在命令行上使用 gradle-spoon-plugin 运行不会影响上述任何一项。
查看我的应用程序和测试应用程序在设置中授予的权限,我看到我的应用程序具有存储权限,但我的测试应用程序 (tv.twitch.android.test) 没有任何请求的权限。
我也尝试过使用Barista 库:
PermissionGranter.allowPermissionsIfNeeded(Manifest.permission.WRITE_EXTERNAL_STORAGE)
他们也没有运气。
更新:
我尝试让我的测试应用以 SDK 版本 22 为目标,希望它能获得写入权限。
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="tv.twitch.android.test">
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="22"
tools:overrideLibrary="android.support.test.uiautomator.v18"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
通过 Gradle (./gradlew spoon) 我尝试配置 gradle-spoon-plugin 以请求所有权限:
spoon {
// Grant all runtime permissions during installation on Marshmallow and above devices.
grantAll = true
}
没有运气。我什至尝试使用 Spoon 库版本 1.3.1 也无济于事。
【问题讨论】:
标签: android ui-testing spoon barista