RuntimeException:无法创建输出目录:/ storage/emulated/0/app_spoon-screenshots

Mar*_*ark 9 android ui-testing spoon barista

我正在使用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)
Run Code Online (Sandbox Code Playgroud)

如果我在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>
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,我都看到这些权限被合并到我的应用程序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")
Run Code Online (Sandbox Code Playgroud)

这将输出一个Permission denied到Logcat并导致上面相同的异常.

如果我尝试利用GrantPermissionRule:

@get:Rule var runtimePermissionRule = GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
Run Code Online (Sandbox Code Playgroud)

我得到一个不同的例外:

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!
Run Code Online (Sandbox Code Playgroud)

删除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)
Run Code Online (Sandbox Code Playgroud)

他们也没有运气.

更新:

我尝试使我的测试应用程序目标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>
Run Code Online (Sandbox Code Playgroud)

通过Gradle(./gradlew spoon)我尝试配置gradle-spoon-plugin来请求所有权限:

spoon {
    // Grant all runtime permissions during installation on Marshmallow and above devices.
    grantAll = true
}
Run Code Online (Sandbox Code Playgroud)

没有运气.我甚至尝试使用Spoon库版本1.3.1也无济于事.

Mar*_*ark 7

我的问题是由外部库合并 maxSdkVersion 与 WRITE_EXTERNAL_STORAGE 权限引起的。为了解决这个问题,我删除了该属性并重新添加了 WRITE_EXTERNAL_STORAGE 权限。这仅适用于我的应用程序清单,而不是测试应用程序清单。

<!-- Strip away maxSdkVersion -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
                 tools:remove="android:maxSdkVersion"/>

<!-- Add the permission with no maxSdkVersion defined -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Run Code Online (Sandbox Code Playgroud)

然后您可以构建、授予权限并运行测试:

# Uninstall existing APKs and install our app APK and test APK
./gradlew uninstallAll installDebug installDebugAndroidTest

# List all APKs installed with adb shell 'pm list packages -f'
# Grant the app APK write and read to external storage permissions
adb shell pm grant gg.mark.debug android.permission.WRITE_EXTERNAL_STORAGE
adb shell pm grant gg.mark.debug android.permission.READ_EXTERNAL_STORAGE

export APK=build/outputs/apk/debug/debug.apk
export TEST_APK=build/outputs/apk/androidTest/debug/debug-androidTest.apk

# TEST_APK and APK are positional arguments so keep them in this order
# Disable GIF generation because it's slow
java -jar spoon-runner-2.0.0.jar --debug --disable-gif "$TEST_APK" "$APK"
Run Code Online (Sandbox Code Playgroud)

pm grant如果没有为您的应用程序的权限正确命令将失败的合并AndroidManifest.xml中。确保这些命令成功!