cause = {ErrnoException @ 5190}“android.system.ErrnoException:open failed:EACCES(Permission denied)”

时间:2016-07-10 17:34:49

标签: android android-testing permission-denied android-espresso aws-device-farm

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

计划在AWS Device Farm上运行测试。

按照他们的3部分教程@ War

但是我被卡住了,因为我无法获得测试的屏幕截图。 将此配置作为androidmanifest.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"/>

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

http://mobile.awsblog.com/post/Tx20RGXMTYT2ZGZ%20/Getting-started-with-Android-testing-on-AWS-Device-Farm-using-Espresso-Part-3

但是,在运行测试时我仍然会被拒绝。

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

开始

1 个答案:

答案 0 :(得分:0)

当权限弹出窗口出现在测试环境中时,您需要使用UIAutomator框架按yes。以下代码段取自this medium post,请务必查看。

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设备上运行它。

检查this link以查看有关如何操作的完整文档。

以下是一个小示例,说明如何请求读取设备中的通话记录的权限。您需要更改逻辑以请求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();
        }
    }
}

This is the link使用完整的GitHub项目。