无法授予在仪器测试清单中声明的 权限

问题描述 投票:0回答:1

我在Android中自动化UI并面临下一个问题:我有Android Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest package="ru.me.widgets"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.INTERNET"/>

    ...

</manifest>

和Android Test Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest package="ru.me.widgets.test"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round">
    </application>
</manifest>

我有使用外部存储器的saveScreenshot方法:

protected final void saveScreenshot() {

    Bitmap bitmap = InstrumentationRegistry.getInstrumentation().getUiAutomation().takeScreenshot();

    File screenshotFile = getScreenshotFile();

    try (FileOutputStream outputStream = new FileOutputStream(screenshotFile)) {
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
        Log.i(mTag, "Saved screenshot: " + screenshotFile.getAbsolutePath());
    } catch (IOException e) {
        Log.e(mTag, "Could not save screenshot", e);
    }
}

private File getScreenshotFile() {

    String path = isExternalWritable(InstrumentationRegistry.getContext())
            ? Environment.getExternalStorageDirectory().toString()
            : Environment.getDataDirectory().toString();

    Date thisMoment = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
    String filename = "screen-" + mTag + "-" + sdf.format(thisMoment) + SCREENSHOT_EXTENSION;

    return new File(path + "/" + filename);
}

private boolean isExternalWritable(Context context) {

    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        if (context.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == 0) {
            return true;
        }

        Log.i(mTag, "No rights for writing to external storage!");
        return false;
    }

    else if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
        Log.i(mTag, "External storage is in ONLY_READ mode!");
        return false;
    }

    Log.i(mTag, "External storage is not mounted!");
    return false;
}

安装应用程序后,我运行UI测试,我可以在设备设置中找到额外的测试应用程序。我为EXTERNAL_WRITE授予权限,但仍然会收到错误:

06-18 12:22:19.998 5566-5582/ru.me.widgets I/DemoActivityTest: No rights for writing to external storage!
06-18 12:22:20.001 5566-5582/ru.me.widgets E/DemoActivityTest: Could not save screenshot
    java.io.FileNotFoundException: /data/screen-DemoActivityTest-2018-06-18_12:22:19.png (Permission denied)

如何在UI测试中保存屏幕截图?为什么在授予设置“Permission denied”错误的外部写入权限后?如果我在清单中设置sharedUserId,一切正常,但我认为这是解决问题的错误方法。

此外,将来,我需要使用GrandPermissionRule自动授予权限。但它只有在App Manifest中设置权限才有效,而不是在Android Test Manifest中。

android user-interface testing permissions manifest
1个回答
0
投票

对于Android 6.0或更高版本,您必须将代码的请求权限添加到此Activity中,而不仅仅是在Manifest中授予权限。

为您提供参考,希望它能帮助您:https://developer.android.com/training/permissions/requesting

© www.soinside.com 2019 - 2024. All rights reserved.