原因 = {ErrnoException@5190} "android.system.ErrnoException: open failed: EACCES (Permission denied)"



我是测试团队的一员,使用浓缩咖啡探索测试记录器来测试我们的Android应用程序。

计划在 AWS 设备场上运行测试。

按照他们的 3 部分教程@ http://mobile.awsblog.com/post/Tx20RGXMTYT2ZGZ%20/Getting-started-with-Android-testing-on-AWS-Device-Farm-using-Espresso-Part-3

但是我被困住了,因为我无法获得测试的屏幕截图。将此配置作为安卓清单的一部分.xml

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

我使用以下链接中的源代码来获取屏幕截图

https://github.com/awslabs/aws-device-farm-sample-app-for-android/tree/master/app/src/androidTest/java/com/amazonaws/devicefarm/android/referenceapp/Util

但是,运行测试时我仍然被拒绝权限。

使用模拟器运行这些测试,从 Nexus 5 (API 23) 和 Nexus 6 (API 23) 开始

权限弹出窗口显示在测试环境中时,您将需要使用UIAutomator框架按yes。以下片段取自这篇媒体帖子,请务必查看。

private void allowPermissionsIfNeeded()  {
    if (Build.VERSION.SDK_INT >= 23) {
        UiObject allowPermissions = mDevice.findObject(new UiSelector().text("Allow"));
        if (allowPermissions.exists()) {
            try {
                allowPermissions.click();
            } catch (UiObjectNotFoundException e) {
                Timber.e(e, "There is no permissions dialog to interact with ");
            }
        }
    }
}

您可能需要在运行时请求权限,因为您是在具有 API 23 的 Android 设备上运行它。

查看此链接以查看有关如何执行此操作的完整文档。


下面是一个小示例,演示如何请求读取设备中呼叫日志的权限。您必须稍微更改一下逻辑以请求WRITE_EXTERNAL_STORAGE而不是READ_CALL_LOG权限。

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivityTAG_";
    private static final int CODE_CALL_LOG = 10;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CALL_LOG)) {
                Log.d(TAG, "onCreate: " + "Show explanation");
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CALL_LOG}, CODE_CALL_LOG);
            } else {
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CALL_LOG}, CODE_CALL_LOG);
            }
        } else {
            Log.d(TAG, "onCreate: " + "Permission already granted!");
            printCallLog();
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case CODE_CALL_LOG: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d(TAG, "onRequestPermissionsResult: Good to go!");
                    printCallLog();
                } else {
                    Log.d(TAG, "onRequestPermissionsResult: Bad user");
                }
            }
        }
    }
    private void printCallLog() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) == PackageManager.PERMISSION_GRANTED) {
            Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI,
                    null,
                    null,
                    null,
                    null);
            while (cursor.moveToNext()) {
                final long dateDialed = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE));
                final String numberDialed = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
                Log.d(TAG, "Call to number: " + numberDialed + "t registered at: " + new Date(dateDialed).toString());
            }
            cursor.close();
        }
    }
}

这是与完整 GitHub 项目的链接。

相关内容

  • 没有找到相关文章

最新更新